Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have combo box, and i use selectedindexchanged event,note the following codes. if i choose, index value one it will create a a label adult, but if i choose index value as two it should show a label 'child' and parallelly it shouldn't show a label 'Adult'. How can i solve this problem?
C#
private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAdults.SelectedIndex == 1)
            {
                Label 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);
                //this.Controls.Add(lblobj);
                groupBox1.Controls.Add(lblobj);
            }
            else if (cmbAdults.SelectedIndex == 2)
            {
                Label 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);
            }
}


i can't use lblobj in 'Child' label and lblobj1 in 'Adult' label..
Posted
Updated 7-Sep-11 23:42pm
v3

private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
Label lblobj ;

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);
//this.Controls.Add(lblobj);
groupBox1.Controls.Add(lblobj);
}
else if (cmbAdults.SelectedIndex == 2)
{
Label 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.Remove(lblobj)
//Or
if(lblobj != null)
lblObj.Visible = false;
groupBox1.Controls.Add(lblobj1);
}
}
 
Share this answer
 
v2
Comments
Viswanthan.M. 8-Sep-11 5:29am    
i can't use lblobj in 'Child' label and lblobj1 in 'Adult' label..
Set name property of each label.
Then use the containsKey and removeByKey methods to remove the objects prior to adding another.

sample usage is shown below -
C#
lblobj.Name = "lblAdult"; // "lblChild"
// in your  if(cmbAdults.SelectedIndex == 2) block 
if(groupBox1.Controls.containsKey("lblAdult")) {
 groupBox1.Controls.removeByKey("lblAdult");
}
// similarly for  if(cmbAdults.SelectedIndex == 1) block

Thanks & Regards,
Niral Soni
 
Share this answer
 
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.

C#
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

C#
private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cmbAdults.SelectedIndex == 1)
    {
        lblobj.Text = "Adult";
    }
    else if (cmbAdults.SelectedIndex == 2)
    {
        lblobj.Text = "Child";                
    }
}
 
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