WinForms ComboBox - Event when Text Input is Clicked Into - c#

I have a feature request that when a ComboBox is 'clicked into' that it clears the text so that the user can start entering in new data to search. Does anyone know of a way to hook into this? The 'click' event is raised on when the text is clicked as well as when the drop down arrow is also clicked (which opens up the drop down with items). I only want it to happen on the first, not the latter.
Right now I'm capturing the click event and filtering on the DroppedDown property like so:
if(!comboBox.DroppedDown)
{
// clear selection
}
This seems to work most of the time, but bugs out frequently as well... so its not 100%.
If anyone knows of a proper way to do this I would appreciate!

Don't handle the click event. For one thing, it won't fire if the user tabs the focus into the control. Use the Enter event which fires when the control receives focus. And rather than clearing text you should just select it all which will give the best of both worlds:
1) The user can start entering new text which will clear any old text or
2) tab past the control and leave the contained text as it was.
If you always remove the previous text you may anger users.

Try the "Enter" event. It happens when a control gains focus on the form.

Related

TextBox receiving focus when clicking on ListView

I'm struggling to make a custom autocomplete style control for my application using a list view. In one form, the custom control is working as expected, however in another, when clicking on the ListView, rather than the ListViewItem being selected, the first TextBox in the form is selected instead.
Rather than clog up the question with a wall of code, here is the code for the AutoCompleteListView.
Here is an image of the form. Items are redacted due to data protection:
Also, here is a .gif showing the behaviour of the form. What I'm doing in the aformentioned gif is attempting to click on the first item within the ListView, however, as I do that, focus changes to the first text box in the form. This same behaviour occurs when clicking on any item, in any of the 3 text boxes.
I have attempted to trace the events using RuntimeFlow to watch as events fire, but it has not provided any clarity as to what is actually happening. If anyone could point me in the right direction, that'd be greatly appreciated.
My first guess is in ItemClick you are setting visibility to false, which means something else in the form needs focus, ie the textbox. So do a textbox.focus() in ItemClick to the desired textbox. And given that you will not have access to the parent form in this class, I recommend you make an OnClose event in the AutoCompleteListView and subscribe to it in the parent form, to set the focus to somewhere.

Event for "end edit" in a text box

I am using a textbox in winform (c#) and using the text to make consults in a database.
But I need constantly consult the text of the textbox every time that text changes. So for these, I use the KeyUp. But this event is too slow.
Is any event that just fires when the textbox editing has been finished ?. I consider for finish 2 conditions
The control lost focus.
The control has 200ms without keypress
You could use the LostFocus event, to capture when the user clicks on a control outside the textbox.
Otherwise, you'll need to choose from one of the existing events. (Listed here)
Come to think of it, you will likely have to capture multiple events. DragDrop if someone copies/pastes, for example...
You mean something like this?
Control.LostFocus
Provided that you consider finished as being when they click off the textbox.

Form only saving when inputs lost focus

I'm using a form (FormView) with databinding (ObjectDataSource) and all my input fields are bound by using '<%# Bind("field") %>'.
Everything works fine, but I have two problems (which I found various hints about like using this.Validate() or .EndEdit() - but none seem to work):
Entries are only saved after leaving the input field so it looses focus
Let's say I have a textbox with an ID of Name and enter "George". When I would tab to the next textbox or when I click somewhere else and click save - everything is saved. But when I keep the focus in the textbox the value is not saved. Why is this happening? What magic can I use to circumvent this (JavaScript to the rescue?).
I set a textbox's field value (element.value) via Javascript (upon selecting something in a combobox).
The same problem as above applies, only when I give the textbox focus and tab out the value is saved. This creates the problem that I only want the user to choose something in the combobox (the textbox is updated accordingly) and move on - I don't want the user to click into the textbox afterwards and tab out again.
Edit:
The second problem I resolved now by setting the focus onto my textbox via Javascript (textbox.focus();) and right after set the focus back to the combobox (combobox.focus();) and that does the trick - this seems fairly hackish to me, doesn't it?
I'm assuming this is fairly common, but my mighty Google fu hasn't help me find a simple solution.
A similar issue can crop up in Winforms development when working with DataGridView controls. I typically attach some logic to the submit button's Click event to cause the DataGridView to validate. I suspect a similar solution would work for you here.

C# - which event to populate a combobox before it's visible?

I'm currently updating it on click, but this results in the user being able to see the repopulation occur. Which other event can I use which will allow me to handle it myself, then show the combobox when i'm ready? ( after population)
I don't know what you're developing, but that combobox is probably on a window or so that will have an event that fires on show. Use that event to populate the combobox in.
[edit] Ah Winforms. Use the Load event.
[edit2] On each click eh.
Alright. I found a dirty solutions that advises you to override the WndProc and capture messages, but I think it's better to inherit the combobox and override OnDropDown to perform you populating before calling the ancestor's OnDropDown method.
You should populate the box when entered too, because a value may be selected using the keyboard (arrows) without even dropping down the box. You'll need both if you want it on each selection, because a click only causes the Enter event when the box didn't have focus before.
Have you tried the ComboBox.DropDown Event?
You could try to call SuspendLayout() before updating and calling ResumeLayout() after the changes.
combobox.add_HandleCreated triggers after the control is created as the form is loading

Issues retrieving selected text from IE browser

I'm creating a IE browser plug-in that needs to be kept updated about what text has been selected by the user, or if no text is currently selected at all. I learned how to get the selected text by reading here. This is my code for doing so:
var doc = browser.Document as IHTMLDocument2;
var selection = doc.selection as IHTMLSelectionObject;
var range = selection.createRange() as IHTMLTxtRange;
string selectedText = range.text;
However, I am having difficulty determining the event handler(s) from which to access range.text to update my plug-in. Using the selectionchange event alone does not work in all cases. When the user deselects the text by clicking directly on the selection, as opposed to clicking on a different part of the webpage, range.text still contains the old (non-null) value when selectionchange is raised. A workaround is to also listen for click events. A click event is raised immediately after selectionchange, but at which point range.text is finally null. However, I've encountered a further problem for which I haven't found a solution. That is, if a user double-clicks on a word, thus selecting it, the selectionchange event isn't raised at all. Plus, when the click event (or mousedown event or selectstart event) is raised, range.text is still null!
How can I solve this latter problem? Or is there an overall better approach I can take?
Try to use the ondragend event for the simple selection and the ondblclick event for the double click selection. If it doesn't work try to combine some events (http://javascript.gakaa.com/c/events.aspx)

Categories