How to disable mouse buttons in C# WPF? - c#

During big files loading I want to disable mouse buttons to unable user to click on UI elements and triggers events.
Edit
When I am loading big size file in my WPF Caliburn Micro application I changed IsEnabled property of Listbox to false, because I want to disable a button during this process.
Next when file is loaded I changed IsEnabled property of Listbox to true.
After that every click on disabled button raised events and I did not want that.
I don't know how to remove this events, and where there are stored, so i thought that the easier way to solve this problem is to disable mouse buttons during file loading process. But it is also not easy...
Thank You in advance!

If you only want to disable mouse buttons, the user can still use the keyboard. So you need a different technique.
You can add a hidden Gird with Opacity="0.5" to your window. When you want to prevent the user from using the window, just call visible the grid.

What about overriding SelectionChanged event on ListBox and setting it to Handled = true when loading big data?

Related

WPF: Restore keyboard focus after re-enabling container

I have a Window with a Grid with two TextBoxes in it. One of them has keyboard focus. If I disable the Grid and then re-enable it some time later, the keyboard focus is not restored to the TextBox that previously held it.
What actually happens is that the cursor is shown but not blinking...
I've tried setting the focus on the TextBox in Grid's IsEnabledChanged event, but none of the approaches I've tried work (Keyboard.SetFocus, txtBox.Focus, FocusManager methods...)
NOTE: I disable and enable the Grid in Dispatcher thread, so it's not a threading issue. Also, Keyboard.FocusRestoreMode is set to Auto.
Has anybody faced this problem? Is there a way around this? What I've managed to deduce is that when Grid's IsEnabledChanged fires, the TextBox is still disabled. But I could't find an event that would fire when Grid's content is enabled also.

WinForms Button Click event and similar events depend on focus

I have the next situation:
there is a client app with a Form
the Form contains a few TabControl's
there are different controls on TabPage's of TabControl's
when the user clicks on any control, I need to activate the TabPage that is a parent of a control. For that I use a TabPage Enter event
when the TabPage gets activated, I need to make request to the server app, and I put focus to a hidden TextBox to disable UI
The problem is, when I click on a Button on another TabPage, in TabPage.Enter event handler I take focus to my hidden TextBox, and it seems like Button click event doesn't have enough time to be processed. When I put timer and handle TabPage.Enter event after 100 ms, the Button click event seems to be fired well. Same thing happens to all the controls: CheckBox doesn't get checked, RadioButton too. I wouldn't like to use timer, as that is not a stable solution.
Any ideas how could I make TabPage to process all mouse events before I take focus to hidden TextBox? I tried to use Application.DoEvents(), but that didn't help.
You are using a wrong event for a wrong control for what you are trying to do.
Enter event for TabPage is going to be fired when that page becomes an active control of the current form which might not happen under certain conditions. Instead, you need to use Selecting or Selected event of TabControl, depending on whether you want to cancel switching to a different tab or not (see TabControlCancelEventArgs parameter of Selecting event). In your case, Selecting event would be more appropriate since it won't allow switching to a selected tab until event is complete (unless you're doing an asynchronous request to the server). Additionally, you may no longer need to use the hidden TextBox.
UPDATE
Regarding comments to OP, when you have 2 (or more) TabControls on a form and you want to get notified when you press a button in any tab of a different TabControl, you can use Enter event for TabControl. Use a global variable to store which TabControl was activated in the Enter event and then send server request for a currently active tab of that activate TabControl.
If this doesn't work for your scenario, then you need to carefully examine your workflow and see if it can be improved in relation to what you want to accomplish.

Tab control - wait until control become enabled

I have multiple controls on one form,and when i select some value from combo box(for example 1) next control became enabled, else next control stay disabled.
Problem is that if i just press 1 and tab, after that next control became enabled, but program jump over it just like control are still disabled, and tab control selecting next control.
I need to find way how to tab check is control become enabled and go on this control,and if control are still disabled that go on next enabled control.
Thanx
You created a mousetrap for the user, very hard to escape from. Technically you can handle the keyboard navigation by trapping the Tab key before it can be used to navigate but the user still has an unsolvable problem when he wants to use the mouse to change the focus. He has nothing decent to click on.
You'll need to re-think your UI design. One possible solution is to change the ComboBox's DropDownStyle to DropDownList. Which ought to be pretty appropriate if you use its selected item to enable other controls, there should only be a limited set of valid selections. If that's not what you want then you need to do something drastic. Not necessarily limited to hiding controls instead of disabling them.
This is probably caused by the event of the combo-box you using to control your flow.
The "Changed"/"Value changed" events in most languages fire up after the control has lost focus.
You forgot to add a tag for the UI technology you are using.
If you are using WinForms, then you can try to execute the SelectNextControl method on your control that the user just edited. This will find the 'next' control for you, and activate it.
Lets assume it's winforms (playing with disabled/enabled like this in wpf is.. against mvvm rules).
Firstly, ensure what tab order/index of your controls is ok. To test, if they are all enabled, then pressing Tab should go through them in the right order. This can be seen easily
Next thing is to choose one of many possible solutions, to make 1,Tab to work:
disable Tab key navigation at all;
make controls to pass control (focus) to specific control (making tab order irrelevant);
use SelectNextControl (work best for custom controls, which when will support that tab-flow schema);
prevent focus changing, do all logic, change focus (theoretically).

WPF Prevent button from taking focus from any other control

I have an "On screen keypad" with some up/down/left/right/select buttons.
The select button is effectively a click and the arrow keys fire the associated up/down/left/right key.
The problem is that when selecting a combo box, I can't press the down/up buttons to navigate the items in the list. It is because the combo box auto closes when loosing focus. I can see similar problems happening with other controls, so I would like to see if there is a way to do the following.
For certain buttons (up/down/etc), when clicked, fire the click event, but don't take focus from w/e currently has the focus. This would allow the combox dropdown to stay open while pressing up/down to navigate through the items.
I have tried to set Focusable=False on the navigation buttons but the focus is still taken away from the combo box and the dropdown closes.
Any ideas/suggestions?
Thanks in advance
This isn't happening because of anything your Buttons are doing so changing their focus state won't make any difference. ComboBoxes close when you click anywhere outside of them, including empty space, non-interactive controls, other windows...

Making a "click-out" event in C#

Is there any simple way to make a method that gets called whenever the user clicks out (or changes focus in some other way) from a text box in C#? I'm not really familiar with the way events are handled in C# - I rely on double-clicking a control to automatically generate the btn_Button_Click method.
Thanks!
Try Control.OnLostFocus, which occurs whenever the control loses focus.
You can get a list of events in the properties window for the control. If its not already visible, display the properties window from the view menu. Then select the control you want to add an event to, click the lightning bolt in the properties window (shows events) and add the event you need.

Categories

Resources