Tacit
Tacit

Reputation: 908

Disabling a dynamic button

Hi I have a small winforms program that will soon develop into something more. The program has 2 panels panel1 and panel2 these panels are populated dynamically with some form controls. the first panel is populated with combo-boxes and the second with a grid of buttons. What I want to achieve is to be able to disable the right button depending on what the user selects from the combobox. Each column of the grid represent a day of the week and the combobox will be used to disable the wanted day by selecting it from the list if you like.

To do this statically is straight forward, however my program will soon expand so that it can handle a large database so that's why I am doing this dynamically. Basically this is where I'm stuck at the moment I want to simply disable the right button.

Below is the interface that i have so far: enter image description here

And this is my code if any help:

 public Form1()
        {
            InitializeComponent();
        }
        Button[] btn = new Button[2];
        ComboBox[] cmb = new ComboBox[1];

        private void Form1_Load(object sender, EventArgs e)
        {
            placeRows();
        }

        public void createColumns(int s)
        {
            for (int i = 0; i < btn.Length; ++i)
            {
                btn[i] = new Button();
                btn[i].SetBounds(40 * i, s, 35, 35);
                btn[i].Text = Convert.ToString(i);

                panel1.Controls.Add(btn[i]);

            }

            for (int i = 0; i < cmb.Length; ++i)
            {
                cmb[i] = new ComboBox();
                cmb[i].SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
                cmb[i].Text = "Disable";
                cmb[i].Items.Add("Monday");
                cmb[i].Items.Add("Tuesday");
                cmb[i].SetBounds(40 * i, s, 70, 70);
                panel2.Controls.Add(cmb[i]);
            }

        }

        void cmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox senderCmb = (ComboBox)sender;

            if (senderCmb.SelectedIndex == 1)
            {
                //MessageBox.Show("Tuesday");
                btn[1].Enabled = false;
            }
        }

        public void placeRows()
        {
            for (int i = 0; i < 80; i = i + 40)
            {
                createColumns(i);               
            }
        }
    }

Upvotes: 0

Views: 1482

Answers (1)

Eric J.
Eric J.

Reputation: 150108

Alternative 1

Every control has a Tag property.

You can set the Tag property of your buttons to represent the column they are in.

When a selection is made in the combo box, simply search through all buttons, and enable or disable the button based on whether each button's Tag property matches the selected text in the combo box.

Alternative 2

Create a

Dictionary<string, List<Button>> buttonMap;

where the key is the value representing the column ("Tuesday") and the value is a list of buttons with that tag. When creating the buttons initially, also populate that dictionary.

If you go with Alternative 2, you'll have to remember the previously selected value of the checkbox so you can re-enable buttons that are no longer disabled.

If you have lots of buttons, you may find that Alternative 2 is noticeably faster.

UPDATE

Here's a complete working sample of Alternative 1.

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

    const int ROWS = 2;
    const int COLS = 2;

    Button[,] btn = new Button[ROWS,COLS];
    ComboBox[] cmb = new ComboBox[ROWS];

    private void Form1_Load(object sender, EventArgs e)
    {
        placeRows();
    }

    private readonly string[] cbTexts = new string[] { "Monday", "Tuesday" };

    public void createColumns(int rowIndex)
    {
        int s = rowIndex * 40;

        // Your original code kept overwriting btn[i] for each column.  You need a 2-D array
        // indexed by the row and column
        for (int colIndex = 0; colIndex < COLS; colIndex++)
        {
            btn[rowIndex, colIndex] = new Button();
            btn[rowIndex, colIndex].SetBounds(40 * colIndex, s, 35, 35);
            btn[rowIndex, colIndex].Text = Convert.ToString(colIndex);
            btn[rowIndex, colIndex].Tag = cbTexts[colIndex];

            panel1.Controls.Add(btn[rowIndex, colIndex]);

        }

        cmb[rowIndex] = new ComboBox();
        cmb[rowIndex].SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
        cmb[rowIndex].Text = "Disable";
        foreach (string cbText in cbTexts)
        {
            cmb[rowIndex].Items.Add(cbText);
        }
        cmb[rowIndex].SetBounds(40, s, 70, 70);
        cmb[rowIndex].Tag = rowIndex; // Store the row index so we know which buttons to affect
        panel2.Controls.Add(cmb[rowIndex]);

    }

    void cmb_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox senderCmb = (ComboBox)sender;

        int row = (int)senderCmb.Tag;
        for (int col = 0; col < COLS; col++)
        {
            Button b = btn[row, col];
            // These three lines can be combined to one.  I broke it out
            // just to highlight what is happening.
            string text = ((string)b.Tag);
            bool match =  text == senderCmb.SelectedItem.ToString();
            b.Enabled = match;
        }
    }

    public void placeRows()
    {
        for (int rowIndex = 0; rowIndex < 2; rowIndex++)
        {
            createColumns(rowIndex);
        }
    }
}

Upvotes: 3

Related Questions