Question Loading back and fore color by name from a .txt file problem!

tylersuehr7

Member
Joined
Nov 24, 2013
Messages
9
Programming Experience
1-3
Hello,
I am having a minor problem with my code. Basically what it does is allows you to change the color (using the "colordialog" code) of the form backcolor and forecolor, and also writes the name of that color to a .txt file so when you exit the form, it saves the colors you chose. When the user activates the form again it reads the .txt file (which contains the name of the color previously selected) and changes the back/fore color to them. It works for the most part except custom colors that give a hexdecimal value (ff10e391) for the color name instead of a normal name such as green, blue, red, whitesmoke, lightgrey... etc. How can I get the form to change its back/fore color to colors that do not have a name and is refered to as a hexdecimal value?? I'll post my code with what each thing does below. Thanks!!

Imports System.IO

Public Class MainConsole
Dim colorChanger As New ColorDialog
Dim backSelectedColor, fontSelectedColor As Object
Dim backColorPath, fontColorPath As String

Private Sub MainConsole_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'<TEXT FILE PATHS>
backColorPath = (Application.StartupPath & "\assets\textFiles\backcolor.txt")
fontColorPath = (Application.StartupPath & "\assets\textFiles\fontcolor.txt")
'</TEXT FIE PATHS>

'<BACK COLOR READER>
Dim dk As New StreamReader(backColorPath)
backSelectedColor = dk.ReadLine()
dk.Close()
'</BACK COLOR READER>

'<FONT COLOR READER>
Dim sk As New StreamReader(fontColorPath)
fontSelectedColor = sk.ReadLine()
sk.Close()
'</FONT COLOR READER>

'<SETS FORM COLORS ON LOAD>
Me.BackColor = System.Drawing.Color.FromName(backSelectedColor)
Me.ForeColor = System.Drawing.Color.FromName(fontSelectedColor)
'</SETS FORM COLORS ON LOAD>

'<SETS BACK COLOR OF BUTTON OF PROPERTIESBAR>
If (backSelectedColor = "Whitesmoke") Then
BackColorProperties.BackColor = Color.Transparent
End If
If (fontSelectedColor = "Black") Then
FontColorProperties.BackColor = Color.Transparent
End If
'</SETS BACK COLOR OF BUTTON OF PROPERTIESBAR>

PropertiesBar.Hide()
End Sub

'<PROPERTIES BAR SELECTOR>
Private Sub PropertiesMainMenuItem_Click(sender As Object, e As EventArgs) Handles PropertiesMainMenuItem.Click
If (PropertiesMainMenuItem.Text = "Properties") Then
PropertiesMainMenuItem.Text = "Hide Properties"
PropertiesBar.Show()
Return
End If
If (PropertiesMainMenuItem.Text = "Hide Properties") Then
PropertiesMainMenuItem.Text = "Properties"
PropertiesBar.Hide()
Return
End If
End Sub
'</PROPERTIES BAR LOOP>

'<MAIN CONSOLE BACKGROUND COLOR>
Private Sub BackColorProperties_Click(sender As Object, e As EventArgs) Handles BackColorProperties.Click
If (colorChanger.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
backSelectedColor = colorChanger.Color
Me.BackColor = backSelectedColor
End If
Dim fj As New StreamWriter(backColorPath)
fj.Write(colorChanger.Color.Name)
fj.Close()
End Sub
'</MAIN CONSOLE BACKGROUND COLOR>

'<MAIN CONSOLE FONT COLOR>
Private Sub FontColorProperties_Click(sender As Object, e As EventArgs) Handles FontColorProperties.Click
If (colorChanger.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
fontSelectedColor = colorChanger.Color
Me.ForeColor = fontSelectedColor
Dim fj As New StreamWriter(fontColorPath)
fj.Write(colorChanger.Color.Name)
fj.Close()
End If
End Sub
'</MAIN CONSOLE FONT COLOR>

End Class
 
Last edited:
I would bind ForeColor/BackColor properties to each their application user setting, and get rid of mostly all that code. To do this select the form in designer and go to (ApplicationSettings) property, in (PropertyBinding) dialog you can find those properties and assign them to a new setting.
 
Back
Top