-
Aug 28th, 2013, 02:42 PM
#1
Thread Starter
Fanatic Member
Create Custom Control Settings File
In this simple example we will write the properties of form controls to a file.
Assume that we have a form with only four controls. Two checkboxes and two buttons. Buttons will be enabling/disabling checheckboxes. And we will read/write their settings to a "settings.ini" file.
First, we'll read the settings on form load. A one-dimensional array for parsing settings will need a shorter code, however a two-dimensional array is a good way to keep things neat. In my example, I used two-dimensinal array "settings". First dimension will be reserved for the control and second dimension will be reserved for that control's property.
Code:
Imports System.IO 'Needed for read and write files.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' First create our settings folder if it doesn't exist.
If (Not System.IO.Directory.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & _
My.Application.Info.AssemblyName)) Then
System.IO.Directory.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & _
My.Application.Info.AssemblyName)
End If
'Parse the filename. If the file doesn't exist then, noproblem. It'll be a null referance.
Dim filename As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & _
My.Application.Info.AssemblyName & "\settings.ini" 'You may modify this line if you are NOT going to use user Documents path.
'If you don't have the file, it will load the controls with default values.
Dim lines() As String 'This array will keep the lines from file.
Dim settings(3, 1) As String 'Our main settings array.
'Read the settings if the file exists
If File.Exists(filename) Then
lines = File.ReadAllLines(filename).Where(Function(s) s.Trim() <> String.Empty).ToArray() 'The additional function after "ReadAllLines" skips empty lines.
Dim iii As Integer = 0' This is a very conventional method that I found for parsing settings. You may write a better method.
For i = 0 To 3
For ii = 0 To 1
settings(i, ii) = lines(iii).Split(" ").Last() 'We get the last words of eachline which are actually property value.
iii += 1
Next
Next
'Set the properties
Me.CheckBox1.Checked = settings(0, 0) 'First dimansion value "0" is reserved for CheckBox1
Me.CheckBox1.Enabled = settings(0, 1)
Me.CheckBox2.Checked = settings(1, 0) 'First dimansion value "1" is reserved for CheckBox2
Me.CheckBox2.Enabled = settings(1, 1)
Me.Button1.Text = settings(2, 0) 'First dimansion value "2" is reserved for Button1
Me.Button1.Enabled = settings(2, 1)
Me.Button2.Text = settings(3, 0) 'First dimansion value "3" is reserved for Button2
Me.Button2.Enabled = settings(3, 1)
End If
End Sub
Here, we'll do some stuff. Let the buttons enable and disable the checkboxes and change their own texts:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case Me.CheckBox1.Enabled
Case True
Me.CheckBox1.Enabled = False
Button1.Text = "Enable"
Case False
Me.CheckBox1.Enabled = True
Button1.Text = "Disable"
End Select
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Select Case Me.CheckBox2.Enabled
Case True
Me.CheckBox2.Enabled = False
Me.Button2.Text = "Enable"
Case False
Me.CheckBox2.Enabled = True
Me.Button2.Text = "Disable"
End Select
End Sub
Now write the settings to our "settings.ini" file with StreamWriter on form close:
Code:
Private Sub Form1_FormClosing1(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filename As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & _
My.Application.Info.AssemblyName & "\settings.ini" ' You don't have to have a ready file in the directory. It creates a "setup.ini" file each time.
Dim settings(3, 1) As String
'Retrive the properties
settings(0, 0) = Me.CheckBox1.Checked.ToString
settings(0, 1) = Me.CheckBox1.Enabled.ToString
settings(1, 0) = Me.CheckBox2.Checked.ToString
settings(1, 1) = Me.CheckBox2.Enabled.ToString
settings(2, 0) = Me.Button1.Text.ToString
settings(2, 1) = Me.Button1.Enabled.ToString
settings(3, 0) = Me.Button2.Text.ToString
settings(3, 1) = Me.Button2.Enabled.ToString
'Write to file
Dim objWriter As New System.IO.StreamWriter(filename)
objWriter.Write("CheckBox1.Checked: " & settings(0, 0) & vbCrLf & _
"CheckBox1.Enabled: " & settings(0, 1) & vbCrLf & _
"CheckBox2.Checked: " & settings(1, 0) & vbCrLf & _
"CheckBox2.Enabled: " & settings(1, 1) & vbCrLf & _
"Button1.Text: " & settings(2, 0) & vbCrLf & _
"Button1.Enabled: " & settings(2, 1) & vbCrLf & _
"Button2.Text: " & settings(3, 0) & vbCrLf & _
"Button2.Enabled: " & settings(3, 1))
objWriter.Close()
End Sub
End Class
And it's all done!
The settings file will look like:
CheckBox1.Checked: False
CheckBox1.Enabled: True
CheckBox2.Checked: False
CheckBox2.Enabled: True
Button1.Text: Disable
Button1.Enabled: True
Button2.Text: Disable
Button2.Enabled: True
In this example, "Enabled" property for the buttons remains unchanged. It may stay like that. I added them just for case.
Simply you may develop this code with control collections and better For loops. For instance, if you know that you are going to assign each value "1" of second dimension as controls' "Enabled" property, then you may try something like that:
Code:
Dim i As Integer = 0
Dim Objects() As Object = {Me.CheckBox1, Me.CheckBox2, Me.Button1, Me.Button2}
For Each Control In Objects
Control.Enabled = settings(i, 1) 'Here "1" is assigned for "Enabled" property of each control.
i+=1
Next
And then, other fixed dimension values for other properties...
I hope this will be useful guide. Good day
Last edited by Flashbond; Sep 11th, 2013 at 04:05 PM.
Reason: Fixed "control" spelling
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Aug 28th, 2013, 02:54 PM
#2
Re: Read/Write Conrol Settings to File
Orrr.... you create a User Setting and then databind the Enabled property (or what ever property) of the CheckBoxes (or which ever control(s) you want) to that setting. No parsing needed, and it's built-in. Everything gets stored in the USer.Config file.
It also helps avoid this:
Code:
Dim filename As String = AppDomain.CurrentDomain.BaseDirectory & "\settings.ini" 'You may modify this line if you are NOT going to use the same path with exe.
Because almost certainly it'll need to be in a different location in order to write to the file. the User.Config avoids that being in a "safe" place to begin with.
-tg
-
Aug 28th, 2013, 03:13 PM
#3
Thread Starter
Fanatic Member
Re: Read/Write Conrol Settings to File
Oww! I didn't know that. How to create that "User Setting"?
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Aug 28th, 2013, 04:31 PM
#4
Re: Read/Write Conrol Settings to File
Right click on your Project in the solution explorer... select "Properties" ... then go to the Settings tab...
Alternatively from the control itself, you can click on "Application Bindings" in the properties window, find "Property Bindings" sub item, and click the elipse button.... left side will hold the control properties... the right will allow you to create/select the setting to bind that property to.
-tg
-
Aug 28th, 2013, 04:44 PM
#5
Thread Starter
Fanatic Member
Re: Read/Write Conrol Settings to File
I can't believe that I didn't know such a simple thing! I couldn't manage the first thing but did the second one. This was cool!
EDIT: One last thing, where is the setting file? Can I change that directory later?
Last edited by Flashbond; Aug 28th, 2013 at 04:48 PM.
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Aug 28th, 2013, 07:48 PM
#6
Re: Create Custom Conrol Settings File
No... you don't... tha'ts the point of it ... it will store it some place unique to that user (or application) and it will store it in a place where the app will have proper access to the file - some folders are write-protected, or have restricted access (such as Program Files except for installation).
-tg
-
Aug 28th, 2013, 07:59 PM
#7
Thread Starter
Fanatic Member
Re: Create Custom Conrol Settings File
OK, got it. Thanks a lot for the tip. I didn't like that can't changing path. Most of the programs keep their settings file in Documents. The relative path may be replaced with default user's documents.
Anyway, since VS has a feature for this purpose, I changed my title to "custom".
Thanks again.
Last edited by Flashbond; Aug 28th, 2013 at 08:38 PM.
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Sep 14th, 2013, 01:30 AM
#8
Junior Member
Re: Create Custom Control Settings File
I did something similar but wrote to the registry in currentuser...
I did come across issues with enable/disable properties of controls that had child controls.. tab pages, numeric up/downs, and the like, so I had to skip them which was a pain
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|