首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#中的复选框未显示

C#中的复选框未显示
EN

Stack Overflow用户
提问于 2013-04-30 04:57:58
回答 1查看 2.3K关注 0票数 0

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

我知道其他的复选框在那里,因为我打印出的信息表明所有的复选框都在那里,但我看不到它们。正如您从下面的for循环中看到的(一个索引为i的循环),我尝试使用BringtoFront()、Update()和Refresh()方法。该循环是将复选框放在窗体上的主循环。您可以看到,我已经尝试了几行代码,并得到了结果。未注释的复选框是我想要的复选框,但屏幕上只出现第一个复选框。

代码语言:javascript
复制
    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 )。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-30 05:00:52

要获得水平线,必须保持长方体的y点不变,然后根据上一个长方体的宽度和用于分隔长方体的间隔值增加该点的x值。

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

此外,不要创建一个包含1000个复选框的数组,只需创建一个将添加到控件中的临时变量即可

这对我很有效

代码语言:javascript
复制
    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);
    }

编辑共享单击事件

代码语言:javascript
复制
void c_Click(object sender, EventArgs e)
{
    CheckBox c = sender as CheckBox;
    if (c.Checked)
    {
        //dostuff
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16288103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档