Control.Leave 由 MouseDown 触发,而不是 MouseUp

发布于 2024-10-20 06:29:34 字数 940 浏览 0 评论 0原文

我有一个 C# .NET WinForm。在表单中,我允许用户通过双击 ListView 来将项目添加到 ListView。这会将 TextBox 控件添加到 ListView 并将键光标放置在 TextBox 中,以便用户可以键入。

我检测到用户以多种方式完成了某个项目(例如按 Enter、Esc、Tab...),而且当他们离开 (TextBox.Leave) TextBox 时也是如此。

问题在于这组步骤:

  1. 用户通过将鼠标放在 TextBox 外部来触发 TextBox.Leave。
  2. 我将新项目添加到 ListView 中。
  3. 我在 ListView 中选择新项目。
  4. 鼠标松开,我刚刚选择的新项目失去焦点并被取消选择。

我想要的是 TextBox.Leave 由 MouseUp 触发,而不是 MouseDown 触发。我怎样才能做到这一点?

编辑:Cody 建议使用 ListView.LabelEdit 属性。以下是我尝试的结果:

listView_DoubleClick(...) {
  listView.LabelEdit = true;
  if(double clicked on existing listViewItem) {
     listViewItem.BeginEdit();  //this works as expected
  } else {
     var newItem = listView.Items.Add("");
     newItem.BeginEdit();       //this doesn't work, see below  
  }
}

仅当用户双击新项目将显示的位置时,对 newItem.BeginEdit() 的调用才有效。如果他们双击列表视图中的任何其他空白区域,则会添加新项目,但不会进入编辑模式。这是怎么回事?

I have a C# .NET WinForm. In the form, I allow a user to add an item to a ListView by double-clicking in the ListView. This adds a TextBox control to the ListView and places the key cursor in the TextBox so a user can type.

I detect that a user is done with an item in a couple of ways (e.g. pressing Enter, Esc, Tab...), but also when they Leave (TextBox.Leave) the TextBox.

The problem is this set of steps:

  1. User triggers TextBox.Leave by mousing down outside of the TextBox.
  2. I add the new item to the ListView.
  3. I select the the new item in the ListView.
  4. Mouse up occurs and the new item that I just selected, loses focus and is unselected.

What I would like is for TextBox.Leave to be triggered by MouseUp, not MouseDown. How can I accomplish this?

Edit: Cody suggests using the ListView.LabelEdit property. Here are my results trying that:

listView_DoubleClick(...) {
  listView.LabelEdit = true;
  if(double clicked on existing listViewItem) {
     listViewItem.BeginEdit();  //this works as expected
  } else {
     var newItem = listView.Items.Add("");
     newItem.BeginEdit();       //this doesn't work, see below  
  }
}

The call to newItem.BeginEdit() only works when the user double clicks where the new item will show up. If they double click on any other blank area in the listview the new item is added, but it does not enter edit mode. What's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-10-27 06:29:34

在另一个控件上按下鼠标会导致该另一个控件请求焦点,因此焦点移动会导致 TextBox.Leave 事件发生。阻止其他可能的控制请求焦点并不是一个非常可行的选择。但幸运的是,您只需要阻止 ListView 使用 MouseDown 来转移焦点即可。因此,您需要重写 ListView 的 WndProc,并且当发生 MouseDown 窗口消息并且您当前正在显示 TextBox 时,您会吃掉该消息。换句话说,您不允许基类处理它。

Pressing the mouse down on another control is causing that other control to request the focus and so the focus moving causes the TextBox.Leave event to occur. Preventing ever other possible control from requesting the focus is not a very viable option. But luckly you only need to prevent the ListView from using the MouseDown to shift focus. So you need to override the WndProc of your ListView and when the MouseDown windows message occurs and you are currently showing a TextBox you eat the message. In order words you do not allow the base class to process it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文