简体   繁体   English

将插入符号位置设置为蒙版文本框中的最后一个字符-Winform?

[英]set caret position to last character in masked text box - winform?

I have a masked textbox with input mask as A-00-000-0000 我有一个带有输入掩码为A-00-000-0000的掩码文本框
It gets data from database. 它从数据库获取数据。 Like 喜欢
蒙版文字框

Is there any way to set caret position to specific position as mentioned above? 如上所述,有什么方法可以将插入符号位置设置为特定位置?

you might wanna try something like this: 您可能想尝试这样的事情:

textBox1.Focus();
textBox1.SelectionStart = 8;
textBox1.SelectionLength = 0;

(selectionStart sets the position - length is 0 so nothing is selected). (selectionStart设置位置-长度为0,因此未选择任何内容)。

so in your case, you'd set SelectionStart to 8, then to 3, then to 7... 因此,您需要将SelectionStart设置为8,然后设置为3,然后设置为7 ...

make sure to set focus to this textBox/Maskedtextbox first, otherwise it won't work... 确保首先将焦点设置为此textBox / Maskedtextbox,否则它将无法工作...

Edit: you'll have to make sure that after setting the text to the values from the DB, the caret-position is calculated respecting the mask-characters. 编辑:您必须确保在将文本设置为数据库中的值后,根据遮罩字符计算插入符位置。

Edit2: like this i can set the Position wherever i want - just, 4 will set the position to position 4 respecting the mask. Edit2:这样,我可以在任意位置设置位置-只需4,就可以将位置设置为相对于蒙版的位置4。 If i omit SelectionStart and SelectionLength, the caret is positioned automatically to the last character inserted by the first line. 如果我省略SelectionStart和SelectionLength,则插入标记会自动定位到第一行插入的最后一个字符。

maskedTextBox1.Text = "A018";
maskedTextBox1.Focus();
maskedTextBox1.SelectionStart = 7;
maskedTextBox1.SelectionLength = 0;

A simple way is to use a reverse loop and check each char. 一种简单的方法是使用反向循环并检查每个字符。 If it's not a Hyphen nor a Space then you've reached the end of the input string. 如果它不是Hyphen也不是Space那么您已经到达输入字符串的末尾。

Dim text As String = Me.MaskedTextBox1.Text
Dim index As Integer = 0
Dim character As Char = Nothing

For index = text.Length To 1 Step -1
    character = text.Chars((index - 1))
    If ((character <> "-"c) AndAlso (AscW(character) <> 32)) Then
        Exit For
    End If
Next

Me.MaskedTextBox1.Focus()
Me.MaskedTextBox1.[Select](index, 0)

After optimizing my codes, my logic is to move caret at desired location, independen of mask, And remove string after that location.. 优化代码后,我的逻辑是将插入符号移动到所需位置,独立于蒙版,并在该位置之后删除字符串。

''' <summary>
'''  Set Caret Position to specific location and remove data After given Location
''' </summary>
''' <param name="MaskedTb"> Masked textbox</param>
''' <param name="Loct99">Location (Index) after which remove occured and caret is set</param>
''' <remarks></remarks>
Public Sub FocsToMaskedTb( MaskedTb As MaskedTextBox, ByVal Loct99 As Integer)
    If MaskedTb.Text.Length >= Loct99 Then
        MaskedTb.Text = MaskedTb.Text.Remove(Loct99)
    End If
    MaskedTb.SelectionStart = MaskedTb.MaskedTextProvider.LastAssignedPosition + 1
    '' I am using MaskedTextProvider Property
    '' If there is nothing in TextBox, MaskedTb.MaskedTextProvider.LastAssignedPosition = -1
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM