I have Label controls in Panel controls that need to be updated. The Panel and Label controls are dynamically created. Now I need to find a way to get the value of 1 Label in a Panel.
C# Code
// Create Panel
Panel newpanel = new Panel();
newpanel.Name = "panel_" + reader.GetValue(0);
newpanel.Size = new Size(200, 200);
newpanel.BorderStyle = BorderStyle.FixedSingle;
newpanel.Parent = FlowPanel;
// Create Label
Label newipaddress = new Label();
newipaddress.Name = "lbl_ip_add";
newipaddress.Text = reader.GetValue(3).ToString();
newipaddress.Location = new Point(55, 175);
newipaddress.Parent = newpanel;
-------------
foreach (Panel p in FlowPanel.Controls)
{
string ip = !!! GET IP FROM LABEL !!!
Ping pingSender = new Ping();
IPAddress pingIP = IPAddress.Parse(ip);
PingReply pingReply = pingSender.Send(pingIP);
lbl_ping_1.Text = string.Format("Ping: {0}", pingReply.RoundtripTime.ToString());
if ((int)pingReply.RoundtripTime < 150)
{
lbl_ping_1.BackColor = Color.Green;
}
else if ((int)pingReply.RoundtripTime < 200)
{
lbl_ping_1.BackColor = Color.Orange;
}
else
{
lbl_ping_1.BackColor = Color.Red;
}
}
The string ip needs to get the IP from the Label. The IP is in string format that will be converted to the IP address as you can see.
How can I get the value of the dynamically created Label?
GUI Tools like labels really shouldn’t hold the data, it should just show the data. So in your case, it would really be better to hold the label information in a local variable or dictionary.
In either case, you can search your panel’s control collection for the label’s name (control key):
This assumes when you created your label, you named it “ipLabel”:
Update:
You also need to add the controls to the container using the
Controlscollection instead of setting theParentof the control.Example:
I would do this with the panel to the flowpanel control as well: