是否可以生成一个函数来处理c#中多个控件的previewKeyDown



im在WPF和C#中进行了新的编程,并习惯了OOP,我试图使用最佳编程实践来编写程序。

我所做的程序在整个程序中有很多文本框,用于不同的输入目的,但我想用一个适用于整个应用程序中所有文本框的单一功能来处理你在每个文本框中键入的内容,以便更容易地维护和在需要时快速修改。

到目前为止,我使用它的事件实现了我想要的单个texbox:
1-只接受数字字符,同时键入
2-接受制表符、箭头、退格、del、home、结束键和ctrl+v命令,同时键入
3-用逗号分隔千的格式数字
4-最多接受99999999
5-处理粘贴事件,如果不是数字,则不粘贴任何内容或空白"quot
6句柄粘贴事件,如果数字大于99999999,则设置为允许的最大值

所有这些都是通过两个文本框事件完成的
1-PreviewKeyDown事件和
2-TextChanged事件

我想实现的是将所有这些打包到一个函数中,否则我将不得不为每个文本框重复相同的代码,如果我需要更新任何内容,我将不得不更改所有的texboxes事件。

欢迎所有建议,提前感谢

这是代码:

XAML

<TextBox 
x:Name="txt_startRange" 
Text="{Binding Path=MyValue, StringFormat={}{0:N0}}" 
TextChanged="txt_startRange_TextChanged" 
PreviewKeyDown="txt_startRange_PreviewKeyDown" />

C#

private void txt_startRange_TextChanged(object sender, TextChangedEventArgs e)
{
//if pasted value is not numeric it pastes empty string
if (IsNumeric(skipComma(txt_startRange.Text)) == false)
{
txt_startRange.Text = "";
return;
}
//if length is greater than 19 (unsigned integer 64 uses 20 characters) assumes is a greater number than UINT64 and rounds it to maximum allowed by the code
if (txt_startRange.Text.Length > 19)
{
txt_startRange.Text = "99999999";
}
//if number is greater than maximum allowed by the code 99,999,999 it rounds it to maximum
if (Convert.ToUInt64(skipComma(txt_startRange.Text)) >= 100000000)
{
txt_startRange.Text = "99999999";
}

//this function separates numbers in thousands with a comma
if (!string.IsNullOrEmpty(txt_startRange.Text))
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
int valueBefore = Int32.Parse(skipComma(txt_startRange.Text), 
System.Globalization.NumberStyles.AllowThousands);
txt_startRange.Text = String.Format(culture, "{0:N0}", valueBefore);
txt_startRange.Select(txt_startRange.Text.Length, 0);
}
//skipcomma function basically removes the comma from the format and converts it to int
}

private void txt_startRange_PreviewKeyDown(object sender, KeyEventArgs e)
{
int key = Convert.ToInt32(e.Key);
//Ignores everything different from 0-9, arrows, tab,backspace,del,home,end and ctr+v
if ((key<74 || key>83) && (key<34 || key>43) && (key < 21 || key > 26) 
&& key!=2 
&& key != 32 
&& key != 3
&& key != 118
&& key != 65
&& key != 67
|| (txt_startRange.Text.Length >9 && key!=2 && key!=32 && key!=3 &&  (key < 21 || key > 26))) 
{
e.Handled = true;
}
}

第一个参数sender是触发事件的实例。如果控件是特定控件,那么在您的情况下,事件连接到TextBox。

您只需要转换为正确的类型。这就是您的PreviewKeyDown的运行方式:

private void PreventChars_PreviewKeyDown(object sender, KeyEventArgs e)
{
TextBox textbox = (TextBox) sender; //sender will have an instance to txt_startRange
int length = textbox.Text.Length;  // use the TextBox length
int key = Convert.ToInt32(e.Key);
//Ignores everything different from 0-9, arrows, tab,backspace,del,home,end and ctr+v
if ((key<74 || key>83) && (key<34 || key>43) && (key < 21 || key > 26) 
&& key!=2 
&& key != 32 
&& key != 3
&& key != 118
&& key != 65
&& key != 67
|| (length >9 && key!=2 && key!=32 && key!=3 &&  (key < 21 || key > 26))) 
{
e.Handled = true;
}
}

您的标记将更改为

<TextBox 
x:Name="txt_startRange" 
Text="{Binding Path=MyValue, StringFormat={}{0:N0}}" 
TextChanged="txt_startRange_TextChanged" 
PreviewKeyDown="PreventChars_PreviewKeyDown" /> <!-- reusable eventhandler -->

如果你有另一个文本框:

<TextBox 
x:Name="txt_endRange" 
Text="{Binding Path=EndValue, StringFormat={}{0:N0}}" 
TextChanged="txt_endRange_TextChanged" 
PreviewKeyDown="PreventChars_PreviewKeyDown" /> <!-- reusable eventhandler -->

我将更改TextChanged的另一个事件作为读者的练习。

最新更新