在我的.NET应用程序的初始开发期间(使用WinForms),我必须进入并创建常见的编辑快捷方式,例如CTRL-A,CTRL-C和CTRL-V,因为它们默认情况下未启用。现在我的应用程序已经发展到很多文本框,我试图弄清楚如何重构以下代码。有人可以帮忙吗?
private void textBox1_results_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
textBox1_results.SelectAll();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.C)
{
textBox1_results.Copy();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.V)
{
textBox1_results.Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
答案 0 :(得分:2)
如果你有一个所有表单继承的BaseForm尝试将上面的代码移动到它并从所有文本框事件处理程序调用它。
public partial class Form2 : Form1
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
base.TextKeyDown(sender, e);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void TextKeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
((TextBox) sender).SelectAll();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.C)
{
((TextBox)sender).Copy();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.V)
{
((TextBox)sender).Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
}
答案 1 :(得分:2)
嗯,有两种主要方法。
选项1就像是;
public class ShortcutTextBox : TextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
SelectAll();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.C)
{
Copy();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{
Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
base.OnKeyDown(e);
}
}
虽然这有一点需要注意,你需要用ShortcutTextBox替换每个TextBox实例。
选项二仍然涉及重构,但它是每个表单选项一次。创建一个如下所示的辅助类;
public abstract class ControlUtilities
{
public static void AddTextBoxShortcuts(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c is TextBox)
{
TextBox txt = (TextBox)c;
txt.KeyDown += textBox_KeyDown;
}
else if (c.Controls != null && c.Controls.Count > 0)
{
// recursively look for text boxes
AddTextBoxShortcuts(c.Controls);
}
}
}
private static void textBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox txt = (TextBox)sender;
if (e.Control && e.KeyCode == Keys.A)
{
txt.SelectAll();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.C)
{
txt.Copy();
e.SuppressKeyPress = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{
txt.Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
}
并在表单的Load事件中调用它,如;
private void Form1_Load(object sender, EventArgs e)
{
ControlUtilities.AddTextBoxShortcuts(this.Controls);
}
答案 2 :(得分:1)
可能是这样的:
private static void Shortcut_KeyDown(object sender, KeyEventArgs e)
{
var textBox = (TextBox)sender;
if (e.Control && e.KeyCode == Keys.A)
{
textBox.SelectAll();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.C)
{
textBox.Copy();
e.SuppressKeyPress = true;
}
if (e.Control && e.KeyCode == Keys.V)
{
textBox.Text = Clipboard.GetText();
e.SuppressKeyPress = true;
}
}
并应用它:
textBox1.KeyDown += Shortcut_KeyDown;
textBox2.KeyDown += Shortcut_KeyDown;
// etc...
虽然我不确定为什么你需要写这个 - 据我所知,这种行为是Winforms TextBox的标准。