You cant reference lblobj from the 'else if' block as it is defined within the scope of the 'if' block and, conversely, you cannot reference lblobj1 within the 'if' block as it is defined within the scope of the 'else if' block
to remedy this you could simply move the decleration (not the instantiation) above the if-elseif
e.g.
private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
Label lblobj;
Label lblobj1;
if (cmbAdults.SelectedIndex == 1)
{
lblobj = new Label();
lblobj.Text = "Adult";
lblobj.Font = new Font(lblobj.Font, lblobj.Font.Style | FontStyle.Bold);
lblobj.BackColor = Color.Transparent;
lblobj.ForeColor = Color.White;
lblobj.AutoSize = true;
lblobj.Location = new Point(270, 110);
groupBox1.Controls.Add(lblobj);
}
else if (cmbAdults.SelectedIndex == 2)
{
lblobj1 = new Label();
lblobj1.Text = "child";
lblobj1.Font = new Font(lblobj1.Font, lblobj1.Font.Style | FontStyle.Bold);
lblobj1.BackColor = Color.Transparent;
lblobj1.ForeColor = Color.White;
lblobj1.AutoSize = true;
lblobj1.Location = new Point(270,140);
groupBox1.Controls.Add(lblobj1);
}
}
However, this is still a bad way to go about this.
Why not simply have a single label, already positioned on your form with text set to "", then the simplifies to
private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbAdults.SelectedIndex == 1)
{
lblobj.Text = "Adult";
}
else if (cmbAdults.SelectedIndex == 2)
{
lblobj.Text = "Child";
}
}