Results 1 to 5 of 5

Thread: How to get color value from colordialog

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    How to get color value from colordialog

    I am very new in Vb.NET ..
    I need to save color value to a table, How to get the value of color?
    Dim MyDialog As New ColorDialog
    ' Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = False
    ' Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = True
    ' Sets the initial color select to the current text color,
    MyDialog.Color = LblPreviewColor.ForeColor

    ' Update the text box color if the user clicks OK
    If (MyDialog.ShowDialog() = DialogResult.OK) Then
    LblPreviewColor.ForeColor = MyDialog.Color

    ' How to get the color value??

    End If

    I need advice .. thanks

    Winanjaya

  2. #2
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    Hi

    This works for me, just modify it to your needs.

    Code:
      'Open the color dialog control
            With ColorDialog
                'Get the current color from the picturebox
                .Color = Me.picCropColor.BackColor
                If .ShowDialog = DialogResult.OK Then
    
                    Try
                        'Place the color in the picbox
                        Me.picCropColor.BackColor = ColorDialog.Color
                        Dim colour As Integer = ColorTranslator.ToWin32(ColorDialog.Color)
    
                        Me.lblColor.Text = colour.ToString
    
                    Catch
                        Dim Msgbox As MessageBox
                        Msgbox.Show("Error! " & vbCrLf & "An error has occured while trying to set the color to the controls", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        'Now exit
                        End
    
                    End Try
                End If
            End With

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    Thanks.

    Dim colour As Integer = ColorTranslator.ToWin32(ColorDialog.Color)
    this will return a long type variable and then I save it to a table..
    but when I recall it from table, how to implement it to "lblColor.ForeColor"

    I need advice .. many thanks in advance

    Regards
    Winanjaya

  4. #4
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    Hi

    To retrieve the colour and set a label or column in a listview you do:

    Code:
    'Retrieve the color from the stored integer value
       Dim colour As ColorTranslator
       osItem.BackColor = colour.FromWin32(CInt(drv("GrainColour").ToString))
    This is for a listview but you would replace osItem.BackColor with Label1.BackColor or whatever. I've stored the integer value in the field drv("GrainColour")

    You'll have to change that to your setup.
    Last edited by Wallabie; May 17th, 2004 at 05:45 AM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    RESOLVED!!!

    Thanks ....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width