vb.net 如何在 Windows 窗体应用程序中嵌入控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20815881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed a Console in a Windows Form application?
提问by 08robertsj
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.Textand Rich_Text_Box_KeyUpwith an if statement for enter being pressed to call the procedure for an enter button.
我正在尝试在 VB.net 中构建文本冒险游戏,就像过去一样。显而易见的选择是控制台应用程序,但是,我决定使用 Windows 窗体,因为我希望包含交互式按钮和图片。目前,我的表单上已经有了一个图片框和一个富文本框。我希望通过富文本框可以实现与控制台相同的工作方式。唉,我的努力是徒劳的。我尝试过的一切都失败了,包括:阅读Rich_Text_Box.Text并Rich_Text_Box_KeyUp使用 if 语句来按下输入以调用输入按钮的过程。
I was wondering if there was any way to include a Console with standard Console.WriteLineand Console.ReadLinecapabilities inside of my Form? This would very much shorten my task and streamline the whole process.
我想知道是否有任何方法可以在我的表单中包含具有标准Console.WriteLine和Console.ReadLine功能的控制台?这将大大缩短我的任务并简化整个过程。
Any ideas?
有任何想法吗?
回答by Jens
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:
您可以为您的目的使用不是一个而是两个文本框。tbOutput 和 tbInput。tbOutput 将是 Multiline 和 ReadOnly 而 tbInput 将是单行,而不是只读并放置在 tbOutput 之下。然后要处理输入,您可以执行以下操作:
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
At the 'Handle input valueyou 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 :-)
在'Handle input value那里,您将检查用户输入并根据您的需要处理它。使用 Lucida Console 字体在灰色和黑色背景中以粗体显示样式:-)
回答by Hans Passant
Sure, a RichTextBox can be used to emulate a console. Some surgery is required to avoid the user from making it malfunction as a console. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Subscribe the InputChanged event to detect when the user presses the Enter key, the Input property gives you the typed text. Use the Write() or WriteLine() methods to add text.
当然,RichTextBox 可用于模拟控制台。需要进行一些手术以避免用户使其作为控制台出现故障。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。订阅 InputChanged 事件以检测用户何时按下 Enter 键,Input 属性为您提供键入的文本。使用 Write() 或 WriteLine() 方法添加文本。
Imports System.Windows.Forms
Public Class RichConsole
Inherits RichTextBox
Public Event InputChanged As EventHandler
Public ReadOnly Property Input() As String
Get
Return Me.Text.Substring(InputStart).Replace(vbLf, "")
End Get
End Property
Public Sub Write(txt As String)
Me.AppendText(txt)
InputStart = Me.SelectionStart
End Sub
Public Sub WriteLine(txt As String)
Write(txt & vbLf)
End Sub
Private InputStart As Integer
Protected Overrides Function ProcessCmdKey(ByRef m As Message, keyData As Keys) As Boolean
'' Defeat backspace
If (keyData = Keys.Back OrElse keyData = Keys.Left) AndAlso InputStart = Me.SelectionStart Then Return True
'' Defeat up/down cursor keys
If keyData = Keys.Up OrElse keyData = Keys.Down Then Return True
'' Detect Enter key
If keyData = Keys.[Return] Then
Me.AppendText(vbLf)
RaiseEvent InputChanged(Me, EventArgs.Empty)
InputStart = Me.SelectionStart
Return True
End If
Return MyBase.ProcessCmdKey(m, keyData)
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
'' Defeat the mouse
If m.Msg >= &H200 AndAlso m.Msg <= &H209 Then Return
MyBase.WndProc(m)
End Sub
End Class

