-1

I have some code that creates a few combo boxes on the click of a button. Something like this.

for (int i =0; i< noCol;i++){
tableLayoutPanel2.RowCount = tableLayoutPanel2.RowCount + 1;
tableLayoutPanel2.Controls.Add(new Label() { Text = "العمود " + (i+1) }, 0, (i));
var com = new ComboBox();
com.Name = "reportColumn" + (i + 1).ToString();
g = tableLayoutPanel2.Controls[com.Name] as ComboBox;
c.Add(g);
g.SelectedIndexChanged += new EventHandler(ReportWizardStep1ComboboxSelectedIndexchanged);
tableLayoutPanel2.Controls.Add(com,1,i);}

private void ReportWizardStep1ComboboxSelectedIndexchanged(object sender, EventArgs e){ComboBox combo = sender as ComboBox;
MainForm.exportReport.getDataGridView1().ColumnCount++;
MainForm.exportReport.getDataGridView1().Columns
            [MainForm.exportReport.getDataGridView1().ColumnCount - 1].HeaderText = g.Text;
comboValues.Add(g.Text);}

but, when I run the code, an exception is thrown for this line:

g.SelectedIndexChanged += new EventHandler(ReportWizardStep1ComboboxSelectedIndexchanged);
tableLayoutPanel2.Controls.Add(com,1,i);

the exception is :

An exception of type 'System.NullReferenceException' occurred. how can I solve this problem

  • I'm trying to follow your logic. You create a ComboBox "com". Then reference a ComboBox of the same name to "g". Then move "g" to "c's" Control collection. Then add "com" to the TableLayoutPanel. So are you trying to replace a ComboBox with a ComboBox? What are you trying to do that makes you jump through these hoops? – LarsTech Feb 22 '19 at 22:12
  • What is this: `c.Add(g);`? Why are you trying to set `g` to an existing child control of `tableLayoutPanel2`? Aren't you just creating this new ComboBox? The vent handler should be subscribed to by `com`, not this `g` (which is, btw, probably `null` and the whole thing doesn't make sense). – Jimi Feb 22 '19 at 22:12
  • after creating combo boxes dynamically, I'm trying to handle a "selectedIndexChanged" for everyone by using its reference name. I used this "g = tableLayoutPanel2.Controls[com.Name] as ComboBox;" to get the reference name – Burhan Barhoush Feb 22 '19 at 22:19
  • Your code example is not easy to read: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Michael Freidgeim Feb 22 '19 at 22:20
  • The *reference name* (the reference to a newly created control) is `com`. Attach the handler to this instance and add the instance to the collection. `c.Add(g);` still a mystery – Jimi Feb 22 '19 at 22:21
  • `MainForm` looks like a dangerous reference. It might not reference the form you see on the screen. Always pass a reference and use that. – LarsTech Feb 22 '19 at 22:22

1 Answers1

1
...
var com = new ComboBox();
com.Name = "reportColumn" + (i + 1).ToString();
g = tableLayoutPanel2.Controls[com.Name] as ComboBox;
c.Add(g);
g.SelectedIndexChanged += new EventHandler(ReportWizardStep1ComboboxSelectedIndexchanged);
tableLayoutPanel2.Controls.Add(com,1,i);}

you are creating a new combobox, setting a name and searching this newly created control in tableLayoutPanel2.Controls...it is expected to return you null... you should be adding it first before looking for it.

akhileshcoer
  • 162
  • 10