473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use function to change textbox color

134 100+
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
12 7001
CyberSoftHari
487 Expert 256MB
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
jamesnkk
134 100+
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
469 256MB
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
CyberSoftHari
487 Expert 256MB
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
CyberSoftHari
487 Expert 256MB
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
469 256MB
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
jamesnkk
134 100+
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
jamesnkk
134 100+
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
236 100+
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
jamesnkk
134 100+
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
236 100+
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
236 100+
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

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

Similar topics

0
by: Mauro Baraldi | last post by:
Hello, Someone can post some example of how to change the color of text on the TextBox. I try to use the method bind to capture a event like <b> key press, but I don' found a method to change...
5
by: Matt | last post by:
I created 3 hyperlinks, when the user click each link, it will change the color of the text of a link. For example, when user clicks Link1, text Link1 will become red color, but Link2 and Link3...
4
by: Alexandre Jaquet | last post by:
Hi, I would like to know how can I change a row background color when an user select a row thx
1
by: Mike | last post by:
On my update screen i want to change the textbox color to Yellow only if the field is editable. How can i do that within code? thx
2
by: Walter | last post by:
I have a GridView using Template columns. In my EditItemTemplate I am using CompareValidators to check data types. When validation fails, I would like to change the border of the textbox in...
4
by: Matrixreloadedth | last post by:
How to change disable color of Checkbox??? I have a checkbox with forecolor in red but when i disable by set Enable properties to false forecolor is changed to gray color but i don't want it. how...
2
by: Shailja | last post by:
Hello, Please can anyone suggest me code to change the color of textbox at runtime in VB?
1
by: zand | last post by:
how can i change textbox alignment in web form in vb.net . i wanna to have righttoleft textbox
6
by: stormcandi | last post by:
Hello, I know this question has been asked everywhere but I cannot find a solution that will help me. I hope someone here has some ideas. I have 9 TextBoxes in a Repeater Control. A user can...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.