Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:37:19+00:00 2026-06-18T08:37:19+00:00

Hi I have a small winforms program that will soon develop into something more.

  • 0

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);               
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:37:20+00:00Added an answer on June 18, 2026 at 8:37 am

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small program in winforms that holds 3 buttons. Thus far the
I have a very small VS2008 Winforms project that will not start. When I
I have a small WinForms app that utilizes a BackgroundWorker object to perform a
I have a small calculation system that will be installed in multiple PCs. Those
I have a small winforms app that creates a new event log source. I
I have a small C# 3.5 WinForms app I am working on that grabs
I have a small WinForms application that sends notification emails. It works fine with
I'm using c#2.0 and WinForms. I have a datagridview control, unbound, loading small amounts
I have a small .NET WinForms application, and couple of linux servers, DEV and
I've got a WinForms application that I am working on. There is one small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.