禁用动态按钮

时间:2013-02-05 17:49:12

标签: c# arrays winforms

嗨,我有一个很小的winforms程序,很快就会发展成更多东西。该程序有2个面板panel1panel2这些面板通过一些表单控件动态填充。第一个面板用组合框填充,第二个面板用按钮网格填充。我想要实现的是能够根据用户从combobox中选择的内容禁用右键。网格的每一列代表一周中的某一天,如果您愿意,可以使用combobox来禁用所需日期。

要静态地执行此操作是直截了当的,但是我的程序将很快扩展,以便它可以处理大型数据库,这就是我动态执行此操作的原因。基本上这就是我想要简单地禁用右键的那一刻。

以下是我到目前为止的界面: enter image description here

如果有任何帮助,这是我的代码:

 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 个答案:

答案 0 :(得分:3)

备选方案1

每个控件都有Tag属性。

您可以设置按钮的Tag属性以表示它们所在的列。

当在组合框中进行选择时,只需搜索所有按钮,并根据每个按钮的Tag属性是否与组合中的选定文本匹配来启用或禁用按钮框。

备选方案2

创建

Dictionary<string, List<Button>> buttonMap;

其中键是表示列的值(“星期二”),值是带有该标记的按钮列表。最初创建按钮时,也会填充该字典。

如果您使用备选方案2,则必须记住之前选中的复选框值,以便重新启用不再禁用的按钮。

如果您有很多按钮,您可能会发现备选方案2明显更快。

<强>更新

以下是备选方案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);
        }
    }
}