How to Create a Custom Command Line program in Visual Basic
Submitted by GeePee on Wednesday, March 25, 2015 - 23:19.
Introduction:
Welcome to my tutorial on how to create a Custom Command Line in Visual Basic using a form design.
Steps of Creation:
Step 1:
First we want to create a form with; listbox1 - to contain program output, textbox1 - to contain user input.
Step 2:
Next we want to access teh textbox1 keyPress event. We want to check if the pressed key is the Enter key, if it is we will run a custom function to process the input...
Step 3:
So now we want to make our custom function which will run the input. First we create an 'ss' String variable which is set to the lower case version of the user input. We then create another variable as a Boolean which is set to False by default. This is because we set this to True whenever a comand is ran, this way we can tell if a command was successful or not and output accordingly.
We then want to create custom scripts for each command we want within our custom command line program. I have just done a couple; Output, to output a custom message in a messagebox. And open to open a file directory.
Step 4:
Finally we want to create our last custom function which simply adds output to the listbox.
Project Complete!
Below is the full source code and download to the project files.
- Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
- If (e.KeyChar = ChrW(Keys.Enter)) Then
- processCommand(TextBox1.Text)
- End If
- End Sub
- Private Function processCommand(ByVal s As String)
- Dim ss As String = s.ToLower()
- Dim successfulCommand As Boolean = False
- If (ss.StartsWith("output ")) Then
- MsgBox(s.Substring(7, s.Count() - 7))
- successfulCommand = True
- ElseIf (ss.StartsWith("open ")) Then
- Diagnostics.Process.Start(ss.Substring(5, ss.Count() - 5))
- successfulCommand = True
- End If
- If (successfulCommand) Then
- addCommand(s)
- Else : addCommand("Failed: " & s)
- End If
- End Function
- Private Function addCommand(ByVal s As String)
- ListBox1.Items.Add(s)
- End Function
- Public Class Form1
- Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
- If (e.KeyChar = ChrW(Keys.Enter)) Then
- processCommand(TextBox1.Text)
- End If
- End Sub
- Private Function processCommand(ByVal s As String)
- Dim ss As String = s.ToLower()
- Dim successfulCommand As Boolean = False
- If (ss.StartsWith("output ")) Then
- MsgBox(s.Substring(7, s.Count() - 7))
- successfulCommand = True
- ElseIf (ss.StartsWith("open ")) Then
- Diagnostics.Process.Start(ss.Substring(5, ss.Count() - 5))
- successfulCommand = True
- End If
- If (successfulCommand) Then
- addCommand(s)
- Else : addCommand("Failed: " & s)
- End If
- End Function
- Private Function addCommand(ByVal s As String)
- ListBox1.Items.Add(s)
- End Function
- End Class
Add new comment
- 91 views