如何在Windows Form应用程序中嵌入控制台?

如何在Windows Form应用程序中嵌入控制台?

问题描述:

就像过去一样,我正在尝试在VB.net中构建文字冒险游戏。显而易见的选择是控制台应用程序,但是,我决定使用Windows窗体,因为我希望包括交互式按钮和图片。目前,我已经在表格上得到了一个图片框和一个RTF框。我希望通过Rich Text Box可以实现与控制台相同的功能。 las,我的努力是徒劳的。我尝试过的所有操作都失败了,包括:读取 Rich_Text_Box.Text Rich_Text_Box_KeyUp 并带有按回车键的if语句,

I'm trying to build a Text Adventure game in VB.net, just like the days of old. The obvious choice would be a Console application, however, I have decided on a Windows Form because I am hoping to include interactive buttons and pictures. Currently, I have already got on my form a picture box and a Rich Text Box. I was hoping that with the Rich Text Box I could achieve something that worked in the same way as a console. Alas, my efforts are futile. Everything I have tried has failed, including: reading Rich_Text_Box.Text and Rich_Text_Box_KeyUp with an if statement for enter being pressed to call the procedure for an enter button.

我想知道是否可以用标准的 Console.WriteLine $ c包含一个控制台。表单中的$ c>和 Console.ReadLine 功能?这样会大大缩短我的任务并简化整个过程。

I was wondering if there was any way to include a Console with standard Console.WriteLine and Console.ReadLine capabilities inside of my Form? This would very much shorten my task and streamline the whole process.

有什么想法吗?

您不能使用一个文本框,而只能使用两个文本框。 tbOutput和tbInput。 tbOutput将是Multiline和ReadOnly,而tbInput将是单行,不是只读的,并放置在tbOutput下。然后,要处理输入,您可以执行以下操作:

You could use not one but two Textboxes for your purpose. tbOutput and tbInput. tbOutput would be Multiline and ReadOnly whereas tbInput would be single line, not readonly and placed beneath tbOutput. Then to process inputs you could do something like:

Private Sub Output(s As String)
    If s <> "" Then
        tbOutput.AppendText(vbCrLf & ">> " & s)
    End If
End Sub
Private Sub tbInput_KeyDown(sender As Object, e As KeyEventArgs) Handles tbInput.KeyDown
    If e.KeyCode = Keys.Enter Then
        If tbInput.Text <> "" Then
            Output(tbInput.Text)
            ' Handle input value
            tbInput.Text = ""
        End If
    End If
End Sub

句柄输入值 用户输入并根据您的需要进行处理。使用Lucida Console字体以灰色和黑色背景的粗体显示样式:-)

At the 'Handle input value you would check the user input and handle it according to your needs. Use Lucida Console font in bold in gray and black background for style :-)