Skip to main content

How to Create a Custom Command Line program in Visual Basic


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...

  1. Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
  2. If (e.KeyChar = ChrW(Keys.Enter)) Then
  3. processCommand(TextBox1.Text)
  4. End If
  5. End Sub

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.

  1. Private Function processCommand(ByVal s As String)
  2. Dim ss As String = s.ToLower()
  3. Dim successfulCommand As Boolean = False
  4. If (ss.StartsWith("output ")) Then
  5. MsgBox(s.Substring(7, s.Count() - 7))
  6. successfulCommand = True
  7. ElseIf (ss.StartsWith("open ")) Then
  8. Diagnostics.Process.Start(ss.Substring(5, ss.Count() - 5))
  9. successfulCommand = True
  10. End If
  11. If (successfulCommand) Then
  12. addCommand(s)
  13. Else : addCommand("Failed: " & s)
  14. End If
  15. End Function

Step 4:
Finally we want to create our last custom function which simply adds output to the listbox.

  1. Private Function addCommand(ByVal s As String)
  2. ListBox1.Items.Add(s)
  3. End Function

Project Complete!
Below is the full source code and download to the project files.

  1. Public Class Form1
  2. Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
  3. If (e.KeyChar = ChrW(Keys.Enter)) Then
  4. processCommand(TextBox1.Text)
  5. End If
  6. End Sub
  7.  
  8. Private Function processCommand(ByVal s As String)
  9. Dim ss As String = s.ToLower()
  10. Dim successfulCommand As Boolean = False
  11. If (ss.StartsWith("output ")) Then
  12. MsgBox(s.Substring(7, s.Count() - 7))
  13. successfulCommand = True
  14. ElseIf (ss.StartsWith("open ")) Then
  15. Diagnostics.Process.Start(ss.Substring(5, ss.Count() - 5))
  16. successfulCommand = True
  17. End If
  18. If (successfulCommand) Then
  19. addCommand(s)
  20. Else : addCommand("Failed: " & s)
  21. End If
  22. End Function
  23.  
  24. Private Function addCommand(ByVal s As String)
  25. ListBox1.Items.Add(s)
  26. End Function
  27. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.