简体   繁体   English

在c #Windows窗体应用程序中单击鼠标外的文本框时是否会触发事件处理程序?

[英]Is there an event handler that is fired when mouse is clicked outside textbox in c# Windows Form application?

i'm working on c# windows form application. 我正在研究c#windows表单应用程序。 I'd like to know if there is an event handler for textbox control that is fired whend mouse is clicked outside the textbox.I tried "Leave" and "LostFocus" event Handlers but these are fired only if the mouse is clicked on a clickable control like textbox , button or listbox but if the mouse is clicked on Form or tab or any container the handler is not fired.(To make the question more clear > i want it like the action of hiding AutoCompleteListBox of textbox when mouse is clicked outside the textbox). 我想知道是否有文本框控件的事件处理程序在文本框外单击鼠标时被触发。我尝试了“离开”和“LostFocus”事件处理程序,但只有在点击鼠标时才会触发这些事件处理程序像文本框,按钮或列表框一样控制,但是如果在窗体或制表符或任何容器上单击鼠标,则不会触发处理程序。(为了使问题更清晰>我希望它像在外面单击鼠标时隐藏文本框的AutoCompleteListBox的操作文本框)。 I hope you understand and i hope there is a direct way to this issue. 我希望你理解,我希望有一个直接解决这个问题的方法。 Thanks 谢谢

** Ok it seems that the question is not sufficient clear. **好吧,这个问题似乎还不够明确。 I'm making a control like the AutoCompleteTextBox but it list a the items in a different way of the AutoCompleteBox.When the user writes in the textbox this control appear. 我正在制作一个类似AutoCompleteTextBox的控件,但它以AutoCompleteBox的不同方式列出项目。当用户在文本框中写入时,此控件出现。 Sometimes the user doesn't want to choose any of the items so he wants to hide the control by anyway. 有时用户不想选择任何项目,因此他想要隐藏控件。 I want the user to be able to hide this control not just by choosing one of the items or clearing the textbox but also by cliking on any part of the form whatever the type of the control was."Lost Focus" and "Leave" handlers don't fire the action when the user click on form or TabControl or panel.Hope you understand. 我希望用户能够隐藏这个控件,不仅可以通过选择其中一个项目或清除文本框,还可以通过克服表单的任何部分来控制控件的类型。“Lost Focus”和“Leave”处理程序当用户单击表单或TabControl或面板时,不要触发操作。希望您理解。

May I suggest a different approach? 我可以建议一种不同的方法吗? You want a smooth, hidden "AutoComplete", right? 你想要一个流畅,隐藏的“自动完成”,对吧? Imagine that the user has entered some partial information and wants to leave the textbox. 想象一下,用户输入了一些部分信息,并希望离开文本框。 Now, suppose the user doesn't use a mouse, but just tabs out of the textbox into another. 现在,假设用户不使用鼠标,而只是将文本框中的选项卡标注到另一个文本框中。 Shouldn't that make the textbox autocomplete? 不应该使文本框自动完成吗?

Tying the behaviour to a click outside the textbox means that, for some reason, you expect the user not to click on another control, but on the form (or immediate container to the textbox), which just isn't standard behaviour. 将行为绑定到文本框外部的单击意味着,出于某种原因,您希望用户不要单击另一个控件,而是单击表单(或文本框的直接容器),这不是标准行为。 Why would users click on nothing? 为什么用户点击什么?

Your best approach is with the Leave and LostFocus events. 最好的方法是使用Leave和LostFocus事件。 Tie both to the same autocomplete function. 将两者绑定到相同的自动完成功能。

The auto-complete list is shown as long as the text box as the focus. 只要文本框为焦点,就会显示自动完成列表。 Clicking outside of the text box is indeed one way to get the text box to lose focus. 单击文本框外部确实是让文本框失去焦点的一种方法。 But that does require that you click on something that wants to receive the focus. 但这确实需要您点击想要获得焦点的内容。 The form doesn't, it has no use for it, that's why it doesn't work. 表格没有,它没有用,这就是为什么它不起作用。

