Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with 15 ComboBoxes in a column. Called cbo_T_Project_01 to 15
Each ComboBox has a Label to the right of it. Called lbl_T_Init_01 to 15

When the user selects a value from the Combobox I need to update the text of the Label adjacent to it.

In my endeavour not to write reams of code I am attempting to 'build' the Control name for the applicable Label because I dont know which ComboBox the user will select.






VB
Private Sub cbo_Project_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_T_Project_01.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_02.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_03.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_04.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_05.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_06.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_07.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_08.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_09.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_10.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_11.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_12.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_13.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_14.SelectedIndexChanged,
                                                                                                                     cbo_T_Project_15.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_01.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_02.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_03.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_04.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_05.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_06.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_07.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_08.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_09.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_10.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_11.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_12.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_13.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_14.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_15.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_16.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_17.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_18.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_19.SelectedIndexChanged,
                                                                                                                     cbo_O_Project_20.SelectedIndexChanged

        Dim ProjCtrl As Control = Me.ActiveControl  
     
        Dim InitCtrl As Label 
        InitCtrl.Name = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)

        If Me.ActiveControl.Text = "xxxxxx" Then
            InitCtrl.Text = "Primary Project"
        Else
            InitCtrl.Text = "Secondary Project"
        End If

    End Sub    


Clearly I am going wrong at the Dim InitCtrl stage.
Is it even possible to associate the Label with the applicable ComboBox?

Could anyone please assist. Thanks in advance
Posted
Comments
nilesh sawardekar 15-Jul-14 5:08am    
Why not your create control dynamically.?

1 solution

Just to stick to your example as more as possible...

Your code:
VB
Dim InitCtrl As Label
InitCtrl.Name = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)


Should change to something like:
VB
Dim sInitCtrlName As String
sInitCtrlName = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)
Dim InitCtrl As Label = Me.Controls(sInitCtrlName) 


There are even other approaches, like:
- Finding the label by its location, if each label is always at the same side and distance from its ComboBox mate.
- If the Label and ComboBox controls are layed inside a TableLayoutPanel (with 2 columns and as many rows as the Label+ComboBox pairs are), you can use GetPositionFromControl and GetControlFromPosition methods to find the mate control from any other control.
- Having a custom ComboBox-derived class with a dedicated property of type Label to assign at design time to the correct Label control, though forgetting about the names.
- Assigning the Label control as Tag property of the ComboBox at initialization time.
- Assigning at initialization time the same value to the property Tag of both the Label and ComboBox and using it to find the matching label looping Me.Controls colletion.
- Creating a composite UserControl containing both a Label and a ComboBox , handling all events itself and exposing the internal properties, then replacing each existing pair of Label+ComboBox with one instance of this control.
- As suggested by nilesh sawardekar, create and add the controls at run-time and during this process link them in some way.

Regards,
Daniele.
 
Share this answer
 
v3
Comments
Darrell de Wet 15-Jul-14 7:54am    
Hi Daniele. Thanks for your response and suggestions which look good.

The Tag property is probably what I am going to go with but before that :
Your code change almost works, just that when I try to execute the
InitCtrl.Text = "Primary Project" statement
I get 'Object reference not set to an instance of an object'

Any suggestions on how to fix that.

Your assistance is much appreciated.
Daniele Rota Nodari 15-Jul-14 8:01am    
That should mean that Me.Controls(sInitCtrlName) returned Nothing
This might be due to Me not being the parent of the label control or sInitCtrlName containing a wrong name.
You should make sure that sInitCtrlName contains the right full name of the label control and which container is the parent of the label control (being it Me or a sub control like a Panel).

Just to be more clear, if the label control is layed inside a sub control of Me (e.g.: "panel1"), you should replace "Me.Controls" with "panel1.Controls". The easiest ways you can determine what is the right control are through the designer window or the "document layout" window.

As an alternative to Me.Controls(sInitCtrlName) you can also use Me.Controls.Find(sInitCtrlName, true), but this will return an array of Control objects, not only one.

More, as a general rule because InitCtrl is obtained dynamically, before using it you should always check for a valid value.

For further details you can look at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28v=vs.100%29.aspx and http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection%28v=vs.100%29.aspx

Regards,
Daniele.
Darrell de Wet 15-Jul-14 8:13am    
Wonderful - the label was in a GroupBox. I changed Me. to groupbox name and it worked like a dream.

I thank you greatly for taking the time to teach me something !!
Daniele Rota Nodari 15-Jul-14 8:16am    
I'm glad to have been helpful. :)

Daniele.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900