-1

How to get value of checkboxes (and the textbox upon change in text) in real time with form particulars that are all generated via code?

This following code produces a form upon button press, the form has checkboxes and a richtextbox. Ideally I want any interaction to have an effect, so if I paste in a grid of ones and zeros the checkboxes update, and once a checkbox gets click, the corresponding one or zero in the textarea will update (So that I can then copy the grid (matrix) out and into another program.

I know how to get the events using the visual studio GUI maker, but not from a programmatically created form like this.

    private void begin_button_Click(object sender, EventArgs e)
    {
        // Build the child form
        Form check_box = new Form();
        check_box.FormBorderStyle = FormBorderStyle.FixedSingle;

        // Get the values from the textboxes
        int height = Convert.ToInt16(this.height_input.Text);
        int width = Convert.ToInt16(this.width_input.Text);

        // Set the dimensions of the form
        check_box.Width = width * 15 + 40;
        check_box.Height = ( height * 15 + 40 ) * 3;

        // Build checkboxes for the checkbox form
        CheckBox[] chk;
        chk = new CheckBox[height * width];

        int count = 0;
        for (int i = 1; i <= height; i++)
        {
            for (int j = 1; j <= width; j++)
            {
                chk[count] = new CheckBox();
                chk[count].Name = count.ToString();
                chk[count].Width = 15; // because the default is 100px for text
                chk[count].Height = 15;
                chk[count].Location = new Point(j * 15, i * 15);
                chk[count].CheckedChanged += new EventHandler(this.CheckedChanged);
                check_box.Controls.Add(chk[count]);
                count += 1;
                //Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
            }
        }

        RichTextBox output_area;
        output_area = new RichTextBox();
        output_area.Location = new Point(chk[0].Location.X, chk[count-1].Location.Y + 30);
        check_box.Controls.Add(output_area);
        output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
        output_area.Width = check_box.Width - 40;
        output_area.Height = check_box.Height / 2;

        // Run the form
        check_box.ShowDialog();
    }

EDIT:

I have added the event handler and it's working, however I can't access the checkbox form, only the main form.

    private void CheckedChanged(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        CheckBox x = (CheckBox)sender;
        Console.WriteLine(x);
        Console.WriteLine(x.Name.ToString());
    }
6
  • possible duplicate of Dynamically (programatically) adding check boxes and checkedchanged events + tons of other results from google... Commented Jul 13, 2014 at 17:19
  • @BartoszKP, that link suggests that I have a loop that checks each checkbox continuously? I'm not even sure where I would put it, the code seems to pause at the check_box.ShowDialog(); line. Commented Jul 13, 2014 at 17:23
  • No it doesn't. It shows you how to programmatically assign an event to a generated CheckBox. Commented Jul 13, 2014 at 17:27
  • Ok, so I have the event handler working, however I can't access the generated form, I can't access check_box at all. Commented Jul 13, 2014 at 17:42
  • Please don't modify your post as you go along. This is not a helpdesk service, and more importantly - this is not about you. SO is a place to collect general questions&answers useful to everyone, not for detailed hand guiding for beginners. Please use the vast amount of information available on the internet - in this case .NET tutorials and MSDN documentation. Starting tip for you: read about properties your objects expose for you: CheckBox.Parent. Commented Jul 13, 2014 at 17:57

2 Answers 2

2

Have a look at the .Designer file that the form designer generates for you!

Anyway, in your loop, try something like this:

chk[count].CheckedChanged += MyFancyHandler;

And the handler itself will look just like a normal handler:

private void MyFancyHandler( object sender, EventArgs e )
{
    // ...
}

Also notice that the sender argument there will contain a reference to whichever checkbox/control the event refers to.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure that I have a .Designer file for the checkbox form, because the entire thing is generated during runtime. I have no idea how to access the handler's for the checkboxes.
Maybe what I should have written was "create some form, add a few checkboxes and hook up some events", and then have a look at the designer file! Just to see how it does it!
Ah I see, see how it works normally so that I can replicate it for my situation?
1

Below code updates matrix text in the rich text box when check box check state changed.

    RichTextBox output_area;
    CheckBox[] chk;
    Size MatrixSize;
    private void begin_button_Click()
    {
        // Build the child form
        Form check_box = new Form();
        check_box.FormBorderStyle = FormBorderStyle.FixedSingle;
        check_box.StartPosition = FormStartPosition.CenterScreen;

        // Get the values from the textboxes
        int height = Convert.ToInt16("10");
        int width = Convert.ToInt16("7");

        MatrixSize = new Size(width, height);

        // Set the dimensions of the form
        check_box.Width = width * 15 + 40;
        check_box.Height = (height * 15 + 40) * 3;

        // Build checkboxes for the checkbox form

        chk = new CheckBox[height * width];

        int count = 0;
        for (int i = 1; i <= height; i++)
        {
            for (int j = 1; j <= width; j++)
            {
                chk[count] = new CheckBox();
                chk[count].Name = count.ToString();
                chk[count].Width = 15; // because the default is 100px for text
                chk[count].Height = 15;
                chk[count].Location = new Point(j * 15, i * 15);
                check_box.Controls.Add(chk[count]);
                chk[count].CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);


                count += 1;
                //Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
            }
        }


        output_area = new RichTextBox();
        output_area.Location = new Point(chk[0].Location.X, chk[count - 1].Location.Y + 30);
        check_box.Controls.Add(output_area);
        output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
        output_area.Width = check_box.Width - 40;
        output_area.Height = check_box.Height / 2;

        // Run the form
        check_box.ShowDialog();
    }


    private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
    {
        CheckBox c = (CheckBox)sender;
        Debug.WriteLine(c.Name);

        StringBuilder sb = new StringBuilder();

        int count = 0;
        for (int i = 1; i <= MatrixSize.Height; i++)
        {
            for (int j = 1; j <= MatrixSize.Width; j++)
            {
                if (chk[count].Checked)
                {
                    sb.Append("1,");
                }
                else
                {
                    sb.Append("0,");
                }
                count += 1;
            }
            sb.Append("\r\n");
        }

        output_area.Text = sb.ToString(); 

    }

1 Comment

Thanks! Inside the event handler I added char token = ' '; and then sb[sb.Length-1] = token; at the end of the inner for loop so that I could delimit the end of each line (It's a matrix I'm using in matlab). Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.