I'm dynamicly creating listboxes in my program and i want to do something with them in another method but it gives me the NullReferenceException exception. What should i do? (i shortned the code a lot, so some things may be missing)
EDIT: i added the code for initializing the listbox and the textbox
string tabTitle { get; set; }
public void newTabButton_Click(object sender, EventArgs e)
{
TextBox textBoxJan = new TextBox();
textBoxJan.KeyDown += new KeyEventHandler(textBoxJan_KeyDown);
ListBox LBJan = new ListBox();
tabControl1.TabPages.Add(tabPage);
tabPage.Controls.Add(textBoxJan);
tabPage.Controls.Add(LBJan);
}
public ListBox LBJan;
public Label sumLabel;
public void textBoxJan_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (sender is TextBox)
{
TextBox textBoxJan = (TextBox)sender;
LBJan.Items.Add(textBoxJan.Text)
}
}
}