Set TextBox enabled to input without Keyboard.Focus() method - c#

I have a small window in WPF app, it's contain TextBox and Button. When user click another button I need to make TextBox enabled to input text, and besides that I need to set KeyBoard.Focus() to the Button. Something like that:
private void Show()
{
textBox.Focus();
KeyBoard.Focus(button);
}
However, its didn't work. Can you help me?

You can't have more than one item have keyboard focus. If you want the user to be able to press Enter after entering text, set IsDefault to true on the second button.

Related

Cannot set focus on Text Box

I have a form with 4 textboxes. This form is viewed inside a split container panel.
I can enter the values inside the text box but after the value, I click on the text I entered to modify a value but it is not letting me click, there is no cursor coming on the text box, the only thing I can do is backspace or select all and delete.
I cant click anywhere on the middle of the text entered.
If I check the focus I see it is false.
Can someone tell me what could be the problem and how to set the focus to the text box?
To show the form on the panel this is what I am doing
splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
splitContainerControl1.Panel2.Controls.Clear();
myform.TopLevel = false;
myform.FormBorderStyle = FormBorderStyle.SizableToolWindow;
splitContainerControl1.Panel2.Controls.Add(myform);
myform.Show();
I have tried the below, but not working, I see focus still false.
myform.Focus();
myform.textbox_latitude.Select();
Thanks,
Try using this.ActiveControl:
this.ActiveControl = textbox_latitude;
Or if you want to focus it on the child form:
myform.ActiveControl = textbox_latitude;

On windows forms, set tab focus to a button

I have created an Outlook add in which at some point displays a windows form with four buttons present on it. I am trying to default the focus to the first button, however the visual "selected" border will not appear around the button whenever I default this button as the focused one on start.
Any ideas how I could achieve this?
You can use either of these options to set the focus on a control in Load event of the form:
this.ActiveControl = this.button1;
this.button1.Select();
this.Show(); this.button1.Focus();.
You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to true.
After selection the button, the border of the button will be drawn in a way that shows it's the active control, but the focus cues will not be drawn.
As a quick and dirty fix, you can send a Tab, and a Shift + Tab to your form:
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");
If you are interested to change the standard behavior of Button to see focus cues when you select button in code or using mouse, you can create your own button inheriting Button and override its ShowFocusCues to return Focused value. You can read more about it here:
public class MyCustomButton : Button
{
protected override bool ShowFocusCues
{
get { return this.Focused; }
}
}

Creating an inputbox with radiobuttons c#

I'm currently making a simple C# game for a college assignment, wondering if it's possible to have radio or checkbox (or something similar) inputs upon loading, in an external window.
I'm currently using this to get a username:
public Form1()
{
InitializeComponent();
Initialize();
GlobalVar.Username = Microsoft.VisualBasic.Interaction.InputBox("Welcome to EasiGame, Please enter your username.", "Welcome", "Player1", -1, -1);
label4.Text = GlobalVar.Username;
}
This references VB, and that works great, however could I include radio buttons or something in this box, or a separate box, to grab user input for a difficulty setting.
Thanks in advance!
You would have implement your own form. Assuming you're using a recent version of Visual Studio:
Click Project > Add Windows Form.
Set the FormBorderStyle property of the Form control to FixedDialog to prevent
the user from resizing the form.
Set the MinimizeBox and MaximizeBox properties to false.
Set the StartPosition property to CenterScreen or CenterParent.
Add a button to the form with the text OK and set the DialogResult
property to OK.
Add a button to the form with the text Cancel and set the DialogResult
property to Cancel.
Change the AcceptButton property of the form to reference the OK button.
Change the CancelButton property of the form to reference the Cancel button.
Add a TextBox and a RadioButton or CheckBox control to the form.
Press the F7 key to open the code view for the form.
Implement a property of type string that returns the Text property of
the TextBox control.
You'll also need to come up with a way of getting the difficulty from the
form; I'll let you figure this part out for yourself :)
To show the form, create an instance and call the ShowDialog() method.
This will block the calling method until the user has clicked a button.
The ShowDialog method will return a DialogResult, which can be used to
determine which button was clicked.

want to stop user input by messagebox but bypassed by a Enter keydown

I'm coding a windows form application running on a barcode scanner.
The plantform is .Net2.0CF/C#.
What i want is whenever user input wrong, the app will pop a messagebox and block the next input(actually,a scan action) until user click the OK on the screen.
But normally the user will continuously scan the next stuff as they didn't find anything went wrong, this will insert a Enter keydown so the messagebox will be closed, in one word, the messagebox does not stop the user.
How can i code this? Below is a very simple code snippet
private void tb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "Return")
{
if(!ValidateInput(tb.Text))
MessageBox.Show("Error");
}
}
You can create your own window (Form) that displays the error message, but does not react on the enter key.
It should contain a button which the user can click (as you wrote), however you need to make sure the button does not have focus when the window is displayed. (Because if it had focus, pressing the return key will "click" the button.)
A simple way for doing this is adding another control which has TabStop set to true (e.g. a textbox, another button) and which has a lower TabIndex property than the button.
Additionally, maybe you might want to do a
System.Media.SystemSounds.Beep.Play();
when showing the window to draw the user's attention to the window.

How can I set up my winform button so that I can either click the button or press enter

I have some buttons on a winform that I would like to have the option of either clicking the button or press enter but I cannot figure out how to do it. Is it even possible?
Set the AcceptButton property on your form to the button you want.
If you want to make a button the default button on a form you should set the property AcceptButton.
In this way, if another control in your form has focus, pressing enter will close the form.
The same process is valid if you want a cancel button (a button connected to the Escape key). This time you set the CancelButton property on the form.

Categories

Resources