Make buttons available dynamically

3

insert image description here I would like to know how I can insert a numeric value through a textbox, and with this value x make x y visible.

  

ex. if I type 3 in the textbox, button 1, 2 and 3 will be visible

I'm developing a form in which save in a sql datatable, the possible dates and number of times for each date (that is, if set 3, all those dates will have 3 times). In another form I would schedule the events, and wanted to have the dates of the table available and the schedules, as they were registering the events, they would appear as busy or free. Type of seat marking on airplanes.

    
asked by anonymous 04.10.2017 / 06:15

2 answers

0

Thanks for the help, with your help and research I got the desired result:

namespace teste3 {     public partial class Form1: Form     {         int Days;         int Schedules;

    private Button[][] botoes = new Button[3][];
    private GroupBox[] grupo = new GroupBox[3];

    int quantidade;

    public Form1()
    {
        InitializeComponent();
        botoes[0] = new Button[5] { btnA1, btnA2, btnA3, btnA4, btnA5 };
        botoes[1] = new Button[5] { btnB1, btnB2, btnB3, btnB4, btnB5 };
        botoes[2] = new Button[5] { btnC1, btnC2, btnC3, btnC4, btnC5 };

        grupo = new GroupBox[] { group_1, group_2, group_3 };
    }

    private void btn_Verificar_Click(object sender, EventArgs e)
    {
        Dias = int.Parse(text_Dias.Text);
        Horarios = int.Parse(text_Bancas.Text);
        EscondeBotoes();

        // Display the array elements:
        for (int i = 0; i < Dias; i++)
        {
            grupo[i].Visible = true;

            for (int j = 0; j < Horarios; j++)
            {
                botoes[i][j].Visible = true;
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void EscondeBotoes()
    {
        for (int i = 0; i < 3; i++)
        {
            grupo[i].Visible = false;

            for (int j = 0; j < 5; j++)
            {
                botoes[i][j].Visible = false;
            }
        }            
    }
}

}

    
07.10.2017 / 03:29
1

If you want to show x TextBox , depending on a value, you can create an array to save them and assign the correct Visible to each through a loop. This way it gets a bit more flexible and easy to expand into more elements.

Example:

public partial class Form1 : Form
{
    private TextBox[] botoes; //array de textboxes

    public Form1()
    {
        InitializeComponent();

        //colocar no array as textboxes que quer e pela ordem certa
        botoes = new TextBox[] { textBox1, textBox2, textBox3 };
    }

    //este método mostra a quantidade de textboxes correspondente ao parametro passado
    private void Mostrar(int quantidade)
    {
        for (int i = 0; i < botoes.Length; ++i)
        {
            botoes[i].Visible = (i < quantidade) ? true : false;
        }
    }

So for what I had said

  

If I type 3 in the textbox, button 1, 2 and 3 will be visible

Just call the method with this value: Mostrar(3); .

Note that the same logic applies to Button or any other form element that works.

    
04.10.2017 / 12:10