0% found this document useful (0 votes)
11 views

6 - Controles Comunes - Button

Viasual studio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

6 - Controles Comunes - Button

Viasual studio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6 - Controles comunes - Button

Un control común a disponer dentro de un Form son los botones, esto se hace disponiendo
objetos de la clase Button.

Problema 1:

Confeccionar un formulario que muestre tres objetos de la clase Button, disponer como
etiqueta en cada botón los valores 1,2 y 3. Cuando se presiona el botón mostrar en el título
del formulario el valor de la etiqueta del botón presionado.

Programa:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Button1_Click(object sender, EventArgs e)


{
Text = button1.Text;
}

private void Button2_Click(object sender, EventArgs e)


{
Text = button2.Text;
}

private void Button3_Click(object sender, EventArgs e)


{
Text = button3.Text;
}
}
}
Para el evento click de cada botón inicializamos la propiedad Text del
formulario con la propiedad Text del botón presionado (como la clase
Form1 hereda de la clase Form luego accedemos a la propiedad Text sin
anteceder nombre alguno: Text = button1.Text; ):
private void button1_Click(object sender, EventArgs e)
{
Text = button1.Text;
}

Problema 2:

Modificar el problema anterior para que se acumulen en el título del formulario los valores
de los botones presionados.

Programa:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Button1_Click(object sender, EventArgs e)


{
Text = Text + button1.Text;
}

private void Button2_Click(object sender, EventArgs e)


{
Text = Text + button2.Text;
}

private void Button3_Click(object sender, EventArgs e)


{
Text = Text + button3.Text;
}
}
}
Concatenamos el valor actual de la propiedad Text del formulario con el valor de la
propiedad Text del botón respectivo:
private void button1_Click(object sender, EventArgs e)
{
Text = Text + button1.Text;
}

Problema 3:

Similar al problema anterior solo permitir mostrar hasta 10 caracteres en el título del
formulario.

Programa:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
if (Text.Length < 10)
{
Text = Text + button1.Text;
}
}

private void Button2_Click(object sender, EventArgs e)


{
if (Text.Length < 10)
{
Text = Text + button2.Text;
}
}

private void Button3_Click(object sender, EventArgs e)


{
if (Text.Length < 10)
{
Text = Text + button3.Text;
}
}
}
}
Como la propiedad Text es de tipo string luego podemos acceder a la propiedad Length para
conocer la cantidad de caracteres almacenados:
private void button1_Click(object sender, EventArgs e)
{
if (Text.Length < 10)
{
Text = Text + button1.Text;
}
}

Problema propuesto
1. Elaborar una interfaz gráfica que muestre una calculadora (utilizar objetos de la
clase Button y un objeto de la clase Label donde se muestra el valor ingresado),
tener en cuenta que solo se debe implementar la interfaz y la carga de un valor de
hasta 12 dígitos.

You might also like