Change text color to bold

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
I have a text box named txtName I have a label corresponding to it named lbl_txtName. I have a much wider collection of such items. BAsically what I want to do is if the user doesnt enter anything in the textbox, then label text lbl_<NAME OF THE CONTROL> should be made red. I look through my controls list when the user clicks save. Is there anyway to dynamically get a handle to this control by passing the name of the control?
 

Craig

Well-known member
Joined
Oct 15, 2005
Messages
110
Location
United Kingdom
Programming Experience
Beginner
VB.NET:
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If TxtName.Text = "" Then
            lbl_TxtName.ForeColor = System.Drawing.Color.Red
        End If
    End Sub

That what you want?
 

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
yes and no but i am looping through 50 controls to check this so it needs to go in a loop and i dont want to hard code the name. just contstructed it dynamically from the Control.Name
 

gazeranco

Well-known member
Joined
Feb 11, 2006
Messages
45
Location
Englandshire!
Programming Experience
Beginner
Im not sure either, but you know how when you fill in details on a form on the tinternet... an asp form, and it says "please fill in the details marked with a red *" well thats done in VS 2005 (asp) using validation controls and its sooo easy, but i dunno if it can be done in VB, i don't see why not but im not sure??

It allows you to assign the same type of validation to a group of controls when you give them a group name, but maybe this cant be done..
 

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
For now I just need a way to get a handle to the label control then call the .ForeColor and change the control. The question is how to get a handle to the label control by passing its name?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,557
Location
Norway
Programming Experience
10+

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,557
Location
Norway
Programming Experience
10+

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
i tried the reflection but propinfo returns nothing. The getControlFromName worked for me! but if there are a lot of controls on the form it is going to be slow.
 

anyoneis

New member
Joined
Feb 17, 2006
Messages
4
Location
Seattle
Programming Experience
10+
I would use the ErrorProvider.

1) Create a new VB.NET 2005 Windows Application project
2) Drop 3 or 4 pairs of labels and textboxes on the form
3) drop a button on the form
4) drop an ErrorProvider onto the form
5) double click on the button and enter



ForEach textbox As Control In Me.Controls
If TypeOf (textbox) Is TextBox AndAlso textbox.Text = String.Empty Then


ErrorProvider1.SetError(textbox,​
"You must enter a value here")


EndIf


Next

Ciao,
David
 

anyoneis

New member
Joined
Feb 17, 2006
Messages
4
Location
Seattle
Programming Experience
10+
If you really must use the label color visual indication, then you could try something like:

PublicClass Form1

Dim digits AsChar() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}


PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ForEach textbox As Control InMe.Controls
IfTypeOf (textbox) Is TextBox Then

SetLabelForeColor(textbox,​
False)

EndIf

Next

EndSub

PrivateSub TextBox1_TextChanged(ByVal sender AsObject, ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged, _

TextBox4.TextChanged
SetLabelForeColor(​
DirectCast(sender, TextBox), True)

EndSub


PrivateSub SetLabelForeColor(ByVal textbox As TextBox, ByVal ResetOnly AsBoolean)
Dim tbIndex AsInteger = textbox.Name.IndexOfAny(digits)
Dim suffix AsString = textbox.Name.Substring(tbIndex)
Dim label As Control = Me.Controls("Label" + suffix)
If label IsNotNothingAndTypeOf (label) Is Label Then

If textbox.Text = String.Empty AndNot ResetOnly Then

DirectCast(label, Label).ForeColor = Color.Red

Else

DirectCast(label, Label).ForeColor = Color.Black

EndIf

EndIf

EndSub
End
Class


David
 
Top Bottom