Determine which key was pressed regardless form focus - vb.net

I wanna determine which key was pressed when im viewing a certain form. The problem is that if i want to check which key was pressed when im binding the "keypressed" event to the form, it won't show me which one was pressed if my focus is currently set to lets say, a textbox, or a label, or a panel and so on.
Any way to do that even if the focus is currently set to a Panel, i can still determine which key was pressed? thanks!

Related

Change focus on a Button in a Panel using the arrow keys

Assume you have a Form with ten Buttons on it, with the first one having the focus, so that it can be clicked by hitting the Enter key on the keyboard. Now, you can set the focus to the next button simply by pressing the Down arrow key. This works out of the box.
Then I constrain this functionality to the first three Buttons on the Form.
So, when the third Button on the Form has the focus and I press the Down arrow key, the first Button - instead of the forth button - should receive the focus.
Question:
how can I change the focus to another button outside the Panel with arrow key?

ActiveControl is hidden

To boil the situation down, I have a panel with 3 textboxes on it. When I hide the panel, the first textbox in the tab order for the panel gets assigned to Me.ActiveControl (The form), regardless of which one was focused when the panel was hidden.
I thought a hidden control could never have focus. I have other controls on my form where the issue was originally found, so I thought it would have to choose a non hidden control to focus on. Is this a bug, or designed this way?
I use a timer on my simple form that fires every 5 seconds, recording the active control name in a label.
I have been able to recreate your scenario and it revealed an interesting possibility (more on this later).
WinForms has the concept of the Selected (or activated) control. The Control.Select Method is related to the Control.Focus Method. The Focus Method-Remarks section documentation is relevant.
A control can be selected and receive input focus if all the following
are true: the Selectable value of ControlStyles is set to true, it is
contained in another control, and all its parent controls are both
visible and enabled.
...
Focus is a low-level method intended primarily for custom control
authors. Instead, application programmers should use the Select method
or the ActiveControl property for child controls, or the Activate
method for forms.
The ContainerControl.ActiveControl Property points to the last selected control.
The reason that the first control by tab order in the Panel is selected is due to the code that executes when the Panel.Visible property is set to false. The Visible Property setter calls SetVisibleCore that in turn calls SelectNextIfFocused that calls SelectNextControlInternal that finally calls Control.SelectNextControl that selects the your TextBox1.
This is where it gets interesting. At this point the Panel and TextBox are both visible. Therefore, the TextBox receives focus and retains it when the Panel is hidden. This condition allows for a hidden TextBox to have keyboard input and no rules about a hidden control not being able to receive focus are violated.

OSX: Move keyboard focus between textfields and buttons using tab key

I am trying to toggle between textfields and buttons on my view using keyboard's tab button. The switching between textfields work but it does not switch between buttons. The view is shown as below.I did not find enough resources online to proceed further. Does anyone know how to resolve this?
There is nothing you can do with that issue.
It turns out that in the System Preferences > Keyboard > Shortcuts there is a checkbox, where you can change behaviour of the whole system:
To move keyboard focus only between text boxes and lists
To move keyboard between any of controls
And by default first checkbox is pressed.
As an addition. By default, NSWindow assigns an initial first responder and constructs a key view loop with the objects it finds. You can also change key view loop by calling this method: setNextKeyView.
For example,
[firstTextBox setNextKeyView:secondTextBox];
[secondTextBox setNextKeyView:secondButton];
[secondButton setNextKeyView:firstButton];
[firstButton setNextKeyView:firstTextBox];
This means that for users who expect moving control focus through all controls, this will work. And for those who have disabled this feature in settings, this won't work.
You can right-click on text field, drag "nextKeyView" and drop on another text field that you want to focus next when user press tab. Look like my picture below:
To add to #mjonka's answer, To move focus between controls is dependent on user's keyboard-shortcuts settings. What could be alternate solution is to just selecting your desired action button in your xib file and setting "Key Equivalent" value field to "enter" key in Attributes inspector as shown below. Same thing can be done for cancel button by setting "Key Equivalent" value to "Esc" key.

vb.net winforms datagridview how to tell where the user clicked in a cell

I am adding a custom column to a custom datagridview. One of the options this new datagridcolumn has is the ability show a value and then a button to allow the user to click on the button and something will happen. The button only takes up the right portion of the cell and the rest is a value. This button and value are displayable always and the button should always be able to be clicked on. In datagridview's display mode the value and button are painted. What I need help on is how to tell whether the user clicked on the button portion of the cell. Can someone please provide example code on how to do this?
Thanks,
Greg
Never mind I figured it out. I just put some logic in the cellclick event of the datagridview to get the GetCellDisplayRectangle and converted that to screen points and got where the mouse is on the screen and did a bounds test with the rectangle contains method.

UI picker Trying to do it the right way

I am trying to use a UI picker like a combo box in C#. User clicks on a TextField, I have set the inputView to be the picker. This works fine. User clicks on the row and I get the item on that row and place it in the textfield.
However, when the application loads, this picker is visible! I don't want it to be visible. I want to be able to make it visible when the user clicks on the textbox and then when he or she moved off I want to hide it. How do you do this?
Also , is it possible to have the picker only if the user wants to do a lookup like CTRL click. Otherwise, it uses the keyboard? How do you toggle back and forth between the keyboard and the picker.
For example suppose user wants to add a new book. He/she clicks on the authors textfield and he/she either types the author ( a new author that does not exist in the database) or select and existing author from the list.
Thanks
The best way to do this is using the inputView property of the textField. You can just set that to the picker and it should handle the rest for you.