Windows Develop Bookmark and Share   
 index > Windows Forms General > Dynamically referencing a Label Control
 

Dynamically referencing a Label Control

Hi.

I'm trying to write a generalevent that highlights a textbox and an associated label. This event isthetied to multiple textbox's Enter event.

So far I have this, which successfully changes the background color of the textbox to lightblue when the user enters the textbox.

private void HightLight(object sender, System.EventArgs e)

{

TextBox t = (TextBox)sender;

t.BackColor = Color.LightBlue;

}

But how do I make a reference to the associated label?

The naming of my textbox's and labels are the sameexcept for a txt and lbl prefix.

ie: a customer textbox and customer label would be named "txtCustomer" and "lblCustomer" respectively.

Through string manipulation I am able to derivie the label's name from the sender's textbox's name. But is there a way to now reference the label given that I know it's name?

MTech8  Friday, July 13, 2007 3:58 PM
You can use the Textbox' Parent.Controls property to find the label back. Here's an example:

private static void HighLight(object sender, bool enter) {
Control ctl = sender as Control;
if (ctl == null) return;
ctl.BackColor = enter ? Color.LightBlue : Color.Empty;
// Find matching label, assume control's name has a 3-letter prefix (like "txtSample")
string lblName = "lbl" + ctl.Name.Substring(3);
Control lbl = ctl.Parent.Controls[lblName] as Control;
if (lbl != null) lbl.Font = new Font(lbl.Font, enter ? FontStyle.Bold : FontStyle.Regular);
}
private void txtSample_Enter(object sender, EventArgs e) {
HighLight(sender, true);
}
private void txtSample_Leave(object sender, EventArgs e) {
HighLight(sender, false);
}

nobugz  Friday, July 13, 2007 4:16 PM
You can use the Textbox' Parent.Controls property to find the label back. Here's an example:

private static void HighLight(object sender, bool enter) {
Control ctl = sender as Control;
if (ctl == null) return;
ctl.BackColor = enter ? Color.LightBlue : Color.Empty;
// Find matching label, assume control's name has a 3-letter prefix (like "txtSample")
string lblName = "lbl" + ctl.Name.Substring(3);
Control lbl = ctl.Parent.Controls[lblName] as Control;
if (lbl != null) lbl.Font = new Font(lbl.Font, enter ? FontStyle.Bold : FontStyle.Regular);
}
private void txtSample_Enter(object sender, EventArgs e) {
HighLight(sender, true);
}
private void txtSample_Leave(object sender, EventArgs e) {
HighLight(sender, false);
}

nobugz  Friday, July 13, 2007 4:16 PM

Thank you nobugz. You rock!

I really appreciate you taking the time to inlclude the code. Definitely takes the guess work out for beginners!

Had a little issue with this line of code:

Control lbl = ctl.Parent.Controls[lblName] as Control;

It won't alllow meto reference controls by their string name. It needed the int value of the control in the collection.

So I just wrote some code to loop through the code to retrieve the control.

public static Control GetControlByName(string name, Control Controls)

{

foreach (Control ctrl in Controls.Parent.Controls)

{

if (ctrl.Name == name)

return ctrl;

}

return null;

}

thus replacing your the linethat threw the error with:

Control lbl = GetControlByName(lblName, ctl);

Works perfectly now! Wouldn't be possible without your help.

MTech8  Monday, July 16, 2007 6:12 PM
Very odd problem, shouldn't happen. Oh well, as long as you got it working.
nobugz  Monday, July 16, 2007 6:23 PM

You can use google to search for other answers

Custom Search

More Threads

• What is "application framework"?
• Remove Listbox Scrollbar VB.NET
• Icon
• Using configurationmanager in Windows form, vs.net 2.0
• reading column names from a datagridview
• Losing Application's Top Window Position
• Screen Updating Issues
• [.NET3.X]difference between MessageBox from Windows & Windows.Forms?
• non-rectangular controls in Windows Forms
• How to add culture support in a property with a user control.