in the button click event i have to create three label dynamically and if i double click first label a combobox was create and show with values. the first label will be hidden, then if i select a value from the combobox that value will be placed as the first label content, then again if i double click the first label which is having the combobox value, the same combobox will appear and now the label will be hidden.
i need code for this.......
Loading
Jean PaulPosted Jan 12, 2011, 7:09 AM
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateLabel();
CreateLabel();
CreateLabel();
}
private int _count = 1;
private void CreateLabel()
{
Label label = new Label();
label.Parent = this;
label.Left = 10;
label.Top = _count * 30;
label.Text = "Label " + _count.ToString();
label.MouseClick += new MouseEventHandler(label_MouseClick);
ComboBox comboBox = new ComboBox();
comboBox.Visible = false;
comboBox.Parent = this;
comboBox.Top = label.Top;
comboBox.Left = label.Left;
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox.Tag = label;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
// Fill items
foreach (string item in GetItems())
comboBox.Items.Add(item);
// Store combobox to label tag
label.Tag = comboBox;
_count++;
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
((sender as ComboBox).Tag as Label).Show();
((sender as ComboBox).Tag as Label).Text = (sender as ComboBox).SelectedItem.ToString();
(sender as ComboBox).Hide();
}
private IEnumerable GetItems()
{
yield return "Item 1";
yield return "Item 2";
yield return "Item 3";
}
void label_MouseClick(object sender, MouseEventArgs e)
{
(sender as Label).Hide();
((sender as Label).Tag as ComboBox).Show();
}
}
You can also try using the attachment in reply.
(mark as answer if solves)