Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert from color to brush 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I'm working with brushes and I want the option to enter a color. If the program is expecting Brushes.Black, is there a way to give it Colors.Black?

Is there any function that does a conversion?
 
Why you don't use the color instead of the brush? If you want to fill something, you acn do it with the color.
I don't know a simple way to do it, maybe there is.
You can do it by reflection, but it is hard to get the type of the brushes class since you can't make an instance of it. I found a trickey way:
Code:
Imports System.Reflection

Dim t As Type = [Assembly].LoadWithPartialName("System.Drawing").GetType("System.Drawing.Brushes")
Dim b As Brush = t.GetProperty(Color.White.Name).GetValue(Nothing, Nothing)
This will create a solid brush with a white color.
 
Looks nice korach. Have a star.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I was able to do it this way, but thank you.
Code:
'Set up color and get the color number from column 6
Dim myColor As Color = GetVBColor(Str(myDataGrid(e.Row, 6)))

Dim MyBrush As New SolidBrush(myColor)

e.ForeBrush = MyBrush

GetVBColor is a function that I was very happily given here that convers from (old vb) colors to the .net version.

Code:
Private Function GetVBColor(ByVal myColor As String)
  Dim b As Integer = CInt(Int(myColor / 65536))
  Dim g As Integer = CInt(Int((myColor - b * 65536) / 256))
  Dim r As Integer = CInt(Int(myColor - b * 65536 - g * 256))
  Return Color.FromArgb(255, r, g, b)
End Function

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top