限制.net文本框中的行数

时间:2009-06-26 10:39:21

标签: .net windows winforms

我正在使用带有多行选项ON的winforms文本框。我想限制可以在其中输入的行数。用户不应该输入更多行。

我怎样才能做到这一点?

7 个答案:

答案 0 :(得分:13)

您需要检查

txtbox.Lines.Length

您需要处理以下两种情况:1。用户在文本框中输入内容2.用户在文本框中粘贴了文本

用户在文本框中输入

您需要处理文本框的按键事件,以防止用户在超出最大行数时输入更多行。

private const int MAX_LINES = 10;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (this.textBox1.Lines.Length >= MAX_LINES && e.KeyChar == '\r')
    {
        e.Handled = true;
    }
}

我已经测试了上面的代码。它按预期工作。

用户在文本框中粘贴一些文字

为防止用户粘贴超过最大行数,您可以编写文本更改事件处理程序:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (this.textBox1.Lines.Length > MAX_LINES)
    {
        this.textBox1.Undo();
        this.textBox1.ClearUndo();
        MessageBox.Show("Only " + MAX_LINES + " lines are allowed.");
    }
}

答案 1 :(得分:1)

此解决方案不起作用,因为当您连续输入时,无论您在屏幕上看到的行数是多少,它都将被视为1行。

为了解决这个问题,您需要使用SendMessage API来计算您在屏幕上看到的行数。这是代码。

Private Declare Function SendMessageINT Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const EM_GETLINECOUNT = &HBA

Private Sub txtText1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtText1.KeyPress

    Const MAX_LINES = 13
    Dim lngCount As Long

    lngCount = SendMessageINT(txtText1.Handle, EM_GETLINECOUNT, 0, 0)

    If lngCount = MAX_LINES And Asc(e.KeyChar) <> Keys.Back And Asc(e.KeyChar) <> Keys.Delete Then
        e.Handled = True
    End If
End Sub

除此之外,您还需要在文本框中找到光标位置,以便您可以允许用户键入。在前面的代码中,一旦达到13行,用户将无法输入任何行。为了克服这个问题,你必须找出,光标在哪一行。请使用以下代码。

将此声明与API声明一起声明

Private Const EM_LINEFROMCHAR =&amp; HC9

下面的代码必须放在文本框的MouseDown,MouseUp,KeyDown和KeyUp事件中。

intLineNo = SendMessageINT(txtText1.Handle,EM_LINEFROMCHAR,-1,0&amp;)+ 1

找到LineNo后,您可以在TextBox的KeyPress事件中进行评估。

答案 2 :(得分:0)

根据您要实现的目标,还有MaxLength属性来设置您可以在文本框中输入的字符数(因为一行可能具有可变长度)。

答案 3 :(得分:0)

使用截断进行复制/粘贴限制为MAX_LINES。

    private void textBox1_KeyDown( object sender, KeyEventArgs e )
    {
        if ( textBox1.Lines.Length >= MAX_LINES && e.KeyValue == '\r' )
            e.Handled = true;
    }

    private void textBox1_TextChanged( object sender, EventArgs e )
    {
        if ( textBox1.Lines.Length > MAX_LINES )
        {
            string[] temp = new string[MAX_LINES];
            for ( int i = 0; i < MAX_LINES; i++ )
            {
                temp[i] = textBox1.Lines[i];
            }

            textBox1.Lines = temp;
        }
    }

答案 4 :(得分:0)

private void txttrccertificateto_TextChanged(object sender, EventArgs e)
{
  if (txttrccertificateto.Text.Length > MAX_Char)
  {
    txttrccertificateto.Text = txttrccertificateto.Text.Remove(MAX_Char, 1);
  }
  if (txttrccertificateto.Lines.Length > MAX_LINES)
  {
    string[] temp = new string[MAX_LINES];
    for (int i = 0; i < MAX_LINES; i++)
    {
      temp[i] = txttrccertificateto.Lines[i];
    }

    txttrccertificateto.Lines = temp;
  }
}

private void txttrccertificateto_KeyDown(object sender, KeyEventArgs e)
{
  if (txttrccertificateto.Lines.Length >= MAX_LINES && e.KeyValue == '\r')
    e.Handled = true;
  if (txttrccertificateto.Text.Length >= MAX_Char && e.KeyValue == '\r')
    e.Handled = true;
}

private void txttrccertificateto_KeyUp(object sender, KeyEventArgs e)
{
  if (txttrccertificateto.Lines.Length >= MAX_LINES && e.KeyValue == '\r')
    e.Handled = true;
  if (txttrccertificateto.Text.Length >= MAX_Char && e.KeyValue == '\r')
    e.Handled = true;
}

答案 5 :(得分:0)

我知道这是一个旧线程,但这是我的解决方案。您基本上检查第一个或最后一个字符是否在客户区之外。由于某种原因,如果第一个字符滚出框,它的Y值将大于0,这就是为什么我也检查它。这适用于换行符,复制/粘贴和字符换行。

Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.TextChanged
     If Not Me.Multiline OrElse String.IsNullOrEmpty(Me.Text) OrElse _InTextChanged Then Exit Sub
     _InTextChanged = True

     Try
        Dim posLast As Point = Me.GetPositionFromCharIndex(Me.Text.Length - 1)
        Dim posFirst As Point = Me.GetPositionFromCharIndex(0)

        Dim sizeLast As SizeF
        Dim sizeFirst As SizeF

        Using g As Graphics = Graphics.FromHwnd(Me.Handle)
           sizeLast = g.MeasureString(Me.Text(Me.Text.Length - 1).ToString(), Me.Font)
           sizeFirst = g.MeasureString(Me.Text(0).ToString(), Me.Font)
        End Using

        If posLast.Y + sizeLast.Height > Me.ClientSize.Height OrElse posFirst.Y < 0 OrElse posFirst.Y + sizeFirst.Height > Me.ClientSize.Height Then

           Me.Text = _PreviousText

           If _PreviousSelectionStart > Me.Text.Length Then
              Me.SelectionStart = Me.Text.Length
           Else
              Me.SelectionStart = _PreviousSelectionStart
           End If


        End If
     Catch ex As Exception

     End Try

     _InTextChanged = False


  End Sub

  Private Sub Textbox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
     _PreviousSelectionStart = Me.SelectionStart
     _PreviousText = Me.Text
  End Sub

答案 6 :(得分:-2)

行。如何定义一个实例变量“lastKnownGoodText”并做这样的事情:

private void textBox_TextChanged (object sender, EventArgs e) {
    if (textBox.Lines.Length > 10)
        textBox.Text = lastKnownGoodText;
    else
        lastKnownGoodText = textBox.Text;
}