Set focus to element - c#

There are a button and a textbox. I added a "KeyDown" event to textbox so that when "enter" is pressed button gets clicked. Good, then I tried to give focus to textbox again but failed. In the code below I tried three ways but neither is working.
private void txt_addRemove_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
btn_BC_add.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
// 1.way
// IInputElement focusedElement = FocusManager.GetFocusedElement(txt_addRemove);
// 2.way
// Keyboard.Focus(txt_addRemove);
// 3.way
// txt_addRemove.Focus();
}
}

Add this
if (!textBox1.Focus())
{
textBox1.Focus();
}
What this does:
1. We check if the textbox is NOT focused.
2. If it is not focused, focus the control.
EDIT: How about this:
btn_BC_add.PerformClick()

Try focusing the textbox after the event handler is finished using:
Dispatcher.Invoke(() => { txt_addRemove.Focus(); })

couldnt you also try changing the focus from the button event handler? this might not be desirable if you dont want the focus to be on your textbox after a normal click of the button, but it should work.
Im guessing that your button click is generating a post back before the focus can be changed

Related

Prevent DataGrid from handling Enter-key

I have a readonly DataGrid showing items inside a window that is shown as dialog. This dialog also has two buttons, one to confirm and one to cancel the dialog. The confirm button's IsDefault property is set to true, the cancel button's IsCancel property is set to true. I want to keep up the default dialog button behaviour, i.e. confirming and closing the dialog when pressing the confirm button or ENTER or cancelling and closing it by pressing the cancel button or ESCAPE.
Unfortunately the DataGrid consumes the KeyDown event of the ENTER-key and selects the next row inside the grid. Because the KeyDown event is marked as handled by the DataGrid the dialog will not be confirmed and stays open. My desired behaviour is that the selected row is not changed and the dialog will be confirmed and closed.
I could create my own DataGrid-derived type an overwrite OnKeyDown as follows:
public class MyDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
base.OnKeyDown(e);
}
}
}
But I was wondering if there is another way without having to create a derived type to achieve my goal.
You can use the curernt DataGrid.
<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown"/>
private void DataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// This makes this key pressed event end here.
e.Handled = true;
// Do what you need to do here...
}
}

Do not handle button click when caused by key press

I have a WPF project. In this project at some point I'm dynamically making button in code behind like this:
private Button makeButton()
{
Button b = new Button();
b.Width = 24;
b.Height = 19;
b.Click += ButtonClick;
return b;
}
Where the ButtonClick is:
public void ButtonClick(object sender, RoutedEventArgs e)
{
// Do stuff...
}
Sometimes happen the event handler is called on pressing enter, even when button is not focused.
So my question is how can I disable to handle this event when it's caused by enter.
Tnaks you
I had the same problem. Using PreviewMouseLeftButtonUp event fixed my problem. It will not hire when you use enter button
It is probably getting focused before you press Enter.
Anyway, you could try to set the Focusable property to false:
b.Focusable = false;
Also make sure that you don't set the IsDefault property to true.
I'm not sure how this is happening with the button not focussed. Maybe this is logical rather than keyboard focus.
It'd have been nice to have a minimal reproduction.
Two things you can try.
1)
Eat the enter key just in case the keypress is somehow going to the button by using the previewkeydown event on the button. Marking the event as handled will stop the keydown event from firing so no enter will reach the button if it's somehow logically receiving the keypress.
b.PreviewKeyDown += (o, e) =>
{
if (e.Key == Key.Enter) e.Handled = true;
};
Set IsDefault="False" on the buttons in case they're somehow being set as default.

TextBox.Keypress doesn't respond to return key

