How to embed a Console in a Windows Form applicati

2019-08-14 22:41发布

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.

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.

Any ideas?

2条回答
放荡不羁爱自由
2楼-- · 2019-08-14 23:31

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

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 :-)

查看更多
家丑人穷心不美
3楼-- · 2019-08-14 23:45

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.

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
查看更多
登录 后发表回答