Colour problems

grags

Active member
Joined
Mar 26, 2005
Messages
26
Programming Experience
Beginner
Firstly I've been programming the basic language on and off pretty much all my life, from the Commodore 64 (My pride and joy!) to Qbasic and finally .net. But I'm totally useless at .net. So the questions are probably very rookie :)

I'm also terrible at explaining things but I'll give it my best shot.



There are 2 things I desperately need.


1, I need to be able to convert a Pictureboxes Backcolour to a Brush colour

Example:

VB.NET:
    Private Sub picCol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picCol.Click
        If ColorDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        Dim Col As Color = ColorDialog.Color

        Dim g As Drawing.Graphics = pic.CreateGraphics

        g.FillRectangle(Col, 1, 1, 20, 20) '??????
    End Sub



2, How do I save a color to file?

Example:

VB.NET:
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Sav.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        FileOpen(1, Sav.FileName, OpenMode.Output)        
             Write(1, SomeString)
             Write(1, SomeNumber)

             Write(1, SomeColor) '?????              
        FileClose()
    End Sub


Any help will be greatly appreciated.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
1. Properties of the Brushes class, e.g. Brushes.Red and Brushes.Black, return SolidBrush objects. You can create a SolidBrush yourself and pass a Color to the constructor, e.g.
Dim myBrush As New SolidBrush(myPictureBox.BackColor)
2. First of all you wouldn't use that outdated form of I/O. This is .NET so use .NET, i.e. members of the System.IO namespace, e.g. FileStream or StreamWriter.

The best way to save the Color value depends on what sort of file you're using. One simple option would be to call ToArgb on the Color to get a single Integer representing the colour and storing that in the file. When you get the value back from the file later, you can call Color.FromArgb and pass the Integer to get a Color value back.
 

grags

Active member
Joined
Mar 26, 2005
Messages
26
Programming Experience
Beginner
1. Properties of the Brushes class, e.g. Brushes.Red and Brushes.Black, return SolidBrush objects. You can create a SolidBrush yourself and pass a Color to the constructor, e.g.
1
Dim myBrush As New SolidBrush(myPictureBox.BackColor)

Thank you very much I can probably work with that :)


2. First of all you wouldn't use that outdated form of I/O. This is .NET so use .NET, i.e. members of the System.IO namespace, e.g. FileStream or StreamWriter.
I have no idea what you just said :numbness:

The best way to save the Color value depends on what sort of file you're using. One simple option would be to call ToArgb on the Color to get a single Integer representing the colour and storing that in the file. When you get the value back from the file later, you can call Color.FromArgb and pass the Integer to get a Color value back.

Thank you I have seen that argb thing somewhere I can probably work it out :)

Thanks for the help.
 

grags

Active member
Joined
Mar 26, 2005
Messages
26
Programming Experience
Beginner
VB.NET:
    Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
        If Sav.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        FileOpen(1, Sav.FileName, OpenMode.Output)
        For Z = 1 To 10
            For Y = 1 To 13
                For X = 1 To 15
                    Write(1, RoomName(X, Y, Z))
                    Write(1, RoomDescription(X, Y, Z))
                    Write(1, RoomCol(X, Y, Z).ToArgb) 'Error :(
                Next
            Next
        Next
        FileClose()

The line Write(1, RoomCol(X, Y, Z).ToArgb) Throws up an error. I've spent quite some time trying to figure this one out, but am at a loss. Remember my knowledge on .net is terrible I know NOTHING.


EDIT: I guess I should mention that RoomCol(,,) was dimmed as a Color at the beginning of the form (Dim RoomCol(15, 13, 10) As Color)



EDIT2: This is my code (With errors off course) for loading...

VB.NET:
 Private Sub mnuLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLoad.Click
        If Ope.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        FileOpen(1, Ope.FileName, OpenMode.Input)

        For Z = 1 To 10
            For Y = 1 To 13
                For X = 1 To 15
                    Input(1, RoomName(X, Y, Z))
                    Input(1, RoomDescription(X, Y, Z))
                    Input(1, RoomCol(X, Y, Z).FromArgb) 'Overload resolution failed because no accessible 'FromArgb' accepts this number of arguments.
                Next
            Next
        Next
        FileClose()
    End Sub

As always any help will be greatly appreciated.
 
Last edited:
Top Bottom