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.
12 7001
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.
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 ?
-
Private Sub changecolour(ByVal NoOfBoxs As Integer)
-
For i As Integer = 1 To NoOfBoxs
-
If Me.Controls("textbox" & i).Text < "3" Then
-
Me.Controls("textbox" & i).BackColor = Color.Red
-
Else
-
Me.Controls("textbox" & i).BackColor = Color.Green
-
End If
-
Next
-
-
End Sub
-
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
- Private sub ChangeColor(txtControl As Textbox)
-
'Your Code here
-
End Sub
Then Call this method where ever you need.
(You have to try)
-
Private Sub changecolour(ByVal NoOfBoxs As Integer)
-
For i As Integer = 1 To NoOfBoxs
-
.....................
-
..............
-
...........
-
This is not good practice, because there may be some other textboxes available!
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
- Private sub ChangeColor(txtControl As Textbox)
-
'Your Code here
-
End Sub
Then Call this method where ever you need.
(You have to try)
Thank you so much it work !
-
Private Sub changecolour(ByVal NoOfBoxs As Integer)
-
For i As Integer = 1 To NoOfBoxs
-
If Me.Controls("textbox" & i).Text < "3" Then
-
Me.Controls("textbox" & i).BackColor = Color.Red
-
Else
-
Me.Controls("textbox" & i).BackColor = Color.Green
-
End If
-
Next
-
-
End Sub
-
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
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.
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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
|
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...
|
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...
|
by: Shailja |
last post by:
Hello,
Please can anyone suggest me code to change the color of textbox at runtime in VB?
|
by: zand |
last post by:
how can i change textbox alignment in web form in vb.net .
i wanna to have righttoleft textbox
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |