I have eight radio buttons on a user control. Each of them has or will have their own click handler. The first one has its click handler called when the user control is added to the main window. Is this normal behavior for Windows Forms (I am relatively new to .NET from a Java background)
This effect is likely the result of the default control selection rather than the control being added to the form. When the form finishes loading, one of the controls on the form will become the active control/have focus. If that control is a radio button, the button becomes checked, which will fire events like Click and CheckChanged (unless the radio button Checked property was already set to true). Depending on the Checked property value of the other buttons, you may see their CheckChanged events fire as well.
To test this out yourself, change the TabIndex property value in the designer so that some other control on the form will have the lowest index. This will make that control have focus on startup instead of the radio button(s). When this happens, you should not see the Click event being fired when the form is loaded.
Related
I have a XAML page where, upon tapping an "Add" button, I create a UserControl and add it to my Container. This UserControl has a Rectangle, a TextBox and a few buttons. My problem is that on the first click of the "Add" button, the focus is given to the Textbox. Clicking on the "Add" button repeatedly creates more UserControl instances, but focus remains on TextBox1. If I now click on Textbox5, that box gets focus, but as soon as I click outside, focus returns to Textbox1. I would like focus to be given to textboxes, only on click.
I have tried setting IsTabStop = true in XAML, and intercepting the tapped event and setting it to false, but that doesn't have a noticeable effect.
What worked perfectly is setting the TextBox's TabIndex = 2, and creating another button before it, and setting it's TabIndex = 1. But I lose this benefit when I set the Button's Visibility = Collapsed. The TextBox is the left most control, so it must have the lowest TabIndex (well technically, there is a Rectangle to the left of the TextBox, but since a Rectangle is not a Control, it cannot have a TabIndex).
How can I fix this?
I believe what is happening is when you are adding more controls, it is resetting the current tab stop index. So every time you add a control, the TextBox of TabStop 1 is being focused on.
What you can do, is after you add the control, call Focus() on the control that you want to be focused.
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.
I have a user controls on winforms that contains a toolbar, the problem is whenever the application loses focus then i have to click the toolbar button twice to trigger the click event. I know this is because the first click is for the application to have the focus then then second click will be the actual click event. This only happens to toolbar control but not on other winforms controls.
I got tiered finding the right solution to solve this with ToolStrip control, what I did was I just changed it to Button control set its to Flatstyle then added some mouse hover to mimic the ToolStrip.
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...
The problem like this, I have a groupBox which contains two radio buttons, when I run the form, the first radio button get checked immediately, so I tried the following:
Set the check property for this radio button to false in Load form.
Set the check property for this radio button to false in the form constructor.
Change the tab index property for this radio button, the selection moved to the next radio button in the form.
None of the above worked with me, any suggestions??
You could try setting it to false in the form SHOWN event instead of the form LOAD event as outlined in this question.
As soon as any of the radio buttons get focus it'll be selected, so you need to set initial focus in the form to another control than any of those radio buttons (worst case I suppose you could have a hidden radio button or other control and give that focus, but I'd not recommend it since it looks funny).
The intent of a radio button set is to provide choice between a set of distinctive and exhaustive values.
That means, that at all times, one and only one radio button should be selected.
If this functionality does not suite your application logic, maybe the logic is flawed, or maybe radio-buttons are not the best UI solution.
As mentioned, a radio button group displays its behavior as soon as any of the radios get focus, which can happen even with just tabbing around the form, so basically the behaviour of the form depends on the user behaving nicely.