如何向自定义命令vb.net添加参数
具有将字符串转换为十六进制的函数。使用文本框和按钮很好。但在我的项目中,有一个控制台窗体,有一个richtext框作为输出,一个textbox作为输入。用户必须键入命令并按enter键才能执行命令 我试着让它像:十六进制“字符串” 以下是inputbox代码的当前状态:如何向自定义命令vb.net添加参数,vb.net,string,parameters,Vb.net,String,Parameters,具有将字符串转换为十六进制的函数。使用文本框和按钮很好。但在我的项目中,有一个控制台窗体,有一个richtext框作为输出,一个textbox作为输入。用户必须键入命令并按enter键才能执行命令 我试着让它像:十六进制“字符串” 以下是inputbox代码的当前状态: Private Sub consoleinputbox_KeyDown(sender As Object, e As KeyEventArgs) Handles consoleinputbox.KeyDown Dim C
Private Sub consoleinputbox_KeyDown(sender As Object, e As KeyEventArgs) Handles consoleinputbox.KeyDown
Dim Command As String = consoleinputbox.Text
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Output(Command)
CallCommand(Command)
End If
End Sub
Private Sub consoleinputbox_KeyDown(sender As Object, e As KeyEventArgs) Handles consoleinputbox.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Dim text As String = consoleinputbox.Text
Dim Command As String()
Command = text.Split(" ")
If Command.Length = 1 Then
text.Trim()
Output(text)
CallCommand(Command(0))
ElseIf Command.Length = 2 Then
Output(text)
CallCommand1(Command(0), Command(1))
End If
End If
End Sub
输出函数正在向richtextbox追加文本。CallCommand函数如下所示:
Public Function CallCommand(s As String) 'seperate commands as func.
Dim ss As String = s.ToLower()
ss = ss.Trim
Select Case ss
Case "cls"
GetRtb.rtb.Clear()
Case "help cls"
GetRtb.rtb.AppendText("Obviusly clear your console screen..." & vbCrLf)
Return True
Case "ping"
GetRtb.rtb.AppendText("insufficient parameters. type help ping for more information..." & vbCrLf)
Return True
Case "help ping"
GetRtb.rtb.AppendText("usage: ping [hostname or ip]" & vbCrLf)
Return True
Case "help"
GetRtb.rtb.AppendText("Avaliable commands:" & vbCrLf)
GetRtb.rtb.AppendText(avaliablecommands(s))
Return True
Case Else
GetRtb.rtb.AppendText("Bad command or filename..." & vbCrLf)
Return False
End Select
End Function
如果consoleinputbox.text有空格和
如果有空格,我如何将第一个单词作为命令和rest作为参数分开?
正如你看到的“ping www.me.com”或“hex asdasd”
谢谢大家! 我认为这个问题已经解决了,但如果我需要更多的参数,我必须生成更多的函数,如Callcommand2等。。。 输入框代码:
Private Sub consoleinputbox_KeyDown(sender As Object, e As KeyEventArgs) Handles consoleinputbox.KeyDown
Dim Command As String = consoleinputbox.Text
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Output(Command)
CallCommand(Command)
End If
End Sub
Private Sub consoleinputbox_KeyDown(sender As Object, e As KeyEventArgs) Handles consoleinputbox.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Dim text As String = consoleinputbox.Text
Dim Command As String()
Command = text.Split(" ")
If Command.Length = 1 Then
text.Trim()
Output(text)
CallCommand(Command(0))
ElseIf Command.Length = 2 Then
Output(text)
CallCommand1(Command(0), Command(1))
End If
End If
End Sub
和模块的其余部分
Public Sub Output(s As String)
If s <> "" Then
GetRtb.rtb.AppendText(s & vbCrLf)
GetTb.tb.Clear()
GetTb.tb.Focus()
End If
End Sub
Public Function CallCommand(c1 As String)
c1.ToLower()
Select Case c1
Case "cls"
GetRtb.rtb.Clear()
Case "help"
Output("Avaliable Commands")
Call avaliablecommands()
Case "hex"
Output("insufficent parameters. use: help hex")
Case "ascii"
Output("insufficent parameters. use: help ascii")
Case Else
Output("Bad command or filename...")
End Select
Return True
End Function
Public Function CallCommand1(c1 As String, p2 As String)
c1.ToLower()
p2.ToLower()
Select Case c1
Case "help"
help_par(p2)
Case "hex"
hex_par(p2)
Case "ascii"
ascii_par(p2)
End Select
Return True
End Function
Function help_par(p As String)
Select Case p
Case "ping"
Output("usage: ping [hostname or ip]")
Case "cls"
Output("obviusly clears you console...")
Case "hex"
Output("converts string to hexcimal code. usage: hex <string>")
Case "ascii"
Output("converts hexcimal code to string. usage: ascii <hexcode>")
End Select
Return True
End Function
Function hex_par(p As String)
Dim sth As String = StringToHex(p)
Output(sth)
Return True
End Function
Function ascii_par(p As String)
Dim tostring As String = HexToString(p)
Output(tostring)
Return True
End Function
Public Sub avaliablecommands()
Output("help, cls, ping, hex, ascii")
End Sub
公共子输出(作为字符串)
如果是“”,那么
GetRtb.rtb.AppendText(s&vbCrLf)
GetTb.tb.Clear()
GetTb.tb.Focus()
如果结束
端接头
公用函数调用命令(c1作为字符串)
c1.ToLower()
选择案例c1
案例“cls”
GetRtb.rtb.Clear()
案例“帮助”
输出(“可用命令”)
调用AvailableCommand()
案例“十六进制”
输出(“参数不足。使用:help hex”)
大小写“ascii”
输出(“不完整的参数。使用:help ascii”)
其他情况
输出(“错误的命令或文件名…”)
结束选择
返回真值
端函数
公共函数CallCommand1(c1作为字符串,p2作为字符串)
c1.ToLower()
p2.ToLower()
选择案例c1
案例“帮助”
帮助(p2)
案例“十六进制”
十六进制单位(p2)
大小写“ascii”
ascii_par(p2)
结束选择
返回真值
端函数
函数帮助(p作为字符串)
选择案例p
案例“平”
输出(“用法:ping[主机名或ip]”)
案例“cls”
输出(“明显清除您的控制台…”)
案例“十六进制”
输出(“将字符串转换为十六进制代码。用法:十六进制”)
大小写“ascii”
输出(“将十六进制代码转换为字符串。用法:ascii”)
结束选择
返回真值
端函数
函数hex_par(p作为字符串)
将某物变暗为String=StringToHex(p)
输出(某物)
返回真值
端函数
函数ascii_par(p作为字符串)
Dim tostring As String=HexToString(p)
输出(tostring)
返回真值
端函数
公共子可用命令()
输出(“帮助、cls、ping、十六进制、ascii”)
端接头
端模块
我对编程非常陌生,所以需要一些想法