Resolved How to reference ComBox inside of GroupBox control

Joined
Sep 14, 2020
Messages
11
Programming Experience
5-10
Hello group,

I am trying to display combobox text value using a groupbox reference.

CmbR1 is inside of my groupbox named GroupBox6, when I try to run next piece of code I am getting a Reference null error:

1600444333817.png


My piece of code is as follow:
C#:
GroupBox GroupBox6 = new System.Windows.Forms.GroupBox();

                        for (int i = 1; i <= 9; i++)
                        {

                            MessageBox.Show(GroupBox6.Controls["CmbR" + i].Text);

                        }
I have a group of comboboxes from CmbR1 to CmbR9 inside of GroupBox6.

Any help, will be really appreciated.

Kind regards,

Francisco Mtz.
 

Attachments

  • 1600444297505.png
    1600444297505.png
    268.1 KB · Views: 30
  • 1600444458918.png
    1600444458918.png
    1.1 KB · Views: 30
Last edited by a moderator:
Hi group,

This is another test that I am doing:

int i = 1;
MessageBox.Show(Convert.ToString(i)); --> Shows 1
MessageBox.Show(CmbR1.Text); --> Show Blank space which is the value of CmbR1.Text
MessageBox.Show(GroupBox6.Controls["CmbR" + Convert.ToString(i)].Text); --> I am getting error in null reference

I have a similar code in Visual Basic, and it is working as expected:

VB.NET:
For i = 1 To 14
            If GroupBox6.Controls("CmbR" & CInt(i)).Text = GroupBox1.Controls("txbCom11" & CInt(i)).Text Then
                contador_aciertos_1 = contador_aciertos_1 + 1
                GroupBox1.Controls("txbCom11" & CInt(i)).BackColor = Color.Sienna
            Else
                GroupBox1.Controls("txbCom11" & CInt(i)).BackColor = Color.NavajoWhite
            End If
Next
Thanks in advance.

Kind regards,

Francisco Mtz.
 
Last edited by a moderator:
On your VB code, show us how you got GroupBox6.

In C# code in post #1, you are getting a null exception error because your GroupBox6 is a newly constructed control on line #1. It obvously won't have any child controls. Why would you expect it to if it is brand new and never been populated?
 
Please take the crash course in my signature. There is a getting started section linked under my spoiler. You need that before you go any further.
 
On your VB code, show us how you got GroupBox6.

In C# code in post #1, you are getting a null exception error because your GroupBox6 is a newly constructed control on line #1. It obvously won't have any child controls. Why would you expect it to if it is brand new and never been populated?

Thank you Skydiver, I really appreciate your response.

Is there any way to get combox text value inside of this container (GroupBox6)?

In Visual Basic, I just have this code:

If GroupBox6.Controls("CmbR" & CInt(i)).Text = GroupBox1.Controls("txbCom11" & CInt(i)).Text Then
contador_aciertos_1 = contador_aciertos_1 + 1
GroupBox1.Controls("txbCom11" & CInt(i)).BackColor = Color.Sienna
Else
GroupBox1.Controls("txbCom11" & CInt(i)).BackColor = Color.NavajoWhite
End If

I am just a newbie in C# trying to get Visual C# code working at the same way than Visual Basic.

Thanks in advance.

Kind regards,

Francisco Mtz.
 
What's the name you assigned to the group box that contains CmbR1 in your WinForms implementation? It should work exactly the same way. Post the code found in of your Form1.Designer.cs file.
 
Hi guys,

I am uploading Designer code file.

How can I use code tags in my posts?

Thanks in advance.

Kind regards,

Francisco Mtz.
 

Attachments

  • 2Fijos_7Dobles_Designer.zip
    38.3 KB · Views: 19
I can see this in my designer file:

C#:
this.GroupBox6.Controls.Add(this.CmbR1);

So the definition for this ComboBox exists inside of GroupBox6.

Kind regards,

Francisco Mtz.
 
Yes, it exists in the form's instance of the GroupBox that was instantiated on line 610:
Screenshot_1.png


But in your code in post #1, you are creating a new instance of a GroupBox on line #1, and then trying to search that new instance. You should be searching the instance that is on form.

So to fix the code in post #1, just delete line #1 (assuming that the rest of the code in post #1 lives in the form).
 
Yes, it exists in the form's instance of the GroupBox that was instantiated on line 610:
View attachment 1099

But in your code in post #1, you are creating a new instance of a GroupBox on line #1, and then trying to search that new instance. You should be searching the instance that is on form.

So to fix the code in post #1, just delete line #1 (assuming that the rest of the code in post #1 lives in the form).

Hi Skydiver,

I really appreciate your help.

It is working now, I just deleted the new reference and the error is not more there.

I will continue validating this form.

Thanks in advance.

Kind regards,

Francisco Mtz.
 
Glad you got it sorted. Always be careful when working with object references, especially those defined locally and those redefined with the new keyword within methods etc. I will mark this one resolved for you.
 
As a quick aside, when you have 687 controls on a form, you might want to seriously consider implementing user controls, or revisit your UI design.
 
Back
Top Bottom