C#中的复选框没有出现

时间:2013-04-29 20:57:58

标签: c# checkbox

我遇到以下代码问题...我希望在我的C#程序中创建一个新表单。我已经以编程方式创建了一个新表单,其中包含一个组合框,一个复选框列表和一个允许表单将所需信息返回给调用例程的按钮。一切都按照需要工作,除了我无法按照我想要的方式排列Checkboxes。我希望在表单底部附近有一行水平的复选框。我已经以编程方式创建了复选框,但我无法获得它们的水平线。我可以得到一条对角线(从左上角到右下角)或只有第一个复选框。

我知道其他复选框在那里,因为我打印出的信息表明所有复选框都在那里,但是我没有看到它们。正如您从下面的for循环中看到的那样(一个索引为i),我尝试使用BringtoFront(),Update()和Refresh()方法。该循环是将复选框放在表单上的主循环。你可以看到我有我试过的代码行和发生的结果。取消注释的是我想要的那个,但屏幕上只显示第一个复选框。

    internal System.Windows.Forms.ComboBox ComboBox12;
    string theitem;
    Form prompt2;
    private Button getSelectedRB2;
    private Button thebutton;
    private CheckBox[] _checkBoxes;
    CheckBox checkBox15;
    int numberOcheckboxes = 40;



    // initialize the combo box 
    private void InitializeComboBox()
    {
        this.ComboBox12 = new System.Windows.Forms.ComboBox();
        string[] employees = new string[]{"Choose One", "Normal",
            "Setup", "Special-Skip 1", "Special-Skip 2", "Special-All Blades" };

        ComboBox12.Items.AddRange(employees);
        this.ComboBox12.Location = new System.Drawing.Point(136, 32);
        this.ComboBox12.IntegralHeight = false;
        this.ComboBox12.MaxDropDownItems = 20;
        this.ComboBox12.DropDownStyle = ComboBoxStyle.DropDownList;
        this.ComboBox12.Name = "ComboBox12";
        //size of combobox
        this.ComboBox12.Size = new System.Drawing.Size(136, 81);

        this.ComboBox12.TabIndex = 0;
        this.Controls.Add(this.ComboBox12);

        // Associate the event-handling method with the  
        // SelectedIndexChanged event. 
        this.ComboBox12.SelectedIndexChanged +=
            new System.EventHandler(ComboBox12_SelectedIndexChanged);

        //Create button  
        Button confirmation1 = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
        confirmation1.Click += (sender, e) => { prompt2.Close(); };
        confirmation1.Click += new EventHandler(okClicked);
        this.prompt2.Controls.Add(confirmation1);


        prompt2.Controls.Add(this.ComboBox12);

        _checkBoxes = new CheckBox[1000];

        for (int i = 0; i < numberOcheckboxes; i++)
        {
            _checkBoxes[i] = new CheckBox();

            //diagonal – works correctly
            //_checkBoxes[i].Location = new Point(20 * i, 20 * i);

            //left to right? - only first one appeared
            //_checkBoxes[i].Location = new Point(20 * i, 200);

            //left to right? - get just checkbox 0 even though they are all there
            _checkBoxes[i].Left = i * 30; 
            _checkBoxes[i].Top = 500;

            //top to bottom?  Worked correctly
            //_checkBoxes[i].Left = 50;
            //_checkBoxes[i].Top = i * 20;



            _checkBoxes[i].Text = i.ToString();
            _checkBoxes[i].Enabled = true;
            _checkBoxes[i].Checked = true;
            _checkBoxes[i].BringToFront();

            _checkBoxes[i].Update();
            this.prompt2.Refresh();


            _checkBoxes[i].CheckedChanged += new EventHandler(ShowCheckedCheckboxes);
            this.prompt2.Controls.Add(_checkBoxes[i]);
        }
    }

    //gets values when combobox changes...
    void ComboBox12_SelectedIndexChanged(object sender, EventArgs e)
    {
        //works except that it has an error at the end
        System.Windows.Forms.ComboBox theobject;
        theobject = (System.Windows.Forms.ComboBox)sender;
        theitem = (string)theobject.SelectedItem;
        MessageBox.Show(theitem);

        MessageBox.Show("Making everything Disabled and Unchecked so everything is set to default");
        for (int i = 0; i < numberOcheckboxes; i++)
        {
            _checkBoxes[i].Enabled = false;
            _checkBoxes[i].Checked = false;
        }
    }

    private ComboBox combobox10;

    //Combobox on a form?
    public string Select_Blades_InitializeCombobox()
    {
        prompt2 = new Form();
        prompt2.Width = 1000;
        prompt2.Height = 600;
        InitializeComboBox();

        prompt2.ShowDialog();

        return (theitem);
    } 


    void ShowCheckedCheckboxes(object sender, EventArgs e)
    {

    }


    private void CheckBoxCheckedChanged(object sender, EventArgs e)
    {
        MessageBox.Show("CheckBoxCheckedChanged");
    }


    private void okClicked(object sender, EventArgs e)
    {
        MessageBox.Show("okClicked");
    }

正如我之前所说,这是用C#编写的。我有一台Windows XP机器,我正在使用Visual Studio 2008.我正在创建一个Windows应用程序。

总而言之,一切都出现在我的表单上(上面称为prompt2),除了看起来不喜欢在水平线上的复选框。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

要获得水平线,您必须保持框的y点相同,然后根据前一个框的宽度和用于分隔框的间隔值来增加点的x值

设置位置是正确的,但您也可能需要设置宽度。

此外,不要制作1000个复选框的数组,而只使用40,只需创建一个临时变量,将其添加到控件中

这对我有用

    for (int i = 0; i < 40; i++)
    {
        CheckBox c = new CheckBox();
        c.Location = new Point(20 * i, 20);
        c.Width = 20;
        c.Text = i.ToString();
        c.Click += c_Click;
        this.Controls.Add(c);
    }

修改 共享点击事件

void c_Click(object sender, EventArgs e)
{
    CheckBox c = sender as CheckBox;
    if (c.Checked)
    {
        //dostuff
    }
}