【发布时间】:2020-12-07 22:21:36
【问题描述】:
我正在尝试在运行时获取 textBox 控件的 KeyUp 事件,但我正在努力正确转换。下面的代码编译,当我添加 Watch/Inspect rtbPrivateNote_KeyUp -> EventArgs e:
时我可以看到事件信息public class Form1
{
private System.Windows.Controls.TextBox rtbPrivateNote = null;
public InitFormControls()
{
LoadSpellChecker(ref pnlPrivateNotes, ref rtbPrivateNote, "txtPrivateNotePanel");
rtbPrivateNote.TextChanged += new System.Windows.Controls.TextChangedEventHandler(rtbPrivateNote_TextChanged);
rtbPrivateNote.KeyUp += new System.Windows.Input.KeyEventHandler(rtbPrivateNote_KeyUp);
}
private void LoadSpellChecker(ref Panel panelRichText, ref System.Windows.Controls.TextBox txtWithSpell, string ControlName)
{
txtWithSpell = new System.Windows.Controls.TextBox
{
Name = ControlName
};
txtWithSpell.SpellCheck.IsEnabled = true;
txtWithSpell.Width = panelRichText.Width;
txtWithSpell.Height = panelRichText.Height;
txtWithSpell.AcceptsReturn = true;
txtWithSpell.AcceptsTab = true;
txtWithSpell.AllowDrop = true;
txtWithSpell.IsReadOnly = false;
txtWithSpell.TextWrapping = System.Windows.TextWrapping.Wrap;
ElementHost elementHost = new ElementHost
{
Dock = DockStyle.Fill,
Child = txtWithSpell
};
panelRichText.Controls.Add(elementHost);
}
// private void rtbPrivateNote_KeyUp(object sender, KeyEventArgs e) // WONT COMPILE
private void rtbPrivateNote_KeyUp(object sender, EventArgs e)
{
//if (e.Key == Key.Enter
// || e.Key == Key.Return)
//{
// Do Something here
//}
}
}
【问题讨论】:
-
您正在混合 WinForms 和 WPF 控件/代码。您订阅的事件有 WPF KeyEventArgs 而不是 WinForms 的默认值 KeyEventArgs
-
你为什么要通过
ref传递panelRichText? -
@flydog 我正在使用面板在运行时保持文本控件。
-
@IvanStoev 没有使用 WPF 控件 - 我想你看到了我从以前使用的一些代码中复制的控件名称“WPFControlName” - 我现在重命名了它,但它只是分配的控件名称
-
我看到您在函数中使用了
panelRichText控件,但您没有更改参数的值。你不需要通过 ref 传递它。
标签: c# winforms events casting keyevent