Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thanks in advance!!!....I need to create a multiple labels dynamically in panel using c#.net. First label shows in panel but the other labels are not shown...that's my problem...

What I have tried:

void GetFiles(DirectoryInfo d, string sourthPath, CheckedListBox cblst)
      {


          cblst.Items.Clear();
          DirectoryInfo[] dInfo = d.GetDirectories();
          if (dInfo.Length > 0)
          {
              foreach (DirectoryInfo driSub  in dInfo)
              {
                  cblst.Items.Add(sourthPath + "\\" + driSub.Name);
                  Label dynamicLabel = new Label();
                  dynamicLabel.Text = "dynamicLabel" + i;
                  panel1.Controls.Add(dynamicLabel);
                  i = i + 1;


              }
          }



      }
Posted
Updated 23-Feb-21 23:24pm
v2
Comments
Maciej Los 24-Feb-21 1:58am    
And what's wrong with your code?
Member 15028582 24-Feb-21 2:01am    
While adding the label to the panel.....the first label shown but the other labels are not shown in panel...
Maciej Los 24-Feb-21 2:11am    
Please, see my answer.

Seems, you forgot to set Location.
foreach (DirectoryInfo driSub  in dInfo)
{
    cblst.Items.Add(sourthPath + "\\" + driSub.Name);
    Label dynamicLabel = new Label()
    {
        AutoSize = true,
        Text = "dynamicLabel" + i,
        Location = new Point(cblst.Left + cblst.Width, (cblst.ItemHeight*i) + cblst.Top)
    };
    panel1.Controls.Add(dynamicLabel);
    i = i + 1;

}
 
Share this answer
 
v2
Comments
Member 15028582 24-Feb-21 2:23am    
Tq ji....it's works fine....while I am changing the location also the label didn't get top of the panel (i.e., first it gets three to four space vertically and then the label fetch in panel...I don't need like this I need to show the label in panel starting itself)....Is there any idea ji
Maciej Los 24-Feb-21 2:29am    
Well... It's your code, so change it to your needs.
Member 15028582 24-Feb-21 2:39am    
Ji....While I am changing Location = new Point(1, 20 * i) this line to Location = new Point(1, 1) it shows a albel in right postion in panel but shows only one label in panel the balance label are not shown....previous problem occurs...
Maciej Los 24-Feb-21 2:44am    
Because all your labels are placed in the same position. Think of it!
In other words: if i==0 then what's the result of: i*28, uhm?
RickZeeland 24-Feb-21 2:42am    
Ji...5d :)
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[^]:


C#
//somewhere in the scope of Form class
private List<Label> myLabels= new List<Label>();

private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
    //find label associated to the item; if not found -> create new one, otherwise change its visibility
    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[^]:
C#
void Main()
{
	MainForm mf = new MainForm();
	mf.Show();
}

// Define other methods and classes here
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)
	{
		//this.Text = $"{e.Index} => state changed from {e.CurrentValue} to {e.NewValue}";
		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;
		
	}
	

}
 
Share this answer
 
Comments
Member 15028582 24-Feb-21 7:10am    
Tq ji....If I click Start button then automatically the label should get remove from this checklistbox....I have tried but it not works

chkLBDestinationFolder.Items.Remove(myLabels);

And I too use this code also but shows error

chkLBDestinationFolder.Controls.Remove(myLabels);
Maciej Los 24-Feb-21 7:19am    
Please, study my code to find out how do i access the label related to the checked item in CkeckedListBox.
Your code is trying to remove entire list of labels at once, which is not allowed. There is even worse, you're trying to remove label from CheckedListBox.Items collection, but label is not a part of that collection!
Member 15028582 24-Feb-21 8:13am    
Ya....You are adding the dynamic label in form I need to hide that label while click the start button but it does not get hide....pls help....I have tried alot..
Member 15028582 24-Feb-21 8:14am    
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}",
Text = $"Success",
AutoSize = true,
BackColor = Color.White,
ForeColor = Color.DarkGreen,
Location = new Point(990, (chkLBDestinationFolder.ItemHeight * e.Index) + 138)
};
myLabels.Add(lbl);
this.Controls.Add(lbl);
lbl.BringToFront();
lbl.Font = new Font(lbl.Font.Name, 8, FontStyle.Bold);
lbl.Visible = false;

}
else
lbl.Visible = e.NewValue == CheckState.Checked;
lbl.Visible = false;
Member 15028582 24-Feb-21 8:14am    
but lbl didn't get visible false

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