TextBox TextChanged event does not fire when Visible = False? - c#

I have a textbox bound to a datasource. The textbox's TextChanged event updates another textbox.
The problem is, I do not want the first textbox to show, so I set its Visible property to false.
However, now the TextChanged event does not fire!
I can work around it by setting Visible=True, Left=-100000 on form load, but I'd like a proper solution.
Can anyone offer an explanation?

Set your textbox.Visible = false in the FormLoad event instead of in the designer. It has to do with handle creation. If the textbox is not visible during construction, then the handle is not created. If the textbox is made invisible after construction, then the handle will have been created and events will occur.
See this discussion on MSDN.

An alternative solution to the accepted answer is to set up the TextChanged listener on Loaded, this works for me just the same (in Silverlight at least) and keeps the designer view as it should be.

What type of datasource is it? It might have an event that you can use directly instead of using a textbox to listen for an update.

If Visible is equal to false then the Control is not rendered. Therefore it will be unable to fire an event.
Instead, set the style to display:none. You can set/unset this programmatically using the Attributes collection:
MyTextBox.Attributes.Add("style", "display: none");

Related

c# DataGridViewCheckBoxColumn suppress default behavior when clicked

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.

WPF Validate/Setter calls in wrong order

I have a textbox inside a TabControl. The textbox binding has UpdateSourceTrigger=LostFocus. The textbox uses Attribute based validation from the data model. This validation is working correctly.
In the TabControl.SelectedItemChanged event I call modelObject.Validate() and prevent the switch to a different tab if an error occurs.
The problem I have is that the order of execution is backwards. The validate call occurs before the property setter. In the case of an invalid field I am able to switch away from the tab even though an error has been detected.
How do I get the order or these events ordered properly?
Is there a way to cancel TabControl.Items.CurrentChanging?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d8ac2677-b760-4388-a797-b39db84a7e0f/how-to-cancel-tabcontrolselectionchanged?forum=wpf
Seems to work by subscribing the CurrentChanging event. Can cancel tab changing action by setting the CurrentChangingEventArgs Cancel to true
WPF is definitely incorrect here. However, the fix is really simple.
In the SelectionChanged event handler call {whatever your tabcontrol is called}.Focus(). This immediately forces the lostfocus event for the textbox (which forces the setter to be fired) and solves the problem.

What Textbox event is raised right after bound data source is updated?

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.

Hold focus on textbox that do something in the textchanged event

I have a problem:
I handled textchanged event of a textbox. When the event fires I do something in the UI (like add a row in a listbox) and the textbox lost the KeyboardFocus.
How can I hold the KeyboardFocus on that textbox?
Thanks
Write the following
this.MyTextBox.Focus()
at the end of your handler.
or make your UIElement.Focusable = false. it should help.
You can set the focus back on the textbox in the code-behind where you handle the event using textBoxName.Focus();
Focus can be set in the XAML using
FocusManager.FocusedElement="{Binding ElementName=textBoxName}"
but if the focus is lost when you add a new element then you may need to use code-behind to reset it.

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

Categories