Hello Every body
I am standing on a cross road with a doubt which way is the correct way. As you all are seeing the image Groupbox and CheckBoxes all are dynamically created. What I am confused about If user select Sun Tue and Fri from XII2013SIF01 and Mon Tue Wed and Thr from XII2013SIG02 how I can pass these check boxes value to Step 3 Form on button click. In Step 3 form I need to identify days by their Group name because according to that processing will be done. What technique will be best for me please give some guidance/suggestion or advice. 1287b233fe553534c2072add00429d7b

Recommended Answers

All 4 Replies

You could use the Tag property to store an Identifying string or the GoupBox, your checkbox relates to.

@ddanbe Thank you but I am not aware of tag property can you please give some highlights on that. Well This the code by which the I generation the output shown in figure -

        GroupBox[] grpBox;
        CheckBox[] dayCheckBox;
        int grpposition = 40;
        public step2(List<string> list)
        {
            InitializeComponent();
            int x = list.Count;
            grpBox = new GroupBox[x];

            for (int i = 0; i < x; i++)
            {
                grpBox[i] = new GroupBox();
                grpBox[i] = new GroupBox();
                grpBox[i].Name = "gpBox" + Convert.ToString(i);
                grpBox[i].Text = list[i];
                grpBox[i].Location = new Point(5, grpposition);
                grpBox[i].Width = 500;
                grpBox[i].Height = 75;
                this.Controls.Add(grpBox[i]);

                dayCheckBox = new CheckBox[7];
                String[] dayName={"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
                for (int j = 0; j < 7; ++j)
                {
                    dayCheckBox[j] = new CheckBox();
                    dayCheckBox[j].Name = "radio" + Convert.ToString(j);
                    dayCheckBox[j].Text = dayName[j];
                    dayCheckBox[j].Width = 50;
                    dayCheckBox[j].Location = new System.Drawing.Point(70 * j, 25);
                    grpBox[i].Controls.Add(dayCheckBox[j]);
                    //dayCheckBox[i].CheckStateChanged += new System.EventHandler(dayCheckBoxCheckedChanged);
                }
                    grpposition += 80;
            }

Click Here to know a bit more about the Tag property.
Other example:
Recently, I had 4 textboxes on a Form that I have to validate for numeric input only.
I could have used 4 Validating event handlers. instead, I marked the Tag in each TB in the designer( can of course be done dynamically also) with a,b,c and d and used one Validating method.
Here is the code:

private void Txb_Validating(object sender, CancelEventArgs e)
        {
            TextBox TB = sender as TextBox;
            double result = 0.0;

            try
            {
                result = double.Parse(TB.Text);
            }
            catch (FormatException)
            {
                e.Cancel = true;
                errorProvider1.SetError(TB, "Entry must be numeric.");
            }
            finally //finally I use finally!
            {
                switch (TB.Tag.ToString())
                {
                    case "a": Coeff.A = result; break;
                    case "b": Coeff.B = result; break;
                    case "c": Coeff.C = result; break;
                    case "d": Coeff.D = result; break;                 
                }
            }
        }

With a dictionary you can store the value of each checked checkbox indexed by the groupbox it belongs in.

    Dictionary<string, List<string>> GBValues = new Dictionary<string, List<string>>();
    GroupBox[] grpBox;
    CheckBox[] dayCheckBox;
    int grpposition = 40;
    public step2(List<string> list)
    {
        InitializeComponent();
        int x = list.Count;
        grpBox = new GroupBox[x];
        for (int i = 0; i < x; i++)
        {
            grpBox[i] = new GroupBox();
            grpBox[i] = new GroupBox();
            grpBox[i].Name = "gpBox" + Convert.ToString(i);
            grpBox[i].Text = list[i];
            grpBox[i].Location = new Point(5, grpposition);
            grpBox[i].Width = 500;
            grpBox[i].Height = 75;
            this.Controls.Add(grpBox[i]);
            GBValues.Add(gbgrpBox[i].Name, new List<string>());
            dayCheckBox = new CheckBox[7];
            String[] dayName={"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
            for (int j = 0; j < 7; ++j)
            {
                dayCheckBox[j] = new CheckBox();
                dayCheckBox[j].Name = "radio" + Convert.ToString(j);
                dayCheckBox[j].Text = dayName[j];
                dayCheckBox[j].Width = 50;
                dayCheckBox[j].Location = new System.Drawing.Point(70 * j, 25);
                grpBox[i].Controls.Add(dayCheckBox[j]);
                dayCheckBox[i].CheckStateChanged += new System.EventHandler(dayCheckBoxCheckedChanged);
            }
            grpposition += 80;
        }
    }
    private void dayCheckBoxCheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)(sender);
        if (cb.Checked)
        {
            GBValues[cb.Parent.Name].Add(cb.Text);
        }
        else
        {
            GBValues[cb.Parent.Name].Remove(cb.Text);
        }
    }        

With this you have one structure holding the text values of each checked checkbox. One advantage of using the Dictionary class, is you can index by string value. Thus GBValues["gpBox1"][0] will return the first string stored in the list of checked checkbox text for the groupbox gpBox1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.