By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

How to use function to change textbox color

100+
P: 134
Hi, I am new to visual basic 6, In a form I have many 20 textboxes such as

If (texbox1.Text < 2.85) Or (textbox1.Text > 3.15) Then
textbox1.Text = ""
Else
textbox1.BackColor = &H80000011

end if

If (texbox2.Text < 2.85) Or (textbox2.Text > 3.15) Then
textbox2.Text = ""
Else
textbox2.BackColor = &H80000011

end if

Is there a way that i can use function to change the backcolor without repeating
for the rest of the 20 textboxes. By the way please dont ask me to create
control array for the 20 text boxes as due to some reasons.
Apr 24 '08 #1
Share this Question
Share on Google+
12 Replies


Expert 100+
P: 487
You can prepare your own method.
  • Create a method with textbox control as parameter.
  • Program what you done here inside that method.
  • Pass all the textbox control to that method.
Apr 24 '08 #2

100+
P: 134
You can prepare your own method.
  • Create a method with textbox control as parameter.
  • Program what you done here inside that method.
  • Pass all the textbox control to that method.
I don;t really understand, Isn't the same as I still need to code for the 20 text boxes ?
Apr 24 '08 #3

jamesd0142
100+
P: 469
Expand|Select|Wrap|Line Numbers
  1. Private Sub changecolour(ByVal NoOfBoxs As Integer)
  2.         For i As Integer = 1 To NoOfBoxs
  3.             If Me.Controls("textbox" & i).Text < "3" Then
  4.                 Me.Controls("textbox" & i).BackColor = Color.Red
  5.             Else
  6.                 Me.Controls("textbox" & i).BackColor = Color.Green
  7.             End If
  8.         Next
  9.  
  10.     End Sub
  11.  
This is vb2005 code but im sure it will give you an idea of how it could be done...

Depending on the number of textbox's you say are present, it checks the value and changes the backcolor accrodingly.

James
Apr 24 '08 #4

Expert 100+
P: 487
Expand|Select|Wrap|Line Numbers
  1. Private sub ChangeColor(txtControl As Textbox)
  2. 'Your Code here
  3. End Sub
Then Call this method where ever you need.
(You have to try)
Apr 24 '08 #5

Expert 100+
P: 487
Expand|Select|Wrap|Line Numbers
  1. Private Sub changecolour(ByVal NoOfBoxs As Integer)
  2.         For i As Integer = 1 To NoOfBoxs
  3. .....................
  4. ..............
  5. ...........
  6.  
This is not good practice, because there may be some other textboxes available!
Apr 24 '08 #6

jamesd0142
100+
P: 469
This is not good practice, because there may be some other textboxes available!
simple solution to that is the ones you want the function to work on will have a different name, ie textbox1 textbox2 etc.

and call all other textboxes by more usefull names ie txtlogin, txtpassword.

its simply an example, im not saying its the best way of doin this...

James
Apr 24 '08 #7

100+
P: 134
Expand|Select|Wrap|Line Numbers
  1. Private sub ChangeColor(txtControl As Textbox)
  2. 'Your Code here
  3. End Sub
Then Call this method where ever you need.
(You have to try)
Thank you so much it work !
Apr 24 '08 #8

100+
P: 134
Expand|Select|Wrap|Line Numbers
  1. Private Sub changecolour(ByVal NoOfBoxs As Integer)
  2.         For i As Integer = 1 To NoOfBoxs
  3.             If Me.Controls("textbox" & i).Text < "3" Then
  4.                 Me.Controls("textbox" & i).BackColor = Color.Red
  5.             Else
  6.                 Me.Controls("textbox" & i).BackColor = Color.Green
  7.             End If
  8.         Next
  9.  
  10.     End Sub
  11.  
This is vb2005 code but im sure it will give you an idea of how it could be done...

Depending on the number of textbox's you say are present, it checks the value and changes the backcolor accrodingly.

James
Thank you so much for the beautiful coding, i will try it out
Apr 24 '08 #9

smartchap
100+
P: 236
Place your controls (like textboxes, labels, timer, picturebox, etc.) on the form.
The name of all the textboxes for which you want to change color must start with 'Text' or any other initials, i.e. must have starting letters same. Then use the code given below:

Private Sub Command1_Click()
End
End Sub

Private Sub Form_Load()
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
For Each TextBox In Me
If Left$(TextBox.Name, 4) = "Text" Then
If Val(TextBox.Text) < 2.85 Or Val(TextBox.Text) > 3.15 Then
TextBox.BackColor = vbRed
Else
TextBox.BackColor = vbYellow
End If
End If
Next
End Sub

In the code you can change TextBox.BackColor = vbYellow with TextBox.Text = "" as you required.

Hope it will certainly solve your problem. If yes then give a post.
Apr 25 '08 #10

100+
P: 134
Place your controls (like textboxes, labels, timer, picturebox, etc.) on the form.
The name of all the textboxes for which you want to change color must start with 'Text' or any other initials, i.e. must have starting letters same. Then use the code given below:

Private Sub Command1_Click()
End
End Sub

Private Sub Form_Load()
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
For Each TextBox In Me
If Left$(TextBox.Name, 4) = "Text" Then
If Val(TextBox.Text) < 2.85 Or Val(TextBox.Text) > 3.15 Then
TextBox.BackColor = vbRed
Else
TextBox.BackColor = vbYellow
End If
End If
Next
End Sub

In the code you can change TextBox.BackColor = vbYellow with TextBox.Text = "" as you required.

Hope it will certainly solve your problem. If yes then give a post.
Thanks You so much for that wonderful code
Apr 25 '08 #11

smartchap
100+
P: 236
Dear Jamesnkk

We are always here to help u out. With this code u can check any type of control on any form and will give u wonderful results.
Apr 26 '08 #12

smartchap
100+
P: 236
Also with my code you can check any number of textboxes. Even if u change the no. of textboxes (or other controls) it will take care of and no need to change the code.
Apr 26 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.