Jim
Jim

Reputation: 1066

Checkboxes in C# are not appearing

I am having problems with the following code… I want to have a new form created in my C# program. I have created programmatically a new form that has a combobox, a list of Checkboxes and a button that will allow the form to return the needed information back to the calling routine. Everything works as needed except that I cannot get the Checkboxes to line up in the way I want. I want a horizontal line of Checkboxes down near the bottom of the form. I have programmatically created the Checkboxes but I cannot get a Horizontal line of them. I can get a diagonal line (top left to bottom right) or only the first checkbox.

I know that the other checkboxes are there because I print out information that indicates all the checkboxes are there however I do not see them. As you can see from the for loop down below (one with the index i) I tried using the BringtoFront() , Update() and Refresh() methods. That loop is the main loop for putting checkboxes on the form. You can see that I have lines of code that I had tried and the result that happened. The one that is uncommented is the one I want but only the first checkbox appears on the screen.

    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");
    }

As I indicated previously, this is written in C#. I have a Windows XP machine and I am using Visual Studio 2008. I am creating a Windows application.

To summarize, everything is appearing on my form (called prompt2 above) except for the Checkboxes which seem to not like to be in a horizontal line.

Thanks for your help.

Upvotes: 0

Views: 2699

Answers (1)

Sayse
Sayse

Reputation: 43300

To get a horizontal line you must keep the y point of the box the same and then increase the x value of the point based upon the width of the previous box and a spacer value for separating your boxes

Setting the location is correct, however you may need to set a width also.

Also, instead of making an array of 1000 checkboxes where you only use 40, just make a temporary variable that will get added to your controls

this worked for me

    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);
    }

Edit Shared clicked event

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

Upvotes: 1

Related Questions