• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Custom Command Line program in Visual Basic

Encrypt4d2

Social Impact Manager
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
81
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 700 XP
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. Private

    Function

    processCommand(ByVal

    s As

    String

    )
  8. Dim

    ss As

    String

    = s.ToLower()
  9. Dim

    successfulCommand As

    Boolean

    = False
  10. If

    (ss.StartsWith("output "

    )) Then
  11. MsgBox(s.Substring(7, s.Count() - 7))
  12. successfulCommand = True
  13. ElseIf

    (ss.StartsWith("open "

    )) Then
  14. Diagnostics.Process.Start(ss.Substring(5, ss.Count() - 5))
  15. successfulCommand = True
  16. End

    If
  17. If

    (successfulCommand) Then
  18. addCommand(s)
  19. Else

    : addCommand("Failed: "

    & s)
  20. End

    If
  21. End

    Function

  22. Private

    Function

    addCommand(ByVal

    s As

    String

    )
  23. ListBox1.Items.Add(s)
  24. End

    Function
  25. End

    Class


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top