Dynamically provide buttons

Asked

Viewed 491 times

3

insert the description of the image here.You can find out how I can through a textbox insert a numerical value, and with this value x make visible x buttons of y.

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

I am developing a form in which save in an sql datatable, possible dates and number of times for each date (ie if set 3, all these dates will have 3 times). In another form I will schedule the events, and I wanted to make available the dates of the table and the times, as it was registering the events, they would appear as busy or free. Seat marking type on aeroplanes.

inserir a descrição da imagem aqui

2 answers

2

If you want to show x TextBox, depending on a value, you can create an array to store and assign the Visible correct for each one through a loop/loop. This way it becomes a little more flexible and easy to expand for 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, the button 1, 2 and 3 will be visible

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

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

  • Wouldn’t you be able to make an array of buttons to enable them and then change the text of each and the value q it passes when clicked? I will use the buttons as the seats of the seat markers of the cia areas

  • @i_melo The button array would be exactly the same as the textboxes. Change the value when you also click perfectly. Now I’m not seeing exactly what you’re trying to do about the seats and that.

  • I have a db in sql that saves several dates for monograph defense scheduling. A form will check these dates and for each date will inform me the vacant times. So I can schedule in another db these stalls. Hence the buttons. I created groupbox with 5 buttons for each available date q are invisible by default. If there are 2 dates, it releases only 2 groupbox. The buttons will be the available times, already scheduled times will display a code on the button to warn q it is unavailable. When selecting an available button it fills a textbox with the time to save in db.

  • @i_melo Print out what you have so it’s visually easier to follow. But if I have understood correctly, it is best to have a list of elements of a class made by you that indicate whether a certain time is available or not. Then through a loop/loop they hide or show in the textboxes array. If you already have classes for this you should also specify.

  • This is the form. It will fetch the data from a db, hide the groupbox. For every available day it releases a groupbox. In the same db is the number of times per day. Then only the number of times specified for each day will be shown. By clicking the button it saves in another db, all recovered data from the other db, and adds date and time. When you open this form again that day and at that time the corresponding button will be labeled with the presentation code. Then I will know that that time is unavailable. I will have to choose another time via another button.

  • @i_melo Enter the code you are using to find available and unavailable times, which is where you need to enter the code I gave in the answer.

Show 1 more comment

0


Thanks for the help, with your help and searches I got the desired result: inserir a descrição da imagem aqui

namespace teste3 { public partial class Form1 : Form { int Dias; int Horarios;

    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;
            }
        }            
    }
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.