How to reset Windows Application's default programmatically?

G

Guest

Hi Everyone,

I have made a windows application using vb.net 2003. When user opens this
application it will display a default location in a textbox. This
application has to allow user to change the default location and set their
desired location as application’s new default. For instance, if the
application’s default location is Chicago, every time user launches this
program Chicago will be displayed in the textbox. If user changes the
default location to Toronto, then click a button to set Toronto as default.
Later when user opens this application again, it should display Toronto
instead of Chicago. How can I achieve this? Please help.

Thank you in advance.

Nina
 
I

IdleBrain

Hey Nina,
You might want to use windows registry to do this.
Use SaveSetting() to store the setting into registry and GetSetting to
retreive it back.

Use SaveSetting("ApplicationName", "FolderName", "City", "Chicago") to
store after user selects a different city.

Use txtboxCityName.text = GetSetting("ApplicationName", "FolderName",
"City", "DefaultValue") when you load the form to fill up the textbox

Hope it helps.
 
R

rowe_newsgroups

Another option is to use XML files. Check out the XML document object
model. Be warned that this approach is more difficult to set up and
requires more code to use. Just throwing in my 2 cents :)

Thanks,

Seth Rowe
 
I

Izzy

I agree with rowe_newsgroups, XML is how I would do it.

Using XML isn't hard either. I use the code below to do exactly this.

Private Sub LoadSettings()

Dim dsSettings as new Dataset
Dim dtSettings as Datatable
Dim SettingsPath As String = Application.StartupPath & "\" &
My.Settings.ServerSettingsFile

dsSettings.DataSetName = "ServerSettings"

If IO.File.Exists(SettingsPath) Then
dsSettings.ReadXml(SettingsPath)
dtSettings = dsSettings.Tables("HardDriveSettings")

Else

Dim dcSN As New DataColumn 'Server Name
Dim dcDTM As New DataColumn 'Drive to monitor.
Dim dcFSTH As New DataColumn 'Free Space Threshhold in GBs
Dim drSettings As DataRow

dcSN.ColumnName = "ServerName"
dcSN.DataType = GetType(System.String)
dcDTM.ColumnName = "DriveLeter"
dcDTM.DataType = GetType(System.String)
dcFSTH.ColumnName = "FreeSpaceThreshHold"
dcFSTH.DataType = GetType(System.Int32)

dtSettings = New DataTable
dtSettings.TableName = "HardDriveSettings"

dtSettings.Columns.Add(dcSN)
dtSettings.Columns.Add(dcDTM)
dtSettings.Columns.Add(dcFSTH)

drSettings = dtSettings.NewRow
drSettings(0) = "stc-dc"
drSettings(1) = "D:"
drSettings(2) = 15
dtSettings.Rows.Add(drSettings)

End If

End Sub

In the above example I created a settings file for a program which
monitors hard drive space on production servers. Using XML to store the
list of servers to be monitored along with the dirve letter and free
space setting.

In your case after you load the settings using dsSettings.ReadXml(Path)
and after your users make a change you would then call
dsSettings.WriteXml(Path) to write the change to disk.

Izzy
 
I

Izzy

I agree with rowe_newsgroups, XML is how I would do it.

Using XML isn't hard either. I use the code below to do exactly this.

Private Sub LoadSettings()

Dim dsSettings as new Dataset
Dim dtSettings as Datatable
Dim SettingsPath As String = Application.StartupPath & "\" &
My.Settings.ServerSettingsFile

dsSettings.DataSetName = "ServerSettings"

If IO.File.Exists(SettingsPath) Then
dsSettings.ReadXml(SettingsPath)
dtSettings = dsSettings.Tables("HardDriveSettings")

Else

Dim dcSN As New DataColumn 'Server Name
Dim dcDTM As New DataColumn 'Drive to monitor.
Dim dcFSTH As New DataColumn 'Free Space Threshhold in GBs
Dim drSettings As DataRow

dcSN.ColumnName = "ServerName"
dcSN.DataType = GetType(System.String)
dcDTM.ColumnName = "DriveLeter"
dcDTM.DataType = GetType(System.String)
dcFSTH.ColumnName = "FreeSpaceThreshHold"
dcFSTH.DataType = GetType(System.Int32)

dtSettings = New DataTable
dtSettings.TableName = "HardDriveSettings"

dtSettings.Columns.Add(dcSN)
dtSettings.Columns.Add(dcDTM)
dtSettings.Columns.Add(dcFSTH)

drSettings = dtSettings.NewRow
drSettings(0) = "stc-dc"
drSettings(1) = "D:"
drSettings(2) = 15
dtSettings.Rows.Add(drSettings)

End If

End Sub

In the above example I created a settings file for a program which
monitors hard drive space on production servers. Using XML to store the
list of servers to be monitored along with the dirve letter and free
space setting.

In your case after you load the settings using dsSettings.ReadXml(Path)
and after your users make a change you would then call
dsSettings.WriteXml(Path) to write the change to disk.

Izzy
 
G

Guest

Thank you IdleBrain, rowe_newsgroups, and Izzy for all your help. Now I know
where to start. Have a nice day!

Nina
 
G

GhostInAK

Hello Izzy,

I don't like this approach. Datasets dont feel right for storing application
settings. A custom object would be a better approach. It feels better.
It also allows your settings to take advantage of strongly typed data, and
intellisense within the IDE.

-Boo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top