Click here to Skip to main content
15,071,262 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;

}
   
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 :)
Maciej Los 24-Feb-21 2:43am
   
Thank you, Rick.
Member 15028582 24-Feb-21 3:23am
   
ji.....can't understand
Maciej Los 24-Feb-21 3:28am
   
Which part you don't understand?

Test it:
//current code
for(int i =0; i<10; i++)
	Console.WriteLine($"{i} * {28} = {i*28}");
//changed code
for(int i =0; i<10; i++)
	Console.WriteLine($"({i} * {24})+4 = {(i*24)+4}");

Member 15028582 24-Feb-21 3:37am
   
Ji....Location = new Point(1, 20 * i) this line.....while I am changing the location
Location = new Point(1, 1) also the label didn't get the top of the panel...it gives some 3 to 4 spaces horizontally and then place a label in panel...I don't want that upper space....the label should start from a panel starting position...
Maciej Los 24-Feb-21 3:42am
   
This should give you proper Location:
Location = new Point(1, (20 * i)+1)

Unless.... you set Margin or Padding for Panel...
Member 15028582 24-Feb-21 4:22am
   
Ji....Sry...I need one clarification....In checklistbox the details are fetched vertically and I need to set the dynamic label horizontally with the same line...Is this possible in c#.net..
Member 15028582 24-Feb-21 4:23am
   
Now I added this line chkLBDestinationFolder.Controls.Add(dynamicLabel);.....But the label shows under the checkbox list item...But I need in same line...
Maciej Los 24-Feb-21 4:28am
   
Sorry, but i DON'T see your screen and CAN'T read in your mind. I have no idea what you mean by "in the same line"...
Member 15028582 24-Feb-21 4:35am
   
Actually....I have a Check list box in my winform ....In form load 10 items are loaded in that Checklistbox vertically one by one....If I choose the 3rd checkbox automatically need to create one dynamic label horizontally and show it in same line horizontally.....
Member 15028582 24-Feb-21 4:36am
   
Item1
--->Item2(If I tick this chkbox) (sucess label shown horizontally like this)
Item3
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;
		
	}
	

}
   
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
Member 15028582 24-Feb-21 23:27pm
   
ji....ur add the label in form in ur code....but i need to add the label in checklistbox ...Actually I added but it shows only once....I don't know what's the reason
Member 15028582 24-Feb-21 23:33pm
   
private void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
{

if (!destination.Exists)
{
destination.Create();
}

// Copy all files.
FileInfo[] files = source.GetFiles();

foreach (Object item in chkLBDestinationFolder.Items)
{
if (Convert.ToString(destination.FullName) == Convert.ToString(item))
{
index = chkLBDestinationFolder.Items.IndexOf(item);
}

//Console.WriteLine("{0}:{1}", item, index);
}

foreach (FileInfo file in files)
{
FileName = file.Name;
if(!FileName.Contains(".rdlc"))
{
destinationDir1 = destination.FullName;
file.CopyTo(Path.Combine(destination.FullName, file.Name), true);
}
else
{

}


}

// Process subdirectories.
DirectoryInfo[] dirs = source.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
// Get destination directory.
string destinationDir = Path.Combine(destination.FullName, dir.Name);
destinationDir1 = destinationDir;
// Call CopyDirectory() recursively.
CopyDirectory(dir, new DirectoryInfo(destinationDir));
}
WriteLogFile(source.FullName, destination.FullName);



Label lbl = myLabels.Where(x => x.Name.Equals($"Label{index}")).SingleOrDefault();
//if (lbl == null)
//{
lbl = new Label()
{
Name = $"Success",
//Text = $"Label{e.Index}",
Text = $"Success",
AutoSize = true,
BackColor = Color.White,
ForeColor = Color.DarkGreen,

Location = new Point(450, 0)
};
myLabels.Add(lbl);
chkLBDestinationFolder.Controls.Add(lbl);
//this.Controls.Add(lbl);
lbl.BringToFront();
lbl.Font = new Font(lbl.Font.Name, 8, FontStyle.Bold);
}
Member 15028582 24-Feb-21 23:49pm
   
Sry ji....I got the output...Prblm is in location...Thanks for the support ji

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