Solved

Save color in a txt file from a ColorDialog?

Posted on 2004-09-21
2
272 Views
Last Modified: 2010-04-23
like the topic says how do i do this?
0
Comment
Question by:stromberg10
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 12114960
Here is a simple example of a form that stores its user customizable backcolor in a text file and restores it on form load.  Just double click the form to select a color.  Then close the form and reopen it.  The form should still be the same color.

Regards,

Idle_Mind

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sp As String = Application.StartupPath
        If Not sp.EndsWith("\") Then
            sp = sp & "\"
        End If
        Dim fileName As String = sp & "myColor.txt"
        If File.Exists(fileName) Then
            Dim fs As StreamReader = New StreamReader(fileName)
            Try
                Dim strColor As String = fs.ReadLine()
                Dim intColor As Integer = Integer.Parse(strColor)
                Me.BackColor = Color.FromArgb(intColor)
            Catch ex As Exception
            End Try
            fs.Close()
        End If
    End Sub

    Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
        If ColorDialog1.ShowDialog = DialogResult.OK Then
            Me.BackColor = ColorDialog1.Color
        End If
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim sp As String = Application.StartupPath
        If Not sp.EndsWith("\") Then
            sp = sp & "\"
        End If
        Dim fs As StreamWriter = New StreamWriter(sp & "myColor.txt")
        fs.WriteLine(Me.BackColor.ToArgb.ToString)
        fs.Close()
    End Sub

End Class
0
 

Author Comment

by:stromberg10
ID: 12115942
thanks, just the answer i was looking for.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now