开发者

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. 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). 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. 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 T开发者_开发知识库abControl or panel.Hope you understand.


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. 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. Set it to true and all mouse message are directed to the control, even if the mouse is no longer close to the window. 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.

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