VB.NET: Changing object back color

  • Thread starter Thread starter Chin Fui
  • Start date Start date
C

Chin Fui

Hi, all... I am writting a program that need to change the objects
backcolor(such as label, textbox). I need to change the color by using
coding, not directly change in the property menu. Then, how do I code that?

Another problem is how to apply the object color as the If statement
condition in VB.NET?

For example:
if label.backcolor = white then
label.backcolor = red
end if

How can I code the statement above in VB.NET?

Thanks...
 
You can do it this way..

If Label1.BackColor.Equals(System.Drawing.Color.White) The
Label1.BackColor = System.Drawing.Color.Re
End I

Hope this helps

Gary
 
Thanks, Gary...
I tried the way you told, but it doesn't works, although it does not have
error.
Hope anyone can help me on this problem, thanks..
 
If you step through the code with your debugger, does it go into the code that attempts to set the label backcolor to red?

Gary
 
In order to test whether the function work or not, I code the statement like
this:

If Label1.BackColor.Equals(System.Drawing.Color.White) Then
MessageBox.Show("GOOD")
Label1.BackColor = System.Drawing.Color.Red
Else
MessageBox.Show("BAD")
Label1.BackColor = System.Drawing.Color.Violet
End If

Then, the system prompt the messagebox with "BAD", but the label1 backcolor
still remain to white.

Gary Milton said:
If you step through the code with your debugger, does it go into the code
that attempts to set the label backcolor to red?
 
I just tested your exact code and it works as expected in my test application. The backcolor of the label is initially the system window colour (System.Drawing.SystemColors.Window) so the code falls into the 'else', displays "BAD", and *does* change the backcolor of the labels to violet. Have you changed any properties of the label from ther defaults? My only thought is that maybe one of the properties is set to some obscure setting that is preventing the backcolor from being changed. Check the 'Windows Form Designer generated code' region and see what properties your label has. The only ones mine has are as follows (all the other properties are using the default values)..


'Label

Me.Label1.Location = New System.Drawing.Point(32, 28
Me.Label1.Name = "Label1
Me.Label1.TabIndex =
Me.Label1.Text = "Label1

If it any help, I tested this in VS.NET 2003 and .NET Framework 1.1

Gary
 
* "Chin Fui said:
backcolor(such as label, textbox). I need to change the color by using
coding, not directly change in the property menu. Then, how do I code that?

Another problem is how to apply the object color as the If statement
condition in VB.NET?

For example:
if label.backcolor = white then
label.backcolor = red
end if

How can I code the statement above in VB.NET?

\\\
If Label1.BackColor.Equals(Color.White) Then
...
End If
///

Don't give an instance variable the same name as a class.
 
Back
Top