Accordingly to the comments under solution #1...
If you would like to display Label on the right side of checked item in
CheckedListBox
, you can use
CheckedListBox.ItemCheck Event (System.Windows.Forms) | Microsoft Docs[
^]:
private List<Label> myLabels= new List<Label>();
private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
Label lbl = myLabels.Where(x=> x.Name.Equals($"Label{e.Index}")).SingleOrDefault();
if(lbl==null)
{
lbl = new Label(){Name=$"Label{e.Index}", Text = $"Label{e.Index}", AutoSize=true, Location = new Point(160, (clb.ItemHeight*e.Index)+4)};
myLabels.Add(lbl);
this.Controls.Add(lbl);
}
else
lbl.Visible = e.NewValue == CheckState.Checked;
}
Working example created in
LinqPad[
^]:
void Main()
{
MainForm mf = new MainForm();
mf.Show();
}
public class MainForm: Form
{
List<Label> myLabels= new List<Label>();
CheckedListBox clb = new CheckedListBox(){Location=new Point(4,4), Size = new Size(150,180)};
public MainForm()
{
this.Size = new Size(300,250);
this.MinimizeBox = false;
this.MaximizeBox = false;
clb.ItemCheck+= clb_ItemCheck;
this.Controls.Add(clb);
for(int i=0; i<10; i++)
clb.Items.Add($"Item{i}");
}
private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
Label lbl = myLabels.Where(x=> x.Name.Equals($"Label{e.Index}")).SingleOrDefault();
if(lbl==null)
{
lbl = new Label(){Name=$"Label{e.Index}", Text = $"Label{e.Index}", AutoSize=true, Location = new Point(160, (clb.ItemHeight*e.Index)+4)};
myLabels.Add(lbl);
this.Controls.Add(lbl);
}
else
lbl.Visible = e.NewValue == CheckState.Checked;
}
}