.net 将插入符号放置在 MaskedTextbox 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/119284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Position the caret in a MaskedTextbox
提问by benPearce
I would like to be able to override the default behaviour for positioning the caret in a masked textbox.
我希望能够覆盖在掩码文本框中定位插入符号的默认行为。
The default is to place the caret where the mouse was clicked, the masked textbox already contains characters due to the mask.
默认是将插入符号放置在鼠标单击的位置,由于掩码,被掩码的文本框已经包含字符。
I know that you can hide the caret as mentioned in this post, is there something similar for positioning the caret at the beginning of the textbox when the control gets focus.
我知道你可以像这篇文章中提到的那样隐藏插入符号,当控件获得焦点时,是否有类似的东西可以将插入符号定位在文本框的开头。
回答by Abbas
This should do the trick:
这应该可以解决问题:
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate()
{
maskedTextBox1.Select(0, 0);
});
}
回答by Ishmaeel
To improve upon Abbas's working solution, try this:
要改进 Abbas 的工作解决方案,请尝试以下操作:
private void ueTxtAny_Enter(object sender, EventArgs e)
{
//This method will prevent the cursor from being positioned in the middle
//of a textbox when the user clicks in it.
MaskedTextBox textBox = sender as MaskedTextBox;
if (textBox != null)
{
this.BeginInvoke((MethodInvoker)delegate()
{
int pos = textBox.SelectionStart;
if (pos > textBox.Text.Length)
pos = textBox.Text.Length;
textBox.Select(pos, 0);
});
}
}
This event handler can be re-used with multiple boxes, and it doesn't take away the user's ability to position the cursor in the middle of entered data (i.e does not force the cursor into zeroeth position when the box is not empty).
此事件处理程序可以与多个框一起使用,并且不会剥夺用户将光标定位在输入数据中间的能力(即,当框不为空时,不会强制光标进入第零个位置)。
I find this to be more closely mimicking a standard text box. Only glitch remaining (that I can see) is that after 'Enter' event, the user is still able to select the rest of the (empty) mask prompt if xe holds down the mouse and drags to the end.
我发现这更接近于模仿标准文本框。唯一剩下的小故障(我可以看到)是在“Enter”事件之后,如果 xe 按住鼠标并拖动到最后,用户仍然可以选择(空)掩码提示的其余部分。
回答by Mike M
That is a big improvement over the default behaviour of MaskedTextBoxes. Thanks!
这是对 MaskedTextBoxes 的默认行为的重大改进。谢谢!
I made a few changes to Ishmaeel's excellent solution. I prefer to call BeginInvoke only if the cursor needs to be moved. I also call the method from various event handlers, so the input parameter is the active MaskedTextBox.
我对 Ishmaeel 的出色解决方案进行了一些更改。我更喜欢仅在需要移动光标时调用 BeginInvoke。我还从各种事件处理程序调用该方法,因此输入参数是活动的 MaskedTextBox。
private void maskedTextBoxGPS_Click( object sender, EventArgs e )
{
PositionCursorInMaskedTextBox( maskedTextBoxGPS );
}
private void PositionCursorInMaskedTextBox( MaskedTextBox mtb )
{
if (mtb == null) return;
int pos = mtb.SelectionStart;
if (pos > mtb.Text.Length)
this.BeginInvoke( (MethodInvoker)delegate() { mtb.Select( mtb.Text.Length, 0 ); });
}
回答by Brian Pressman
//not the prettiest, but it gets to the first non-masked area when a user mounse-clicks into the control
private void txtAccount_MouseUp(object sender, MouseEventArgs e)
{
if (txtAccount.SelectionStart > txtAccount.Text.Length)
txtAccount.Select(txtAccount.Text.Length, 0);
}
回答by mdb
Partial answer: you can position the caret by assigning a 0-length selection to the control in the MouseClick event, e.g.:
部分答案:您可以通过为 MouseClick 事件中的控件分配 0 长度选择来定位插入符号,例如:
MaskedTextBox1.Select(5, 0)
...will set the caret at the 5th character position in the textbox.
...将在文本框中的第 5 个字符位置设置插入符号。
The reason this answer is only partial, is because I can't think of a generally reliable way to determine the position where the caret shouldbe positioned on a click. This may be possible for some masks, but in some common cases (e.g. the US phone number mask), I can't really think of an easy way to separate the mask and prompt characters from actual user input...
这个答案只是部分的原因是因为我想不出一种普遍可靠的方法来确定插入符号应在单击时定位的位置。这对于某些掩码可能是可能的,但在某些常见情况下(例如美国电话号码掩码),我真的想不出一种简单的方法将掩码和提示字符与实际用户输入分开......
回答by hmota
This solution works for me. Give it a try please.
这个解决方案对我有用。请试一试。
private void maskedTextBox1_Click(object sender, EventArgs e)
{
maskedTextBox1.Select(maskedTextBox1.Text.Length, 0);
}
回答by Gman Cornflake
I got mine to work using the Click event....no Invoke was needed.
我使用 Click 事件让我的工作......不需要 Invoke。
private void maskedTextBox1_Click(object sender, EventArgs e)
{
maskedTextBox1.Select(0, 0);
}
回答by Bill Kamer
This solution uses the Click method of the MaskedTextBox like Gman Cornflake used; however, I found it necessary to allow the user to click inside the MaskedTextBox once it contained data and the cursor stay where it is.
该解决方案使用了MaskedTextBox的Click方法,就像使用的Gman Cornflake一样;但是,我发现有必要允许用户在 MaskedTextBox 包含数据并且光标停留在它所在的位置后单击它。
The example below turns off prompts and literals and evaluates the length of the data in the MaskedTextBox and if equal to 0 it puts the cursor at the starting position; otherwise it just bypasses the code that puts the cursor at the starting position.
下面的示例关闭提示和文字,并计算 MaskedTextBox 中数据的长度,如果等于 0,则将光标置于起始位置;否则它只是绕过将光标置于起始位置的代码。
The code is written in VB.NET 2017. Hope this helps!
代码是用 VB.NET 2017 编写的。希望这会有所帮助!
Private Sub MaskedTextBox1_Click(sender As Object, e As EventArgs) Handles MaskedTextBox1.Click
Me.MaskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
If Me.MaskedTextBox1.Text.Length = 0 Then
MaskedTextBox1.Select(0, 0)
End If
Me.MaskedTextBox1.TextMaskFormat = MaskFormat.IncludePromptAndLiterals
End Sub
回答by Joelius
I know this is an old question but google has led me here multiple times and none of the answers do exactly what I want.
For example, the current answers don't work as I want when you're using TextMaskFormat = MaskFormat.ExcludePromptAndLiterals.
我知道这是一个老问题,但谷歌多次引导我来到这里,但没有一个答案完全符合我的要求。
例如,当您使用TextMaskFormat = MaskFormat.ExcludePromptAndLiterals.
The method below makes the cursor jump to the first promptwhen the textbox is entered. Once you've entered the textbox, you can freely move the cursor.
Also it works fine for MaskFormat.ExcludePromptAndLiterals.
下面的方法使光标在输入文本框时跳转到第一个提示。输入文本框后,您可以自由移动光标。它也适用于MaskFormat.ExcludePromptAndLiterals.
That's why I think it's worth leaving this here :)
这就是为什么我认为值得把它留在这里:)
private void MaskedTextBox_Enter(object sender, EventArgs e)
{
// If attached to a MaskedTextBox' Enter-Event, this method will
// make the cursor jump to the first prompt when the textbox gets focus.
if (sender is MaskedTextBox textBox)
{
MaskFormat oldFormat = textBox.TextMaskFormat;
textBox.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
string fullText = textBox.Text;
textBox.TextMaskFormat = oldFormat;
int index = fullText.IndexOf(textBox.PromptChar);
if (index > -1)
{
BeginInvoke(new Action(() => textBox.Select(index, 0)));
}
}
}

