Click here to Skip to main content
15,666,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winforms project which changes a property (Tag) of a control (comboBox or textBox) when the value is changed. I'd like to put the change code in a method:-
private void TagControl(string CtrlName, string CtrlText, string CtrlValue )
       if (tabControlPeople.Controls[CtrlName].Tag is null)
           tabControlPeople.Controls[CtrlName].Tag = "Changed";
           if (ChangesItem.IndexOf(CtrlText)!=-1)
               j = ChangesItem.IndexOf(CtrlText);
               ChangesValue[j] = CtrlName;
               MessageBox.Show("Error correcting " + CtrlText);

This is called by:-
TagControl("comboBoxPeople4", "SecurityGroupID", comboBoxPeople4.SelectedValue.ToString());

The controls are on a tabcontrol (tabControlPeople) on a panel (panelPeople) on a form (Form1).
I get an error 'Object reference not set to an instance of an object'.

What I have tried:

Using this.Controls[......]
using panelPeople.Controls[......]
using tabControlPeople.Controls[......]
using this.panelPeople.Controls[.....]
using this.tabControlPeople.Controls[.....]
Updated 14-Nov-21 17:23pm

The easiest way to find the reason is to put a breakpoint to the line calling your method. Using debugger proceed line by line and investigate the variables and collections to see where the exception is thrown and why.

For example possible reasons are, you've misspelled the control name, the control is not in the collection you suspect etc. By investigating the collections with the debugger, it's easy to see the names and the locations of your controls.

For more info about debugging, have a look at
- First look at the debugger - Visual Studio (Windows) | Microsoft Docs[^]
- Tutorial: Debug C# code - Visual Studio (Windows) | Microsoft Docs[^]
Share this answer
ormonds 14-Nov-21 23:01pm    
Yes, I've been single stepping through it and it stops at the line
"if (tabControlPeople.Controls[CtrlName].Tag is null)"
I've looked through Solution Explorer and all control names are correct.
My suspicion is that a control on a tabcontrol on a panel on a form needs some explicit name which I can't figure out.
Wendelius 14-Nov-21 23:03pm    
Try using the debuggers ability to show the collections and their content. This will help you to see in which collections where the controls are located in,
Got it:
I needed to get the control name properly:
ComboBox ThisBox = (ComboBox) this.GetControlByName(this, CtrlName);

public Control GetControlByName(Control ParentCntl, string NameToSearch)
    if (ParentCntl.Name == NameToSearch)
        return ParentCntl;
    foreach (Control ChildCntl in ParentCntl.Controls)
        Control ResultCntl = GetControlByName(ChildCntl, NameToSearch);
        if (ResultCntl != null)
            return ResultCntl;
    return null;
Share this answer

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