c#winforms事件在转义时还原文本框内容
在2008 Express中使用c#。我有一个包含路径的文本框。我在休假事件结束时附加了一个“\”。如果用户按下“退出”键,我希望恢复旧内容。当我输入所有文本并按下“Escape”时,我听到砰的一声,旧文本无法恢复。这是我到目前为止所拥有的c#winforms事件在转义时还原文本框内容,c#,textbox,key,escaping,event-handling,C#,Textbox,Key,Escaping,Event Handling,在2008 Express中使用c#。我有一个包含路径的文本框。我在休假事件结束时附加了一个“\”。如果用户按下“退出”键,我希望恢复旧内容。当我输入所有文本并按下“Escape”时,我听到砰的一声,旧文本无法恢复。这是我到目前为止所拥有的 public string _path; public string _oldPath; this.txtPath.KeyPress += new System.Windows.Forms.KeyPressEventHand
public string _path;
public string _oldPath;
this.txtPath.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtPath_CheckKeys);
this.txtPath.Enter +=new EventHandler(txtPath_Enter);
this.txtPath.LostFocus += new EventHandler(txtPath_LostFocus);
public void txtPath_CheckKeys(object sender, KeyPressEventArgs kpe)
{ if (kpe.KeyChar == (char)27)
{
_path = _oldPath;
}
}
public void txtPath_Enter(object sender, EventArgs e)
{
//AppendSlash(sender, e);
_oldPath = _path;
}
void txtPath_LostFocus(object sender, EventArgs e)
{
//throw new NotImplementedException();
AppendSlash(sender, e);
}
public void AppendSlash(object sender, EventArgs e)
{
//add a slash to the end of the txtPath string on ANY change except a restore
this.txtPath.Text += @"\";
}
提前感谢,您的txtPath\u CheckKeys函数会将路径指定给旧路径,但不会实际更新文本框中的文本。我建议将其改为:
public void txtPath_CheckKeys(object sender, KeyPressEventArgs kpe)
{
if (kpe.KeyCode == Keys.Escape)
{
_path = _oldPath;
this.txtPath.Text = _path;
}
}
这个活动可能会对你有所帮助
它描述事件的触发顺序。因此,选择最适合您需要的事件将更容易实现此功能
这可能是太多的需要,但尝试控制可能也有帮助
让我知道它是否有用。我建议使用
kpe.KeyCode==
Keys.Escape`而不是kpe.KeyChar==(char)27
。很好。。。当通过键盘激活按钮时,LostFocus事件并不总是会触发,但验证事件会触发。@Will Marcouiller:我经常被明显的事情烧焦。。。我现在倾向于先找那些。谢谢你,威尔!!你是个天才。就这样。我还以为砰的一声就是击键迷路了呢。这个if语句(kpe.KeyChar==(char)27)是我唯一能做到的方法。Intellisense没有给我kpe.KeyCode选项。我正在运行VS2008 Express。再次感谢,