Config Creator in Visual Basic

Introduction: This tutorial is on how to create a config creator in Visual Basic. Imports: We need to import the System.IO namespace in order to access files within the computers filesystem for saving later on...
  1. Imports System.IO
Design: Our design will simple consist of; Textbox, textbox1, Hold the property name. Textbox, textbox2, Hold the property value. Button, button1, Add property. Button, button2, Save config. Variables: To hold all the current properties to write to the config file in the near future, we will create a String list within the global scope...
  1. Dim props As New List(Of String)
Button1 Click Event: Once button1 is clicked, we first want to ensure that both the property name and value boxes are not empty...
  1. If (Not TextBox1.Text = Nothing And Not TextBox2.Text = Nothing) Then
  2.  
  3. Else
  4.  
  5. End If
If they are both not nothing, we want to add the values using a delimiter of a colon (:) to separate the name from the value, to the list...
  1. props.Add(TextBox1.Text + ":" + TextBox2.Text)
Button2 Click Event: Once button2 is clicked, we want to allow the user to set a save destination through a new SaveFileDialog browser box, then use a new StreamWriter to write each property held within our 'props' list variable/object to the new config file...
  1. If (props.Count() > 0) Then
  2.         Using fo As New OpenFileDialog
  3.                 fo.MultiSelect = False
  4.                 fo.RestoreDirectory = True
  5.                 fo.ShowDialog()
  6.                 If (fo.FileName = Nothing) Then
  7.                         MsgBox("That config name is not valid.")
  8.                 Else
  9.                         Using sw As New StreamWriter(fo.FileName)
  10.                                 For Each prop As String In props
  11.                                         sw.WriteLine(prop)
  12.                                 Next
  13.                         End Using
  14.                 End If
  15.         End Using
  16. End If

Add new comment