Converting Color.Black to Brushes.Black

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

Hi all,

I am using the color dialog box to select a colour
to print with. However, the

e.Graphics.DrawString

method uses Brushes.Black, not Color.Black.

How do I convert from Brushes to Colors and back
again so that my program will run correctly?

Any help much appreciated,

Jason.
 
You could try something like:

'Determine the font to use
Dim MyFont As New Font(Drawing.FontFamily.GenericSansSerif, 8.0F,
FontStyle.Bold, GraphicsUnit.Point, 0, False)
'Determine which color you are going to use
Dim MyColor As Color = Color.Black
'Create a brush based on the color
Dim MyBrush As New SolidBrush(MyColor)
'Draw the string
e.Graphics.DrawString("Test String", MyFont, MyBrush, 0, 0)
'Dispose of the brush object.
MyBrush.Dispose
'Dispose of the font object
MyFont.Dispose

With this method, you don't need to return the color from the brush since
you have already defined the color before creating the brush. You simply
access MyColor to determine which color the text was drawn in.

Hope this helps,
Jody
 
You could try something like:

'Determine the font to use
Dim MyFont As New Font(Drawing.FontFamily.GenericSansSerif, 8.0F,
FontStyle.Bold, GraphicsUnit.Point, 0, False)
'Determine which color you are going to use
Dim MyColor As Color = Color.Black
'Create a brush based on the color
Dim MyBrush As New SolidBrush(MyColor)
'Draw the string
e.Graphics.DrawString("Test String", MyFont, MyBrush, 0, 0)
'Dispose of the brush object.
MyBrush.Dispose
'Dispose of the font object
MyFont.Dispose

With this method, you don't need to return the color from the brush since
you have already defined the color before creating the brush. You simply
access MyColor to determine which color the text was drawn in.

Hope this helps,
Jody
 
* JJ said:
I am using the color dialog box to select a colour
to print with. However, the

e.Graphics.DrawString

method uses Brushes.Black, not Color.Black.

Color -> Brush:

\\\
Dim b As New Brush(Color.Black)
..
..
..
b.Dispose()
///

Why do you need the reverse "conversion"?
 
Thanks for the help with the color to brush cast.

Using
dim myBRush as new SoldiBrush(myLabels.Colour)

did the trick, but

Dim myBrush As New Brush(Color.Black)


did not!

myLabels is a class and Colour is a shared property that
stores its value in a private shared field that is defined as

Color.

Do you have any ideas when one method should work
and the other not? I get a message that says

'New' cannot be used on a class that is declared 'MustInherit'.

I have the program segment working now but it would be
useful to understand what the difference is between your
two suggestions!

Thanks,

Jason.
 
JJ said:
Thanks for the help with the color to brush cast.

Using
dim myBRush as new SoldiBrush(myLabels.Colour)

did the trick, but

Dim myBrush As New Brush(Color.Black)


did not!

myLabels is a class and Colour is a shared property that
stores its value in a private shared field that is defined as

Color.

Do you have any ideas when one method should work
and the other not? I get a message that says

'New' cannot be used on a class that is declared 'MustInherit'.

I have the program segment working now but it would be
useful to understand what the difference is between your
two suggestions!


Brush is an abstract class, i.e. you can not create an instance. You can
only create an instance of classes derived from Brush, like SolidBrush.
 
Back
Top