Ich weiß, dass dies eine alte Frage ist, aber Google hat mich mehrfach hierher geführt und keine der Antworten entspricht genau dem, was ich will.
Zum Beispiel funktionieren die aktuellen Antworten nicht so, wie ich es möchte, wenn Sie TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
.
Mit der folgenden Methode springt der Cursor auf die erste Aufforderung wenn das Textfeld eingegeben wird. Sobald Sie das Textfeld eingegeben haben, können Sie den Cursor frei bewegen. Es funktioniert auch gut für MaskFormat.ExcludePromptAndLiterals
.
Deshalb denke ich, es lohnt sich, dies hier zu lassen :)
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)));
}
}
}