How to set focus on textbox and button simultaneously in WinForm - winforms

I want to set focus on a button while caret is present in textbox and user can still enter data to textbox. But when user presses enter key, button press will be simulated.
I am currently using a work around to solve this problem by handling onKeyDown event and checking for enter key. But problem is there is no clue for user to understand this as there is not blue border around the button that indicates focus on button.
Here is a example of what I want to implement (user can enter text in textbox while focus is on :
I have tried to search on google and StackOverflow but could not find any relevant result.

This is a fundamental Windows principle. It's not possible to have 2 controls (windows) focused at the same time.
So the focus should be inside the text box. But you can get the visual indication needed by setting the ok button as AcceptButton of the form (you might also want to set cancel button as CancelButton).
In the form constructor, load event or using designer:
this.AcceptButton = okButton;
There is no need to handle KeyDown event - as soon as the text box is not multiline, pressing Enter while the focus is inside it will generate ok button click. The same applies for the button set as CancelButton when you press ESC.

Related

How to keep focus in MessageBox

I am using EditorGridPanel with cellEditor which acts nearly like Excel. We implemented Arrow Keys to move among rows and columns.While validating a row, if it does not match the validation rule we show a MessageBox and hope that focus does not move if it is not valid. But, after the MessageBox shows - focus moves to the next row/column. Another problem is, user have to click in the OK button of the MessageBox to remove that from screen. Can we have the focus on the invalid column editor and also focus on the MessageBox OK button - so that user can press Enter to hide the message and continue entry?
Please check our link. http://www.softworksbd.com/swazilandlmis/yyyy_stockdata.php
Only one element can be focused, so focus should go to the message box. AFAIK enter/escape keys works out of box with message box.
You can pass callback to the messagebox. In this callback you can focus desired cell in the grid. Like this:
Ext.Msg.alert('Title', 'Message.', function(){
this.startEditing(rowIndex, colIndex);
}.createDelegate(grid));
So after message box is closed, the focus will go back to the editor.

In WPF how to determine how focus received

I have a text box on a form and want to perform different actions when it receives focus depending on how the focus was received.
If the user clicks on the text box I want the standard method where the cursor is placed at the end of the text string inside the box. If the user tabs to the text box I want all of the text contained within the text box to be selected.
Is there an elegant way to accomplish this?
I suppose one thing you could try would be to set some application-level (or parent-level) tunneling events so you can toggle a flag indicating it was mouse clicked or keyboard pressed (PreviewMouseDown and PreviewKeyDown). Depending on what was previously pressed by checking that flag, you could do the appropriate action when the text box gets focus.

Control.Leave Triggered by MouseDown, not MouseUp

I have a C# .NET WinForm. In the form, I allow a user to add an item to a ListView by double-clicking in the ListView. This adds a TextBox control to the ListView and places the key cursor in the TextBox so a user can type.
I detect that a user is done with an item in a couple of ways (e.g. pressing Enter, Esc, Tab...), but also when they Leave (TextBox.Leave) the TextBox.
The problem is this set of steps:
User triggers TextBox.Leave by mousing down outside of the TextBox.
I add the new item to the ListView.
I select the the new item in the ListView.
Mouse up occurs and the new item that I just selected, loses focus and is unselected.
What I would like is for TextBox.Leave to be triggered by MouseUp, not MouseDown. How can I accomplish this?
Edit: Cody suggests using the ListView.LabelEdit property. Here are my results trying that:
listView_DoubleClick(...) {
listView.LabelEdit = true;
if(double clicked on existing listViewItem) {
listViewItem.BeginEdit(); //this works as expected
} else {
var newItem = listView.Items.Add("");
newItem.BeginEdit(); //this doesn't work, see below
}
}
The call to newItem.BeginEdit() only works when the user double clicks where the new item will show up. If they double click on any other blank area in the listview the new item is added, but it does not enter edit mode. What's going on here?
Pressing the mouse down on another control is causing that other control to request the focus and so the focus moving causes the TextBox.Leave event to occur. Preventing ever other possible control from requesting the focus is not a very viable option. But luckly you only need to prevent the ListView from using the MouseDown to shift focus. So you need to override the WndProc of your ListView and when the MouseDown windows message occurs and you are currently showing a TextBox you eat the message. In order words you do not allow the base class to process it.

Stop containing button from handling input - Silverlight

I'm working on a custom button where the content is sometimes a textbox. I'm doing this so I have a sort of edit-in-place where you can type text, hit enter, and then the textbox disappears and the button's text is what was typed.
So as a simple case, you could mock this up like so:
<Button>
<TextBox />
</Button>
The problem is if you hit the <space> or <enter> while typing it'll "click" the button instead of input those keystrokes into the textbox.
Now, like I said, this is a custom button so I can do whatever I need to in the codebehind to get this working.
Just because a TextBox appears over a Button does not mean it should be inside the button :)
You are better off with a custom control that has a button with optional Textbox over it. That way the TextBox will not interfere with the button beahaviour. If you put them both in a grid you will still retain a lot of dynamic layout control.
You will just need to expose properties and events of the button (and probably of the TextBox) on your user control.
If you need specific help, just ask.

WinForms Accept Button Annoyance

I have a base panel class that has (among other things) three buttons. I use subclasses of this base class in two different config dialogues. Both dialogues have an OK button set as the accept button.
In one of the dialogues, if I click one of the buttons in the base class, focus immediately returns to the OK button, so pressing the enter key works as expected.
In the other dialogue, focus remains wth the button in the base class that was clicked if it is enabled, or moves to the next button if the clicked button is no longer enabled.
There is no code that handles the base class button click events in either of the derived classes.
Ideas anyone?
I'm not sure what's going on in your first dialog because it doesn't seem to be operating the way I would expect it to. The second dialog sounds more like the standard behavior.
In Windows Forms, the AcceptButton property only comes into play when pressing Enter doesn't otherwise cause any actions. In both of your examples, clicking on a button should move the focus to that button, and subsequently pressing Enter would cause another click on that button.
In any event, I think it's generally preferable to stick with the Windows user interface guidelines and not automatically change the input focus back to the OK button. If the user clicks on one of the other buttons, the focus should stay there until they move it.
i don't know what language you are using, but the button class should have a focus method that will highlite it for enter pressing. in the click method, or when you open the dialog you can call this method to make the button you want get the form's focus
c#
myButton.Focus();

Resources