[2005] testing colors in VB
Hi - newbie to VB - am using Textboxes to dispolay text and have the textboxes colored yellow, white and red depending upon their designation. I want to use the double-click event processing of the textboxes (using event handler) and take different actions depending upon the color of the textbox. I set the color as shown (this works OK):
TextBox1.BackColor = Color.Yellow
I attemp to test the color with the following (this does not work)
if TextBox1.BackColor = Color.Yellow then
' do something'
end if
if TextBox1.BackColor = Color.Red then
' do something else'
end if
This creates an error and does not work and I have not been able to find the way to test this and take appropriate action. Help would be appreciated in the way to test the BackColor existing and take action depending on what this color is.
Thanks and cheers
Ross
Re: [2005] testing colors in VB
use the .Equals method like this ...
VB Code:
[COLOR=Blue]Private Sub[/COLOR] Form1_Load([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] MyBase.Load
TextBox1.BackColor = Color.Yellow
[COLOR=Blue]End Sub[/COLOR]
[COLOR=Blue]Private Sub[/COLOR] TextBox1_DoubleClick([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] [COLOR=Blue]Object[/COLOR], [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] TextBox1.DoubleClick
[COLOR=Blue]If[/COLOR] TextBox1.BackColor.[B]Equals[/B](Color.Yellow) [COLOR=Blue]Then[/COLOR]
MessageBox.Show([COLOR=Purple]"yellow!"[/COLOR])
[COLOR=Blue]ElseIf[/COLOR] TextBox1.BackColor.[B]Equals[/B](Color.Red) [COLOR=Blue]Then[/COLOR]
MessageBox.Show([COLOR=Purple]"red!"[/COLOR])
[COLOR=Blue]End If[/COLOR]
End Sub
Re: [2005] testing colors in VB
Quote:
Originally Posted by Ross007
Hi - newbie to VB - am using Textboxes to dispolay text and have the textboxes colored yellow, white and red depending upon their designation. I want to use the double-click event processing of the textboxes (using event handler) and take different actions depending upon the color of the textbox. I set the color as shown (this works OK):
TextBox1.BackColor = Color.Yellow
I attemp to test the color with the following (this does not work)
if TextBox1.BackColor = Color.Yellow then
' do something'
end if
if TextBox1.BackColor = Color.Red then
' do something else'
end if
This creates an error and does not work and I have not been able to find the way to test this and take appropriate action. Help would be appreciated in the way to test the BackColor existing and take action depending on what this color is.
Thanks and cheers
Ross
Hi,
You could do something like this also;
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.BackColor = Color.Yellow Then
' do something'
MsgBox("Yellow")
End If
If TextBox1.BackColor = Color.Red Then
' do something else'
MsgBox("Red")
End If
End Sub
Change the color of yout textbox with another button and see the result.
Wkr,
sparrow1
Re: [2005] testing colors in VB
Re: [2005] testing colors in VB
Re: [2005] testing colors in VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.BackColor = Color.Yellow Then
' do something'
MsgBox("Yellow")
End If
If TextBox1.BackColor = Color.Red Then
' do something else'
MsgBox("Red")
End If
End Sub
could be:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Select Case TextBox1.BackColor
Case Colors.Yellow
'Yellow color code
Case Colors.Red
'Red Color code
End Select
End Sub