How do I save the backcolor of a textbox to file?
Ive looked everywhere I could, but didn't find anything that worked for me!
Any help much appreciated!
Printable View
How do I save the backcolor of a textbox to file?
Ive looked everywhere I could, but didn't find anything that worked for me!
Any help much appreciated!
To what kind of file? You could let .NET handle it all for you by using the DynamicProperties bit at designtime. Otherwise I'd suggest writing either the RGB values or the name if you are using NamedColors then use the Parse or New methods to recreate the color from said values.
if you want the value in either Integer or RGB you can do these :
VB Code:
'///////// for the Integer version... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim coltrans As Integer = ColorTranslator.ToWin32(TextBox1.BackColor) Dim colorfile As IO.StreamWriter = New IO.StreamWriter(New IO.FileStream("D:\textboxcolor.txt", IO.FileMode.OpenOrCreate)) colorfile.WriteLine(coltrans) colorfile.Close() End Sub '///////// for the RGB version .... Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim coltrans As String = TextBox1.BackColor.R & TextBox1.BackColor.G & TextBox1.BackColor.B Dim colorfile As IO.StreamWriter = New IO.StreamWriter(New IO.FileStream("D:\textboxcolor.txt", IO.FileMode.OpenOrCreate)) colorfile.WriteLine(coltrans) colorfile.Close() End Sub
Or serialize the color obj .
Dynamic_sysop,
Thats works great but how do I read the value from the file to apply it back to the object(textbox)?
VB Code:
Dim sr As New IO.StreamReader("c:\blah.txt") 'This variable will hold color value , btw , you have more options for reading 'files . check them out . Dim ColStr As String = sr.Read()
assuming you've saved as integer :
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sReader As New IO.StreamReader(New IO.FileStream("D:\textboxcolor.txt", IO.FileMode.OpenOrCreate)) Dim col As ColorTranslator TextBox1.BackColor = col.FromWin32(sReader.ReadLine) sReader.Close() End Sub
Thanks it works great!:D
It sooo easy after you see the code!
Makes ya think I should've figured that one out!:confused: