1

我有一个应用程序需要一组可能从 1 到 1000
的问题。这些问题由用户设置,我需要 groupbox 包含下面所示的 2 个单选按钮。该代码确实创建了多个包含 2 个单选按钮的组合框。

此代码处于一个循环中,由需要多少个问题决定。

问题是,当在任何组框中单击单选按钮时,它会删除以前单击过的组框的单击。

我该如何解决这个问题?

                GroupBox grpAnswerType = new GroupBox();                                                         // new groupbox
                if (intZ < 9) 
                {
                    grpAnswerType.Name = "grpAnswerType00" + strQNumber;
                }
                if (intZ >= 10 & intZ <= 99)                                                  // intZ is the counter in the loop
                {
                    grpAnswerType.Name = "grpAnswerType0" + strQNumber;                       // name is used later
                }
                if (intZ >= 100 & intZ <= 999)
                {
                    grpAnswerType.Name = "grpAnswerType" + strQNumber;
                }

                grpAnswerType.Location = new Point(290, intR + 20);
                grpAnswerType.Size = new Size(150, 45);
                grpAnswerType.ForeColor = System.Drawing.Color.Red;
                grpAnswerType.BackColor = SystemColors.Control;
                grpAnswerType.Font = font; 
                grpAnswerType.Text = "Choose answer type ";
                this.Controls.Add(grpAnswerType);
                grpAnswerType.Show();
                clsGlobals.gGroupBoxRadioButton3[intZ] = grpAnswerType;                 // add to array for later storage to database  
                pnlQ11.Controls.Add(grpAnswerType);                                     // add to the dynamic panel on the form




                RadioButton rbtnA1 = new RadioButton();                                                                   // Radio Button1  
                if (intZ < 9)
                {
                    rbtnA1.Name = "rbtnA100" + strQNumber;
                }
                if (intZ >= 10 & intZ <= 99)
                {
                    rbtnA1.Name = "rbtnA10" + strQNumber;
                }
                if (intZ >= 100 & intZ <= 999)
                {
                    rbtnA1.Name = "rbtnA1" + strQNumber;
                }

                rbtnA1.Location = new Point(295, intR + 38);
                rbtnA1.Size = new Size(60, 25);
                rbtnA1.Text = "One"; 

                rbtnA1.Font = font;
                rbtnA1.ForeColor = System.Drawing.Color.Blue;
                rbtnA1.BackColor = SystemColors.Control;
                grpAnswerType.Controls.Add(rbtnA1);
                pnlQ11.Controls.Add(rbtnA1);                                                       // if this is not commented, it appears on the panel, if not it does not
                rbtnA1.Show();
                clsGlobals.gRadioButtonOne[intZ] = rbtnA1;
                rbtnA1.BringToFront();

                RadioButton rbtnA2 = new RadioButton();                                                                   // Radio Button 2  
                if (intZ < 9)
                {
                    rbtnA2.Name = "rbtnA200" + strQNumber;
                }
                if (intZ >= 10 & intZ <= 99)
                {
                    rbtnA2.Name = "rbtnA20" + strQNumber;
                }
                if (intZ >= 100 & intZ <= 999)
                {
                    rbtnA2.Name = "rbtnA2" + strQNumber;
                }
                rbtnA2.Location = new Point(355, intR + 38);
                rbtnA2.Size = new Size(70, 25);
                rbtnA2.Text = "All"; ;

                rbtnA2.Font = font;
                rbtnA2.ForeColor = System.Drawing.Color.Blue;
                rbtnA2.BackColor = SystemColors.Control;
                grpAnswerType.Controls.Add(rbtnA2);
                pnlQ11.Controls.Add(rbtnA2);                                                        // if this is not commented, it appears on the panel, if not it does not                    


                rbtnA2.Show();
                clsGlobals.gRadioButtonAll[intZ] = rbtnA2;
                rbtnA2.BringToFront();     
4

1 回答 1

0

我认为这是因为pnlQ11是另一个UserControl(我猜你正在使用 Windows 窗体),并且根据 MSDN,一个控件只能分配给一个Control.ControlCollectionhttp://msdn.microsoft.com/en-us/library/system .windows.forms.control.controlcollection.add%28v=vs.100%29.aspx)。

因此,您RadioButton从 中删除GroupBox并添加到pnlQ11,这是所有其他RadioButtons 所属的同一控件。然后,解决方案是避免RadioButtonGroupBox

于 2012-11-12T23:29:48.557 回答