private void TextBox27_GotFocus(object sender, RoutedEventArgs e)
{
Stil53.Begin();
TextBox27.Select(TextBox27.Text.Length, 0);
Takvim_Kapat();
KeyEventHandler mhth = new KeyEventHandler(TextBox27_PreviewKeyUp); <=////this
}
private void TextBox27_PreviewKeyUp(object sender, KeyEventArgs e)
{
Listelemeler.Para_Birimi_Noktalama(TextBox27, e);
}
在 gotfocus 事件中,我试图触发 KeyEventArgs 事件。我怎样才能实现这个目标?你能给我一个主意吗?提前谢谢你
在 gotfocus 事件中,我试图触发 KeyEventArgs 事件。我怎样才能实现这个目标?你能给我一个主意吗?提前谢谢你
解决方案示例:
private void TextBox27_PreviewKeyDown(object sender, KeyEventArgs e)
{
var textBox = (TextBox)sender;
bool commaExists = textBox.Text.Contains(",");
int maxLength = 9;
if(textBox.Text.Length >= maxLength)
{
e.Handled = true;
return;
}
if (e.Key == Key.Decimal || e.Key == Key.OemPeriod || e.Key == Key.OemComma)
{
if (!commaExists)
{
// Replace dot with comma
int caretIndex = textBox.CaretIndex;
textBox.Text = textBox.Text.Insert(caretIndex, ",");
// Move caret after the comma
textBox.CaretIndex = caretIndex + 1;
// Mark the event as handled
e.Handled = true;
}
return;
}
if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)))
{
// If the pressed key is not a number, mark the event as handled
e.Handled = true;
return;
}
}