我有一些代码,单击一下按钮即可创建几个组合框。像这样的东西。
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);}
但是,当我运行代码时,此行抛出异常:
g.SelectedIndexChanged += new EventHandler(ReportWizardStep1ComboboxSelectedIndexchanged);
tableLayoutPanel2.Controls.Add(com,1,i);
例外是:
发生类型'System.NullReferenceException'的异常。我该如何解决这个问题
答案 0 :(得分:1)
Sub ColorSheetFive()
Dim i As Integer
Dim m As Integer
Dim n As Integer
Dim allGreen As Boolean
For m = 2 To 26
For n = 2 To 62
allGreen = True
For i = 1 To 4
If Sheets(i).Cells(m, n).Interior.Color <> RGB(0, 255, 0) Then
allGreen = False
End If
Next i
If allGreen Then
Sheets(5).Cells(m, n).Interior.Color = RGB(0, 255, 0)
End If
Next n
Next m
MsgBox "Color checking complete!"
End Sub
您正在创建一个新的组合框,设置一个名称,然后在tableLayoutPanel2.Controls中搜索此新创建的控件。该控件可能会返回null ...您应该在查找它之前先添加它。