36 - 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.

Button

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.
    Button
Solución
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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button0_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button0.Text;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button1.Text;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button2.Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button3.Text;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button4.Text;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button5.Text;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button6.Text;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button7.Text;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button8.Text;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length < 12)
            {
                label1.Text = label1.Text + button8.Text;
            }
        }
    }
}

Retornar