JavaEar 专注于收集分享传播有价值的技术资料

Need to create an edit control that acts like a terminal window

I need to create an edit control that takes each line, as entered, and goes off and does an action according to what was entered. I don't want the user to be able to delete what they typed into the box via their mouse and the delete key. It would look something like this:

Power On (user entered this) Power: On Level:50 (what the power on command returned) Laser On (user entered this ) Laser: On Power:60 ( what the command returned)

So I don't want them to go back and delete the lines already entered, they can just keep appending items and sending off these commands. Any ideas on how to accomplish this? Thanks.

1个回答

    最佳答案
  1. I think your best bet would be creating a hybrid UserControl consisting of 2 Textbox's one which is readonly the other being writable. Enter your data in the one, append it to the other when the enter key is pressed, then append the response. Since you haven't stated a programming language here is an example in vb.net.

    Public Class UserControl1
    
        Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
            If e.KeyCode = Keys.Enter Then
                ScrollToEnd(TextBox1)
                TextBox1.Text += TextBox2.Text + vbCrLf
                TextBox1.Text += GetResponse(TextBox2.Text) + vbCrLf
                TextBox2.Text = ""
                ScrollToEnd(TextBox1)
                e.SuppressKeyPress = True
            End If
        End Sub
    
        Private Sub ScrollToEnd(tb As TextBox)
            tb.SelectionStart = tb.TextLength
            tb.ScrollToCaret()
        End Sub
    
        Private Function GetResponse(command As String) As String
            Select Case command
                Case "Power On"
                    Return "On Level: 50"
                Case "Laser On"
                    Return "Laser: On Power:60"
                Case Else
                    Return "I do not understand"
            End Select
        End Function
    End Class
    

    UserControl1.Designer.VB's InitializeComponent Method (I am only putting this in so you can see the properties of my Controls)

    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
        Me.TableLayoutPanel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.BackColor = System.Drawing.Color.Black
        Me.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.TextBox1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TextBox1.ForeColor = System.Drawing.Color.Lime
        Me.TextBox1.Location = New System.Drawing.Point(3, 3)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(253, 181)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.TabStop = False
        '
        'TextBox2
        '
        Me.TextBox2.BackColor = System.Drawing.Color.Black
        Me.TextBox2.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.TextBox2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TextBox2.ForeColor = System.Drawing.Color.Lime
        Me.TextBox2.Location = New System.Drawing.Point(3, 190)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(253, 17)
        Me.TextBox2.TabIndex = 1
        '
        'TableLayoutPanel1
        '
        Me.TableLayoutPanel1.BackColor = System.Drawing.Color.Black
        Me.TableLayoutPanel1.ColumnCount = 1
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
        Me.TableLayoutPanel1.Controls.Add(Me.TextBox1, 0, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.TextBox2, 0, 1)
        Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
        Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
        Me.TableLayoutPanel1.RowCount = 2
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 89.09953!))
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10.90047!))
        Me.TableLayoutPanel1.Size = New System.Drawing.Size(259, 211)
        Me.TableLayoutPanel1.TabIndex = 2
        '
        'UserControl1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 16.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.TableLayoutPanel1)
        Me.Name = "UserControl1"
        Me.Size = New System.Drawing.Size(259, 211)
        Me.TableLayoutPanel1.ResumeLayout(False)
        Me.TableLayoutPanel1.PerformLayout()
        Me.ResumeLayout(False)
    
    End Sub