Results 1 to 4 of 4

Thread: How do you save ColorDialog Custom Color?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    69

    How do you save ColorDialog Custom Color?

    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)
    Code:
    '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.
    Attachment 168785

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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How do you save ColorDialog Custom Color?

    Those attachments appear to be bad links.

    Have you looked at the text files to see that the values are there? If so, then you are right that it is saving.

    I see that you intend that the user can set colors for different cells, and all of the colors should save. You may be doing a bit more than you need to, since you save every cell in the DGV every time you change the color for any cell in the DGV, but that's only a question of performance. If it works fine, then it works. If it is slow, then there is likely a better fashion.

    In any case, the first place I'd be looking is in the file. Is the data right? If it is, then there are two possible issues that I can think of:

    1) The file is not being read back in. I'm not even sure if this applies, because you didn't actually state that the problem was retaining the colors between runs of the program.

    2) Something else in the code is resetting the colors at some point around clicking the button. The attachments were likely intended to show the problem, but since they appear broken to me, they didn't help any.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    69

    Re: How do you save ColorDialog Custom Color?

    Quote Originally Posted by Shaggy Hiker View Post
    Those attachments appear to be bad links.

    Have you looked at the text files to see that the values are there? If so, then you are right that it is saving.

    I see that you intend that the user can set colors for different cells, and all of the colors should save. You may be doing a bit more than you need to, since you save every cell in the DGV every time you change the color for any cell in the DGV, but that's only a question of performance. If it works fine, then it works. If it is slow, then there is likely a better fashion.

    In any case, the first place I'd be looking is in the file. Is the data right? If it is, then there are two possible issues that I can think of:

    1) The file is not being read back in. I'm not even sure if this applies, because you didn't actually state that the problem was retaining the colors between runs of the program.

    2) Something else in the code is resetting the colors at some point around clicking the button. The attachments were likely intended to show the problem, but since they appear broken to me, they didn't help any.

    Hi, I have attached the images again.
    I think you will understand exactly what I mean by "save custom color" when you see the images.

    Name:  Custom Color 1.JPG
Views: 636
Size:  51.4 KB

    When user click "color" again, the saved custom color is gone.
    Name:  Custom Color 2.JPG
Views: 640
Size:  32.9 KB

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How do you save ColorDialog Custom Color?

    Hi,

    you will need to create an application setting called CustomColours with the type set to string.

    this isn't my Code, see Link where I found it
    Code:
     Private Sub Save_CustomColours()
            'Source found at : http://vbcity.com/forums/t/166302.aspx
    
            'Save Custom Colours to an application setting.  
            If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim CustomColours() As Integer = ColorDialog1.CustomColors
    
                'Save the Integer representation of each custom colour in a single string joined by commas  
                My.Settings.CustomColours = String.Join(",", Array.ConvertAll(CustomColours, New Converter(Of Integer, String)(Function(i) i.ToString)))
            End If
    
        End Sub
    
        Private Sub Load_CustomColours()
            'Check the Application Setting contains a valid value before reading it into an array  
            If String.IsNullOrEmpty(My.Settings.CustomColours) OrElse Not My.Settings.CustomColours.Contains(","c) Then Return
    
            'The below line will read the comma delimited string from the application setting, split it into an array, and then convert the array to integers  
            Dim CustomColours() As Integer = Array.ConvertAll(My.Settings.CustomColours.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries), AddressOf StringToInteger)
    
            'Set the custom colours to those that were saved in an application setting  
            ColorDialog1.CustomColors = CustomColours
            ColorDialog1.ShowDialog()
    
        End Sub
    
        'This function is required to convert all the elements of an array from one type to another  
        Private Function StringToInteger(ByVal S As String) As Integer
            '16777215 appears to be the default value for undefined custom colours (I guess that it might be the integer value for White)  
            If Integer.TryParse(S, Nothing) Then Return CInt(S) Else Return 16777215
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Save_CustomColours()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Load_CustomColours()
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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