I have a form with various text boxes. One text box is used for entering a floating point number, so I am using TextBox.KeyPress to process each digit in turn, which only modifies the Text property. The text is processed by a routine that is called when the OK button id pressed (before closing the form). It is also called by the TextBox.Leave event. However, if I change the contents of the text box then press Return to variable isn't updated.
I thought I could overcome this by the following:
private void DestPointNoTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
prvUpdateDestPointNo();
}
}
This is called whenever a key is pressed while the text box is in focus, as can be proved by setting a breakpoint within it. However, it is not called when Return is pressed.
Can someone explain how I can ensure new text is processed when Return is pressed?
If I change the contents of one then click the OK button the new
I would use the Debugger to determine the value of e.KeyCode at runtime.
Why?
When I ran similar code (Winforms TextBox, KeyDown registered), the value of e.KeyCode was
e.KeyCode = LButton | MButton | Back
Another property of KeyEventArgs you can use is KeyValue -
private void DestPointNoTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
prvUpdateDestPointNo();
}
}
If you are using Windows Form Application, then you can set the AcceptButton property of the Form to the OK button and the Click event of the OK button will be fired when you press Enter/Return key

Problem with button when text entered into the textbox

I am trying to make the save button visible when text is entered into the text box by using the following code:
if (tbName.TextModified == true)
{
btnCTimetablesOk.Visible = true;
}
else
{
btnCTimetablesOk.Visible = false;
}
but it gives error at tbname.textmodified
is there any other way to visible the button when we enter the text in text box
this is error i am getting "the event textbox.textmodified can only appear on the left hand side of += or -="
Try using the textbox's Enter and Leave events to show/hide your button:
private void textBox1_Enter(object sender, System.EventArgs e)
{
btnCTimetablesOk.Visible = true;
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
btnCTimetablesOk.Visible = false;
}
Then modify your textbox to use these new methods.
If I'm reading your text correctly, you want the save button to be visible when the textbox has text in it and invisible when the text box is blank. If that's the case, you can use the Leave event (which occurs when the textbox loses focus) and a simple if statement:
private void textBox1_Leave(object sender, System.EventArgs e)
{
if(textBox1.Text != "")
btnCTimetablesOk.Visible = true;
else
btnCTimetablesOk.Visible = false;
}
You can also put this conditional block in any other methods kicked off by events that change the text of the box.
Also, you might want to consider using Enabled instead of Visible, it'll leave the button on the form but will gray out the text and clicking will do nothing.
I'm going to take a stab in the dark here and assume that the button is related to the textbox and you probably want someone to be able to type something in the textbox then click the button. You probably don't want the user to have to type something, then tab out or click somewhere else to make the button visible then click the button.
tbName_TextChanged(object sender, EventArgs e)
{
btnCTimetablesOk.Visible = !String.IsNullOrEmpty(tbName.Text)
}
Btw you're getting that error because TextModified isn't a boolean property, it's an event, like TextChanged or Leave or Enter. You can assign an event handler to it but you can't just check it like that.
As an aside I personally hate systems hungarian for winforms controls. I'd much rather have a timetablesOkButton than a btnCTimeablesOK button. That way if you also have a timetablesNameTextBox you can see at a glance that the button and the textbox match. Of course it may not be up to you.

Pressing Enter on TextBox in Silverlight

I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".
It just feels like there is a better way to do this. So the question is, is there?
I thinks that's the way to catch Key.Enter.
Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.
If you only care about catching Key.Enter for a single control then your approach is correct.
You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").
Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.
This will work if you use want to bind the Command property instead of using the Click event. Start by creating an event handler for Click (see below) and then in the KeyUp do:
private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) SomeButton_Click(this, null);
}
private void SomeButton_Click(object sender, RoutedEventArgs e)
{
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
{
cmd.Execute(null);
}
}
I use the following implementation when using the command pattern:
private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression b = MyTextBox.GetBindingExpression(TextBox.TextProperty);
if (b != null)
b.UpdateSource();
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
cmd.Execute(null);
}
}
When you press Enter, the data source of the textbox is not updated and the command uses an old value. Therefore you have to call UpdateSource before executing the command.
Of course you can catch the event on a higher level than the textbox.
Well, Im preaty new to Silverlight and I created HitEnter beahaviour for button which have one DependencyProperty Button.
And I manulay wire up Button and Behavior (in code behind) and then when enter is hit I inovke the command on the button.

Categories

Resources