是否可以生成一个函数来处理 c# 中多个控件的 previewKeyDown?
Is it possible to generate a single function that handles previewKeyDown for multiple controls in c#?
我是 WPF 和 C# 的新编程,并且开始使用 OOP,我正在尝试使用最佳编程实践来编写我的程序。
我正在做的程序将在整个程序中有很多文本框用于不同的输入目的,但我想使用一个适用于应用程序中所有文本框的单一函数来处理您在每个文本框中键入的内容,以便于维护并在需要时进行快速修改。
到目前为止,我使用它的事件实现了我想要的单个文本框:
1-打字时只接受数字字符
2-在键入时接受制表符、箭头、退格键、del、home、结束键和 ctrl+v 命令
用逗号
分隔千位的 3 格式数字
4-接受最大值 99,999,999
5 句柄粘贴事件,如果不是数字,则不粘贴任何内容或空白“”
6句柄粘贴事件,如果大于99,999,999则设置为最大允许。
所有这些都是通过两个文本框事件完成的
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
是触发事件的实例。如果 Controls 将成为特定控件,在您的情况下,事件连接到 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 的另一个事件作为 reader 的练习。
我是 WPF 和 C# 的新编程,并且开始使用 OOP,我正在尝试使用最佳编程实践来编写我的程序。
我正在做的程序将在整个程序中有很多文本框用于不同的输入目的,但我想使用一个适用于应用程序中所有文本框的单一函数来处理您在每个文本框中键入的内容,以便于维护并在需要时进行快速修改。
到目前为止,我使用它的事件实现了我想要的单个文本框:
1-打字时只接受数字字符
2-在键入时接受制表符、箭头、退格键、del、home、结束键和 ctrl+v 命令
用逗号
分隔千位的 3 格式数字
4-接受最大值 99,999,999
5 句柄粘贴事件,如果不是数字,则不粘贴任何内容或空白“”
6句柄粘贴事件,如果大于99,999,999则设置为最大允许。
所有这些都是通过两个文本框事件完成的
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
是触发事件的实例。如果 Controls 将成为特定控件,在您的情况下,事件连接到 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 的另一个事件作为 reader 的练习。