Getting a window to see mouse events that occur outside of the window requires capturing the mouse. 获取窗口以查看窗口外发生的鼠标事件需要捕获鼠标。 Winforms supports that with the Control.Capture property. Winforms使用Control.Capture属性支持它。 Set it to true and all mouse message are directed to the control, even if the mouse is no longer close to the window. 将其设置为true并将所有鼠标消息定向到控件,即使鼠标不再靠近窗口也是如此。 You'd use the MouseDown event and check the mouse position against the window client area to detect that the mouse was clicked outside of the window. 您将使用MouseDown事件并检查窗口客户区域的鼠标位置,以检测鼠标是否在窗口外单击。

That's the way that menus work for example. 这就是菜单工作的方式。 Click outside of the menu and it disappears. 单击菜单外部,它将消失。 This is however very tricky to get right for a text box. 然而,这对于文本框来说是非常棘手的。 The mouse is only captured until you cancel it yourself or you click a mouse button. 只有您自己取消鼠标或单击鼠标按钮才能捕获鼠标。 While there's only one reason to click the mouse for a menu, to click a menu item, there are lots of reasons for the user to click the mouse for a text box. 虽然单击鼠标进行菜单只有一个原因,但单击菜单项时,用户有很多理由可以单击鼠标来显示文本框。 Selecting text, changing the insertion point, bringing up the context menu. 选择文本,更改插入点,调出上下文菜单。 All operations that will cancel the capture, you'd have to re-capture. 所有将取消捕获的操作,您必须重新捕获。 Which is very hard to do. 这很难做到。 The context menu makes it especially tricky because you can only re-capture after it disappeared. 上下文菜单使它特别棘手,因为你只能在它消失后重新捕获。 Now you have to write code that detects it closing, not directly supported in Winforms. 现在你必须编写检测它关闭的代码,而不是在Winforms中直接支持。

This is not practical, which is why the auto-complete list only disappears when the text box loses the focus. 这是不实际的,这就是为什么自动完成列表仅在文本框失去焦点时消失。 Maybe you can make it work by restricting the way the text box work. 也许你可以通过限制文本框的工作方式使其工作。 Disabling the context menu for example. 例如,禁用上下文菜单。 Not so sure that's a good idea. 不太确定这是一个好主意。

You can make it work by screening mouse messages before they are delivered. 您可以通过在交付之前筛选鼠标消息来使其工作。 It is a very heavy handed approach but will work. 这是一个非常沉重的方法,但会起作用。 You get the messages by implementing IMessageFilter. 您可以通过实现IMessageFilter来获取消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何捕获文本框外的鼠标事件 - c# how to catch mouse event outside textbox 在Windows窗体中单击“关闭”按钮时的事件处理程序 - An Event Handler when Close Button is clicked in windows form C#-使用鼠标在Windows窗体上调整TextBox的高度 - C# - Adjust height of TextBox on Windows Form using Mouse 我应该如何编写C#Windows窗体应用程序的事件处理程序? - How should I code my event handler for my C# Windows Form Application? 如何检查按钮是否在C#中的Windows窗体应用程序中的另一个按钮单击事件内被单击 - how to check whether a button is clicked, inside another button click event in windows form application in C# 使用Windows窗体控制OS鼠标事件 - C# - Control OS Mouse Event using Windows Form - C# 在c#中的事件处理程序中定义了事件处理程序之外的变量时,如何访问它? - How to access a variable outside an event handler when it is defined within the event handler in c#? C#基础知识-TextBox事件处理程序 - C# Basics - TextBox event handler C#Windows窗体应用程序-每次都不在文本框中打印值 - C# Windows Form Application - value not printing in textbox for each time 如何在C#Windows Form应用程序中验证TextBox值是否为字符串 - How to validate TextBox value is string in C# Windows Form application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM