VB.NET:代码运行没有错误,但不会将项目添加到列表框

时间:2016-02-18 08:28:19

标签: vb.net listbox

我在我的表单上有这个嵌入式CMD,我使用其他人的代码创建,一切正常。在其中一个Private Subs(每次在CMD输出文本框中写入新行时似乎运行)中,我有一行在另一个标记为Status的表单上将项添加到列表框(列表框名称为txtPlayerList)。

当代码的这个区域运行时,它不会抛出任何错误(如果我将msgbox()放在同一行上,则msgbox()可以工作)。如果我在form_load上添加listbox行,那么它是否完美?

这是我的代码,我已经包含了该表单中的所有内容以防万一(它位于顶部的第三个子行中,带有星号和评论“获取播放器以及其他内容”

onUpdatePolygonData(data){

/// DO SOME CALCULATIONS TO GET point1, point2 point3...

if(Cesium.defined(entity.polygon)
     entity.polygon.hierarchy = new Cesium.PolygonHierarchy([point1, point2, point3])  
else entity.polygon = viewer.entities.add({ 
     polygon = new Cesium.PolygonGraphics({
          hierarchy: new Cesium.PolygonHierarchy([point1, point2, point3])
     })
});
}

编辑:以下是我根据请求获得的form1代码

'代码

Imports System.IO

Public Class Console

Public WithEvents MyProcess As Process
Private Delegate Sub AppendOutputTextDelegate(ByVal text As String)
Public LastLine As String
Public LastLineFormatted As String

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim LocalpathParent As String = Application.StartupPath() + "\MCserver"

    'loads embed cmd
    Me.AcceptButton = ExecuteButton
    MyProcess = New Process
    With MyProcess.StartInfo
        .FileName = "CMD.EXE"
        .UseShellExecute = False
        .CreateNoWindow = True
        .RedirectStandardInput = True
        .RedirectStandardOutput = True
        .RedirectStandardError = True
        .WorkingDirectory = LocalpathParent
    End With
    MyProcess.Start()

    MyProcess.BeginErrorReadLine()
    MyProcess.BeginOutputReadLine()
    AppendOutputText("Process Started at: " & MyProcess.StartTime.ToString)

    'Resize with parent mdi container. Needs to be anchored & StartPosition = manual in properties
    Me.WindowState = FormWindowState.Maximized

End Sub

Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived
    AppendOutputText(vbCrLf & "Error: " & e.Data)
End Sub

Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
    AppendOutputText(vbCrLf & e.Data)

    '*****************************************
    'Get Players and maybe other stuff as well
    '*****************************************

    LastLine = Me.OutputTextBox.Lines.Last
    If Status.ServerStarted = True Then

        If Me.LastLine.Contains(" joined the game") Then
            LastLineFormatted = Me.LastLine
            LastLineFormatted = LastLineFormatted.Replace(" joined the game", "")

            '***THIS LINE BELOW WORKS IN FORM LOAD, BUT NOT HERE FOR SOME REASON???***

            Status.txtPlayersList.Items.Add(LastLineFormatted)
            MsgBox("add lastlineformatted")

        ElseIf Me.LastLine.Contains(" left the game") Then
            LastLineFormatted = Me.LastLine
            LastLineFormatted = LastLineFormatted.Replace(" left the game", "")

            Status.txtPlayersList.Items.Remove(LastLineFormatted)
        End If
    End If
End Sub

Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click
    MyProcess.StandardInput.WriteLine(InputTextBox.Text)
    MyProcess.StandardInput.Flush()
    InputTextBox.Text = ""
End Sub

Private Sub AppendOutputText(ByVal text As String)
    If OutputTextBox.InvokeRequired Then
        Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
        Try
            Me.Invoke(myDelegate, text)
        Catch
        End Try
    Else
        Try
            OutputTextBox.AppendText(text)
        Catch
        End Try
    End If
End Sub
End Class

“代码

1 个答案:

答案 0 :(得分:0)

创建嵌入式CMD窗口的原始代码似乎干扰了更新另一个mdi子列表中的列表框的代码。找到另一种方法来嵌入一个cmd控制台,然后一些摆弄,它似乎工作正常。虽然我还没有能够测试纯服务器输出。

这些代码已经发生了很多变化,这些变化太大了,不能在这里发布,但是替代嵌入式CMD就是这样。

将其置于一般形式声明

'command prompt variables
Private strResults As String
Private intStop As Integer
Private swWriter As System.IO.StreamWriter
Friend thrdCMD As System.Threading.Thread
Private Delegate Sub cmdUpdate()
Private uFin As New cmdUpdate(AddressOf UpdateText)
Public WithEvents procCMDWin As New Process

这在你的form_load Sub

    thrdCMD = New System.Threading.Thread(AddressOf Prompt)
    thrdCMD.IsBackground = True
    thrdCMD.Start()

以及表单类

中的这些声明
Private Sub Prompt()
    AddHandler procCMDWin.OutputDataReceived, AddressOf CMDOutput
    AddHandler procCMDWin.ErrorDataReceived, AddressOf CMDOutput
    procCMDWin.StartInfo.RedirectStandardOutput = True
    procCMDWin.StartInfo.RedirectStandardInput = True
    procCMDWin.StartInfo.CreateNoWindow = True
    procCMDWin.StartInfo.UseShellExecute = False
    procCMDWin.StartInfo.FileName = "cmd.exe"
    procCMDWin.StartInfo.WorkingDirectory = LocalpathParent
    procCMDWin.Start()
    procCMDWin.BeginOutputReadLine()
    swWriter = procCMDWin.StandardInput
    Do Until (procCMDWin.HasExited)
    Loop
    procCMDWin.Dispose()
End Sub

Private Sub UpdateText()
    OutputTextBox.Text += strResults
    OutputTextBox.SelectionStart = OutputTextBox.TextLength - 1
    InputTextBox.Focus()
    intStop = OutputTextBox.SelectionStart
    OutputTextBox.ScrollToCaret()
    If OutputTextBox.Lines.Count > 2 Then
        LastLine = OutputTextBox.Lines.ElementAt(OutputTextBox.Lines.Count - 2)
        If Status.ServerStarted = True Then
            'get element 1 of split
            If LastLine.Contains(" joined the game") Then
                LastLineFormatted = ExtractName(LastLine, " joined the game")
                'If listlineformatted.contains(Players.allitems) then do
                Status.txtPlayersList.Items.Add(LastLineFormatted)
                Status.Show()
            ElseIf Me.LastLine.Contains(" left the game") Then
                LastLineFormatted = ExtractName(LastLine, " left the game")
                'If listlineformatted.contains(Players.allitems) then do
                Status.txtPlayersList.Items.Remove(LastLineFormatted)
                MsgBox("remove lastlineformatted")
            End If
        End If
    End If
End Sub

Private Function ExtractName(unformattedString As String, stringToRemove As String) As String
    Dim temp As String = Split(unformattedString, "Server]")(1).ToString
    ExtractName = temp.Replace(stringToRemove, "")
End Function

Private Sub CMDOutput(ByVal Sender As Object, ByVal OutputLine As DataReceivedEventArgs)
    strResults = OutputLine.Data & Environment.NewLine
    Invoke(uFin)
End Sub