How do you save ColorDialog Custom Color?

  • Thread starter Thread starter wk89
  • Start date Start date
W

wk89

Guest
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.
1443125.jpg

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

Continue reading...
 
Back
Top