none
How do you save ColorDialog Custom Color? RRS feed

  • Question

  • Hi all,

    I am not sure if this falls under VB, but the code is.

    How do you save Custom Color?

    In my program, user selects a cell, then click the "Color" button, which opens the "Window Color Pallet".
    User should then be able to save a custom color.


    (I think only the BOLD code matters. The rest is just saving the color code in textfile)

    'Create a Color Pallet Popup window - change color of a Cell
        'Save into a Textfile
        Private Sub color_Btn_Click(sender As Object, e As EventArgs) Handles color_Btn.Click
    
            Dim clrDialog As New ColorDialog
            Dim ID As Integer                       'Store row ID value
            Dim colIndex As Integer                 'Store Column
            Dim colorString As String               'Store Color Code
    
            'Change Cell BG Color
           If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color      'Change Cell Color
                colorString = clrDialog.Color.ToArgb.ToString                                       'Get Color Code
            End If
            clrDialog.Dispose()
    
            '===============================================================================================================
            'Loop through the DataGridView, Get rowID,columnIndex,color.ToARGB, overwrite into Textfile
            Dim i As Integer = 0        'Counter for Rows
            Dim j As Integer = 0        'Counter for Columns
    
            'Prepare and open the Textfile
            Dim StringToFile As String = ""
            Dim filePath As String = "..\..\..\config\cellColor.txt"
            Dim filenum As Integer
            filenum = FreeFile()
            FileOpen(filenum, filePath, OpenMode.Output)        'Open the File
    
            For i = 0 To ProjectsDataGridView.RowCount - 1
                For j = 0 To ProjectsDataGridView.ColumnCount - 1
                    'If backcolor is NOT White (NOT 0 or -1)
                    If (ProjectsDataGridView.Rows(i).Cells(j).Style.BackColor.ToArgb.ToString <> "0") And (ProjectsDataGridView.Rows(i).Cells(j).Style.BackColor.ToArgb.ToString <> "-1") Then
    
                        'This Cell is highlighted
                        ID = ProjectsDataGridView.Rows(i).Cells(0).Value                                        'Get ID number
                        colIndex = j                                                                            'Get Column Index
                        colorString = ProjectsDataGridView.Rows(i).Cells(j).Style.BackColor.ToArgb.ToString()   'Get Color Code
    
                        'System.Windows.Forms.MessageBox.Show(ID.ToString & " Column= " & colIndex.ToString & " color=" & colorString)
    
                        '===============================================================================================================
                        'Overwrite to File (ID;colIndex;colorString)
                        Try
                            StringToFile &= ID.ToString & ";" & colIndex.ToString & ";" & colorString & vbCrLf    'each loop appends StringToFile
    
                        Catch ex As Exception
                            MessageBox.Show("File Error Occurred!" + vbCrLf + ex.ToString)
                        End Try
                        '===============================================================================================================
    
                    End If
                Next
            Next
            MessageBox.Show("Cell Color Saved!")
            Print(filenum, StringToFile)
            FileClose(filenum)
    
        End Sub


    User save a custom Color.


    But when user click "Color" button again, the Custom Color is not saved.

    Sunday, May 26, 2019 9:36 AM

Answers

  • Hi,

    I know where your issue is,try to set clrDialog as a global variable,like this:

          Dim clrDialog As New ColorDialog
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim ID As Integer
            Dim colIndex As Integer
            Dim colorString As String
    
            If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color      'Change Cell Color
                colorString = clrDialog.Color.ToArgb.ToString
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wk89 Sunday, June 2, 2019 10:22 AM
    Monday, May 27, 2019 6:45 AM
    Moderator

All replies

  • Hi wk89,

    Thank you for posting here.

    According to your description, your issue is more related to visual basic development. Since our forum is discussing about test tools in VS IDE, so we will move this post to Visual Basic forum for better support.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, May 27, 2019 2:23 AM
  • See MSDN : ColorDialog.CustomColors

    For example =>

    ' Global
    Dim customColors() As Integer = {&H91EF6, &H85EF6, &H89EF6, &H8DFF6, &H8F6CC, &H8F68C, &H8F64B, &H8F60B, &H46F608, &H86F608, &HC7F608, &HF6E508, &HF6A408, &HF66308, &HF62208, &HF7072E}
    
    
    Dim MyDialog = New ColorDialog()       
    MyDialog.AllowFullOpen = True
    MyDialog.CustomColors = customColors       
    MyDialog.ShowHelp = True
    MyDialog.Color = Me.BackColor
    MyDialog.ShowDialog()
    Me.BackColor = MyDialog.Color
    customColors = MyDialog.CustomColors

    Monday, May 27, 2019 6:40 AM
  • Hi,

    I know where your issue is,try to set clrDialog as a global variable,like this:

          Dim clrDialog As New ColorDialog
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim ID As Integer
            Dim colIndex As Integer
            Dim colorString As String
    
            If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                ProjectsDataGridView.CurrentCell.Style.BackColor = clrDialog.Color      'Change Cell Color
                colorString = clrDialog.Color.ToArgb.ToString
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wk89 Sunday, June 2, 2019 10:22 AM
    Monday, May 27, 2019 6:45 AM
    Moderator