我使用多行选项ON的WinForms文本框。 我想限制可以input的行数。 用户不应该input比这更多的行。
我怎样才能做到这一点?
C#Windows服务 – 多个定时器
获取.NET枚举的Windows本地化
使用模拟来启动进程(间接)在C#
如何在registry中存储securestring?
Windows服务无法正常工作
你需要检查
您需要处理2个方案: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行。
为了解决这个问题,您需要使用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) 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 =&HC9
下面的代码必须放置在文本框的MouseDown,MouseUp,KeyDown和KeyUp事件中。
intLineNo = SendMessageINT(txtText1.Handle,EM_LINEFROMCHAR,-1,0&)+ 1
找到LineNo后,您可以在TextBox的KeyPress事件中进行评估。
根据您要实现的内容,还有MaxLength属性来设置可以在文本框中输入的字符数(因为一行可能具有可变长度)。
限制为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; } }
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; }
我知道这是一个古老的线程,但这是我的解决方案。 你基本上检查是否第一个或最后一个字符是客户区。 出于某种原因,如果第一个字符滚动出框,其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
好。 如何定义一个实例变量“lastKNownGoodText”,并做这样的事情:
private void textBox_TextChanged (object sender,EventArgs e) { if (textBox.Lines.Length > 10) textBox.Text = lastKNownGoodText; else lastKNownGoodText = textBox.Text; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。