Simple question about controls
-
Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example:
Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); }
How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.There is no spoon.
-
Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example:
Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); }
How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.There is no spoon.
Hi Fred, Would the following solve your problem:
lbl = new Label();
lbl = (Label)FindControl("lblName");Ryan
-
Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example:
Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); }
How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.There is no spoon.
-
Hi Fred, Would the following solve your problem:
lbl = new Label();
lbl = (Label)FindControl("lblName");Ryan
-
Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
-
Yes, that's it, in a recursive way and for WinForms.
There is no spoon.
modified on Thursday, March 12, 2009 10:41 AM
Sorry, my mistake. How about something like this:
Control myControl = new Control();
foreach (Control c in this.Controls)
{
if (c.Name == "lblName")
{
myControl = c;
}
} -
The is no FindContol method in WinForm. You can implenet your own for this to work the control names need to be unique.
public static Control FindControl(string controlName)
{
if (!controlName.Empty)
return this.Controls.Find(controlName, true)[0];return null;
}
Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
-
Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example:
Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); }
How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.There is no spoon.
-
The is no FindContol method in WinForm. You can implenet your own for this to work the control names need to be unique.
public static Control FindControl(string controlName)
{
if (!controlName.Empty)
return this.Controls.Find(controlName, true)[0];return null;
}
Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]