我使用以下代码(基于devexpress帮助论坛)来防止用户放弃更多的代码,而不是48 characters在MemoEdit中的一行
Private Sub txtAuthors_EditValueChanging(sender As System.Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles txtAuthors.EditValueChanging
If e.NewValue Is Nothing Then
'No Value found in memoEditor
Return
End If
'Max lenght of textbox
Dim maxLength As Integer = 48
Dim edit As DevExpress.XtraEditors.MemoEdit = TryCast(sender, DevExpress.XtraEditors.MemoEdit)
For Each str As String In edit.Lines
If str.Length > maxLength Then
e.Cancel = True
Return
End If
Next str
End Sub此函数防止插入超过48个字符的字符串。但我真正希望达到的目标是:
我的目标:如果用户使用more than 48 chars输入一个新字符串(使用Ctrl + V/Paste),则为。它不应阻止输入所有数据。除了the first 48 chars以外,控件应该放弃其余的控件。如何实现这种行为。我试过操纵e.NewValue,但没有用.
关于Lines**-property:**的注记
You are not able to use the Lines property to change a particular array's element
directly. Instead, you should read the Lines property to get the array, change
the required array's element and then assign the array back to Lines.备注:我读过这个(Limit the input length of a DevExpress TextEdit and MemoEdit controls),但没有帮助
注2:在MemoEdit内部提供的输入可以从普通用户输入(按任意键或Ctrl + V)到来自WCF-服务的计算机输入不等。
发布于 2014-03-07 07:49:21
经过一些尝试和错误(在混合中添加了一些无限循环),我找到了一个好的(不是完美的)解决方案。我希望下面的代码对任何人都有帮助。
Public Sub EditValueChanged(sender As System.Object, e As System.EventArgs) Handles txt.EditValueChanged
Dim edit As DevExpress.XtraEditors.MemoEdit = TryCast(sender, DevExpress.XtraEditors.MemoEdit)
'Take copy of the array
Dim myStringArrayCopy As String() = control.Lines
Dim hasEdits As Boolean = False
For Each str As String In myStringArrayCopy
If str.Length > maxCharCount Then
Dim indexString As Integer = Array.IndexOf(myStringArrayCopy, str)
myStringArrayCopy(indexString) = str.Substring(0, 47)
hasEdits = True
End If
Next str
If (hasEdits) Then
control.Lines = myStringArrayCopy
control.Refresh()
End If
End Sub我对这段代码有几点看法。
备注1:使用EditValueChanged而不是EditValueChanging。在编辑完成后而不是在中间修改文本框更有意义。
备注2:如果编辑超过48个字符,则为。然后,字符串将被缩短,但光标将放在第一行(这是一个多行txt)
注释3:不要忘记refresh()。否则,所做的更改对用户是不可见的。
发布于 2014-03-05 16:14:14
使用标准的winform,可以通过处理KeyDown事件、查找Ctrl + V键和检查Cliboard文本来实现这一点。
Private Sub txtAuthors_KeyDown(sender As Object, e As KeyEventArgs) Handles txtAuthors.KeyDown
If ((e.Modifiers = Keys.Control) AndAlso (e.KeyCode = Keys.V)) Then
Dim text As String = My.Computer.Clipboard.GetText()
If (text.Length > 48) Then
My.Computer.Clipboard.SetText(text.Substring(0, 48))
End If
End If
End Sub注意:我没有安装devexpress,所以我不能保证这将适用于MemoEdit控件。
https://stackoverflow.com/questions/22201786
复制相似问题