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)
Related
I want to use an event or something what is fired when I am typing in the cell. I tried SheetChange and SelectionChange events, which were not I need. Both of them are fired after things changed. Is there any way I can fire a event when I'm editing in a cell? Like some controls that have XXXEditing/XXXChanging events?
At least, you can detect when the cell is in "Edit mode", i.e. is accepting input, see:
https://www.codeproject.com/Articles/20267/Determining-if-Excel-is-in-Edit-mode
But you won't be able to detect when the user is typing anything, unless you are willing to use dirty Win32 API tricks.
There is no event against changing cell that starts before changing it.
change event fires after you have changed the content of the cel
selectionchange event fires when you change the selection
I have a WinForm with a DataGridView control. It contains a CheckBox column.
I use a CellMouseClick event with evaluation of the current column index for the evaluation. Another form opens after clicking on the check box regardless of the logical state of the check box.
Depending on the outcome of the input in the new form, I try to set the State of the check box.
Unfortunately, my setting is overridden by the default behavior of the check box.
I.e. if the check box was set, finally it is unset and vice versa.
My question is, if there is a possibility to suppress or work around this behavior. Possibly something like e.Handled ?
I have also used the CellContentClick event but the result is the same.
Use the event DataGridView.CellValidating. In the link there is an example how to cancel the event of the current cell.
When there is a checkbox, this event is raised only when the user leaves the cell.
To solve this problem use the event DataGridView.CurrentCellDirtyStateChanged Event and force the commit to change.
This is an example.
Thanks very much for your advice. Unfortunately using the way you described I can not achieve the desired effect.I use the Events CurrentCellDirtyStateChanged and CellValidating as suggested.
The behavior is something strange in my eyes.
Inside the CurrentCellDirtyStateChanged Event I checked the CellValue of the Checkbox before (it is true) and after (it is false) calling CommitEdit. The CellValidating Event is not raised. Cancelling the Event is not possible.
Ok, when leaving the Form - that has been opened by a click on the CheckBox -
I did not leave the CheckBox cell.
So I tried to solve the problem by leaving the CheckBox cell by setting the CurrentCell to another cell.
As a result the CellValidating Event is raised. But setting e.Cancel = true results in an Exception.
After some additional trying based on your suggestions I can achieve the desired effect without using the CurrentCellDirtyStateChanged and CellValidating Events.
After leaving the additional Form now I get the return value from the Form, set the CheckBox cell according to this value, set the CurrentCell to the next cell in the CurrentRow and call RefreshEdit.
The CheckBox state is always set to the correct value and is not changed if I leave the additional Form without changes.
That's what I want.
Perhaps this may help others who have similar problems.
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.
In windows forms, when I tab out of a text box, the bound data source value is updated. I'd like to capture the events right before and right after the data source changed. I think the OnLeave event is what I want for the before event. In the debugger, I'm not seeing the data source value changed. But, what event can I key off of for the after event?
I don't think there is an event that does exactly what you're asking, the closest I think you're going to get is to use the DataBindings and find your specific Binding and the you can catch the Parse event. But I believe this event fires before the data is pushed back to the source, so it's not much better than the LostFocus event.
The default event for TextBox DataBindings is DataSourceUpdateMode.OnValidation. When you tab out of the TextBox, the following events will fire:
Leave
Validating
(data source gets updated)
Validated
The Validating event has a CancelEventArgs parameter that allows you to cancel the leave attempt for the TextBox (the focus will remain in the TextBox).
If you use DataSourceUpdateMode.OnPropertyChanged, it will update the data source with every keystroke or text change.
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.