Cmd控件表单VB.NET [英] Cmd control form VB.NET

查看:77
本文介绍了Cmd控件表单VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows窗体中启动cmd会话命令ipconfig

但是在 显示错误FnctionStartConsoleEvent

有人帮我吗?



我尝试过的事情:



start a cmd session in a windows form whit command "ipconfig"
but show an error on FnctionStartConsoleEvent
anyone help me?

What I have tried:

Dim cdd() As String
    Event Avvia()
    Function FnctionStartConsoleEvent(e As KeyEventArgs) As EventHandler(Of ComboBox.ObjectCollection)
        If e.KeyData = Keys.Enter Then
            Dim process As Process = New Process
            With process
                '  .StartInfo.ErrorDialog = False
                .StartInfo.RedirectStandardInput = True
                .StartInfo.RedirectStandardOutput = True
                .StartInfo.UseShellExecute = False
                .StartInfo.FileName = "cmd.exe"
                .StartInfo.CreateNoWindow = True
                .StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            End With
            process.Start()
            Try
                process.StandardInput.WriteLine("cd/d " & cdd(3))
            Catch
            End Try
            process.StandardInput.WriteLine(ComboBox1.Text)
            process.StandardInput.WriteLine("echo [~cd~]%cd%[~cd~]")
            process.StandardInput.WriteLine("exit")
            Dim outpp As New TextBox
            outpp.Text = process.StandardOutput.ReadToEnd
            cdd = Split(outpp.Text, "[~cd~]")

            For i = 4 To outpp.Lines.Length - 6
                RichTextBox1.Text = RichTextBox1.Text & outpp.Lines(i).Substring(0) & vbCrLf
            Next
            ComboBox1.Items.Add(ComboBox1.Text)
            ComboBox1.Text = ""
            RichTextBox1.SelectionStart = RichTextBox1.Text.Length
            RichTextBox1.ScrollToCaret()
        End If
    End Function

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        AddHandler Avvia, AddressOf FnctionStartConsoleEvent
        If ComboBox1.SelectedText = "ipconfig" Then
            RaiseEvent Avvia()
        End If
    End Sub

推荐答案

第一个问题:

您有一个期望零参数的事件。



您正在尝试添加一个处理器方法,该方法需要一个参数 - 一个 KeyEventArgs 实例。



事件处理程序必须匹配他们处理的事件的签名。如果没有,则会出现屏幕截图中显示的编译错误。






第二个问题:

您已声明 FnctionStartConsoleEvent 返回一个事件处理程序方法,该方法将接受两个参数 - sender As Object args As ComboBox.ObjectCollection



该方法没有这样的事情。



由于该方法没有返回任何内容,VB有帮助插入返回Nothing 在函数退出之前。如果您尝试使用返回值,您将获得 NullReferenceException






您需要重新开始,并阅读一些有关事件如何工作的教程。



事件(Visual Basic) [ ^ ]

提升事件和响应事件 [ ^ ]

演练:声明和提升事件(Visual Basic) [ ^ ]

Step by Ste p:VB.NET中的事件处理 [ ^ ]
First problem:
You have an event which expects zero arguments.

You are trying to add a handler method which expects one argument - a KeyEventArgs instance.

Event handlers MUST match the signature of the event they handle. If they don't, you get the compiler error shown in your screen-shot.



Second problem:
You have declared that FnctionStartConsoleEvent returns an event handler method which will accept two arguments - sender As Object and args As ComboBox.ObjectCollection.

The method does no such thing.

Since the method doesn't return anything, VB "helpfully" inserts a Return Nothing before the function exits. If you try to use the return value, you will get a NullReferenceException.



You need to start again, and read a few tutorials on how events work.

Events (Visual Basic)[^]
Raising Events and Responding to Events[^]
Walkthrough: Declaring and Raising Events (Visual Basic)[^]
Step by Step: Event handling in VB.NET[^]


这篇关于Cmd控件表单VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