Not able to test BackColor of control

T

tshad

I have a Windows form where I am trying to set the BackColor of a control
and check the color before I do it:

If control.BackColor = System.Drawing.Color.Blue Then
ChangeColor(control, Color.Red)
Else
ChangeColor(control, Color.Blue)
End If

But I get an error:

Operator '=' is not defined for types 'System.Drawing.Color' and
'System.Drawing.Color'.

I can apparently do:

control.BackColor = System.Drawing.Color.Blue

Why can't I test the color?

Thanks,

Tom
 
T

Tom Shelton

I have a Windows form where I am trying to set the BackColor of a control
and check the color before I do it:

If control.BackColor = System.Drawing.Color.Blue Then
ChangeColor(control, Color.Red)
Else
ChangeColor(control, Color.Blue)
End If

But I get an error:

Operator '=' is not defined for types 'System.Drawing.Color' and
'System.Drawing.Color'.

I can apparently do:

control.BackColor = System.Drawing.Color.Blue

Why can't I test the color?

Thanks,

Tom

Try using:

If control.BackColor.Equals(System.Drawing.Color.Blue) Then
....
else
....
end if
 
T

tshad

Tom Shelton said:
Try using:

If control.BackColor.Equals(System.Drawing.Color.Blue) Then

That worked.

Where else do I need to use .Equals?

I would assume that if I set a value using an assignment (=), that I would
also be able to test it the same way?

If :

control.BackColor = System.Drawing.Color.Blue

why not

IF control.BackColor = System.Drawing.Color.Blue then ...

Thanks,

Tom
 
H

Herfried K. Wagner [MVP]

tshad said:
I have a Windows form where I am trying to set the BackColor of a control
and check the color before I do it:

If control.BackColor = System.Drawing.Color.Blue Then
ChangeColor(control, Color.Red)
Else
ChangeColor(control, Color.Blue)
End If

But I get an error:

Operator '=' is not defined for types 'System.Drawing.Color' and
'System.Drawing.Color'.

In addition to the other replies: I assume you are using VB.NET 2002/2003.
Those versions do not yet support operator overloading. With VB 2005, the
code above would compile because VB 2005 supports operator overloading and
'Color' overloads the '=' (comparison) operator.
 
T

Tom Shelton

In addition to the other replies: I assume you are using VB.NET 2002/2003.
Those versions do not yet support operator overloading. With VB 2005, the
code above would compile because VB 2005 supports operator overloading and
'Color' overloads the '=' (comparison) operator.

I was going to ask him if he was using VB.NET 2002/2003 - but then I
realized, he had to be or he wouldn't have gotten the error :)
 
T

tshad

Cor Ligthert said:
That,

You can by using the ToARGB, however in this case it is in my idea more
documentative as Tom wrote.

http://msdn2.microsoft.com/en-us/library/system.drawing.color.toargb.aspx

However if you prefer this, there is nothing wrong to use it.

It is not that I prefer, just trying to figure out why. And if I have to
use .Equals here - where else do I need to use it?

I was just confused in the fact that I can assign it but can't test it in
the same way.

Thanks,

Tom
 
T

tshad

Tom Shelton said:
I was going to ask him if he was using VB.NET 2002/2003 - but then I
realized, he had to be or he wouldn't have gotten the error :)

You're right.

It is VS.Net 2003.

Tom
 
T

Tom Shelton

You're right.

It is VS.Net 2003.

Tom


I figured it must be... Those versions don't support operator
overloading. Another way you could have done this is called
op_equality directly, but that is a little more ugly then just
calling .Equals.

Just so you know, in VB.NET 2005 and latter, you could just say:

If control.BackColor = Color.Red then
 
T

tshad

Tom Shelton said:
I figured it must be... Those versions don't support operator
overloading. Another way you could have done this is called
op_equality directly, but that is a little more ugly then just
calling .Equals.

Just so you know, in VB.NET 2005 and latter, you could just say:

If control.BackColor = Color.Red then

That's great.

I am hoping some of the other objects on forms are easier to deal with as
they are in asp.net, such as DataGrid. I'm sure you've noticed I have asked
a lot of questions about DataGrids for forms. Handling it is not as
intuitive as for the DataGrid on Asp.Net. Most of the objects are have the
same of similar methods and attributes such as getting number of rows or
getting data from the object. On windows forms, it doesn't seem to be a
straight forward.

Thanks,

Tom
 
T

Tom Shelton

That's great.

I am hoping some of the other objects on forms are easier to deal with as
they are in asp.net, such as DataGrid.

I don't know - asp.net and windows forms are completely different on
most things, when it comes to controls :) Comming form an asp.net
background you might find WPF a little more to your liking.
I'm sure you've noticed I have asked
a lot of questions about DataGrids for forms. Handling it is not as
intuitive as for the DataGrid on Asp.Net. Most of the objects are have the
same of similar methods and attributes such as getting number of rows or
getting data from the object. On windows forms, it doesn't seem to be a
straight forward.

Thanks,

Tom


As for the DataGrid - wish I could help you there. I'm not much of a
datagrid person. I do know that in 2005, they have deprecated the
datagrid completely (well, almost completely, the only place you would
use it is if you need to display your data in a hiearchial format).
The replacement is the DataGridView. You usually hook it to a
BindingSource, etc, etc....

To be honest, most of my grid experience has been with the Janus
Grid :)
 
C

Cor Ligthert[MVP]

Tom,

I would not see direct the DataGridView as a replacement for a windows form
DataGrid.

A datagrid can show datasets, a DataGridView only single tables of that
however too all kind of other classes. It is a complete different complex
data control).

Although I agree that in the DataGridView the problems in from the DataGrid
are not repeated in the DataGridView.

Ken has specialized himself a little bit in it, we have together a website.

http://www.vb-tips.com/default.aspx

The website needs revision, we have probably used to much Ajax in it, it is
fast but not easy to handle.

Cor
 
T

Tom Shelton

Tom,

I would not see direct the DataGridView as a replacement for a windows form
DataGrid.

According to MS, it is. They suggest, that unless you need to display
heiarchial (parent -> child) relationships, that DataGridView be used
in all new development. They don't even put the old DataGrid in the
toolbox anymore.
From the help:
"The DataGrid control is retained for backward compatibility and for
special needs. For nearly all purposes, you should use the
DataGridView control."
A datagrid can show datasets, a DataGridView only single tables of that
however too all kind of other classes. It is a complete different complex
data control).

It can't do hiearchial relationships, which is what I said. Anyway,
it doens't matter to me much - because I don't really like either one
of them :) I usually use a third party grid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top