需要创建一个类似终端窗口的编辑控件

时间:2014-02-04 21:36:38

标签: winforms mouseevent edit

我需要创建一个编辑控件,按照输入的方式获取每一行,然后关闭并根据输入的内容执行操作。我不希望用户能够通过鼠标和删除键删除他们在框中输入的内容。它看起来像这样:

  

开机(用户输入此内容)   电源:开电平:50(电源开启命令返回)   激光打开(用户输入此)   激光:开机功率:60(命令返回的内容)

所以我不希望他们返回并删除已经输入的行,他们可以继续追加项目并发送这些命令。有关如何实现这一目标的任何想法?感谢。

1 个答案:

答案 0 :(得分:0)

我认为你最好的选择是创建一个混合的UserControl,它由2个Textbox组成,另一个只读,另一个是可写的。在一个中输入您的数据,在按下回车键时将其附加到另一个,然后附加响应。由于您尚未说明编程语言,因此这是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的InitializeComponent方法我只是将其放入,以便您可以看到我的控件的属性

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