【问题标题】:Limit Length (Character input) in MemoEdit (DevExpress)MemoEdit (DevExpress) 中的限制长度(字符输入)
【发布时间】:2014-04-07 17:58:36
【问题描述】:

我正在使用以下代码(基于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 服务的基于计算机的输入

【问题讨论】:

    标签: .net vb.net winforms devexpress


    【解决方案1】:

    使用标准的 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 控件有效。

    【讨论】:

    • 问题是输入可以从用户输入(使用 Ctrl + V)到计算机输入不同。因此EditValuechanged(类似于textbox的textchanged)更合适。我已修改我的问题以包含此内容。
    【解决方案2】:

    经过一些试验和错误(添加了一些无限循环),我设法找到了一个好的(不完美的)解决方案。我希望下面的代码可能对任何人都有用。

     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()。否则用户将看不到所做的更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      相关资源
      最近更新 更多