How to detect enter being pressed in a textbox? - winforms

How do i detect when enter is pressed in a textbox in visual studio 2013 forms application?? I've tried using textbox1_OnKeyPress, KeyUp, KeyDown, KeyPress, everything I could find in google searches, and none of it worked... is it a check I have to make in textChanged??

Add a textbox in your form and handle the KeyDown event of that textbox.
public class Form1
{
public Form1()
{
InitializeComponent();
textBox1.KeyDown += new KeyEventHandler(this.KeyDownEvent);
}
private void KeyDownEvent(object sender, KeyEventArgs e)
{
if (e.KeyCOde == Keys.Return)
{
//Place your code here
}
}
}

Related

OnAddedToForm designer event for UserControl?

Is there an event for a UserControl that only fires the first time it is added to a form? I basically want to show a memmo on how to use the control to the user once they add the control into the form, but never show it afterwards (until they add another instance of this control).
An easy way to do that could be:
Use the event ParentChanged from Control : Control.ParentChanged Event
UserControl is an heritance of Control
In your UserControl, you could use a private field to define if the item has been already added to a parent.
This code could work for you:
public class CustomUserControl : UserControl
{
private bool _firstTimeAdded = false;
private void Init()
{
this.ParentChanged += CustomUserControl_ParentChanged;
}
private void CustomUserControl_ParentChanged(object sender, EventArgs e)
{
if (Parent is Form && !_firstTimeAdded)
{
_firstTimeAdded = true;
// Show your memmo
}
}
}

Windows Forms method Form.ShowDialog() strange behaviour

I was trying to show a form(FormChild), with some radio buttons in, just to select, close, and get the value of the selected radio button from the calling form(FormParent). On a click event handler for a Button in FormParent, I just did:
var formChild=newFormChild();
formChild.ShowDialog(this);
All was working great until I decided to handle the CheckedChanged event of one of the RadioButtons inside FormChild:
private void SomeRadioButton_CheckedChanged(object sender, EventArgs e)
{
Close();
}
Now the formChild.ShowDialog(this); did not showed formChild and formChild immediately returns DialogResult.Cancel.
Any explanation on this?
Thanks in advance
The lowest Tab Index radiobutton will be checked by default, If this event handler is assigned to that button it will cause the situation that you are describing.
You can either change your Tab Order or create a Boolean Flag that is set in your Forms Shown EventHandler to keep it from Triggering until you check it again.
public partial class Form1 : Form
{
bool initDone;
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (initDone)
{
if (((RadioButton)sender).Checked == true)
{
Close();
}
}
}
private void Form1_Shown(object sender, EventArgs e)
{
initDone = true;
}
}
Is there anywhere if your code that sets the value of the RadioButton? If you programmatically set the "Checked" property of the RadioButton, it will fire the event.
In your situation, the event handler contains your Form.Close() so the form never gets the chance to be visible to the user.
Note: Setting the RadioButton.Checked as "true" in the designer will not fire the event.

how to change the layout of a control inside a parent wpf form from a child winform?

i have two separate projects inside a solution, one is a wpf the other is windows form and i have referenced the winform to the wpf project.. Inside the wpf window is an image control, when clicked, a windows form with a button would appear.
How i can be able to change the image source of the image control inside the wpf form when the button inside the winform is clicked...
i have seen a similar question to this but i cant understand the answers...
You could pass a delegate/Action into the Winform to perform the action
Here is a very quick example
WPF
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// pass in the method you want to call when the winform button is clicked
var winform = new Form1(() => ChangeImage()).ShowDialog();
}
private void ChangeImage()
{
// your change image logic
}
}
Winforms
public partial class Form1 : Form
{
private Action _action;
public Form1()
{
InitializeComponent();
}
public Form1(Action action)
{
_action = action;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_action != null)
{
// call the method in the WPF form
_action.Invoke();
}
}
}

Parent window handles the Keyup event from the child window in WPF

A parent window is designed having keyup event attached.
The MainWindow goes like:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e != null && e.Key == Key.Return)
{
MessageWindow msgWindow = new MessageWindow("KEY UP");
msgWindow.Show();
}
}
}
The MessageWindow having a button OK to close the window, goes like:
public partial class MessageWindow : Window
{
public MessageWindow()
{
InitializeComponent();
}
public MessageWindow(string message) : this()
{
txtMess.Text = message;
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
On receiving the input key as [Return] on parent, a new child window is initialized and is displayed.
Press TAB to focus OK button and press Enter.
The child MessageWindow pops up again.
Reason: Parent window receives KeyUp event when RETURN key is pressed on child window to close it.
Please provide a way to stop the handling of KeyUp event by parent, other than using a FLAG.
Try to use PreviewKeyDown event instead of KeyUp one

How to set focus on textbox when showing a child window

Upon starting up my silverlight app a child window appears to prompt the user to login. Within in the window I have a username textbox that I want to be focused so that the user can begin typing without focusing it with the mouse first.
This seems like it should work:
public partial class LoginForm : ChildWindow
{
public LoginForm()
{
InitializeComponent();
}
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
txtUsername.Focus();
}
}
I noticed that the Load event happens before the window is rendered which might be the problem, however I don't see an event handler for Rendered or similar.
Edit: Forgot to mention this application is running in the browser.
You need set TabIndex on txtUsername with lower value than other controls on that child window.
Try this:
public partial class LoginForm : ChildWindow
{
public LoginForm()
{
InitializeComponent();
Loaded+=LoginFormLoaded;
}
private void LoginFormLoaded(object sender, RoutedEventArgs e)
{
txtUsername.Focus();
Loaded-=LoginFormLoaded;
}
}
update:
You can also add your event on your TextBox. the textbox has a Loaded event too. If you use this event, you are sure that the TextBox is loaded.
If you are running the application in the browser make sure that the silverlight plugin has focus before calling the focus method of the control.
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.Plugin.Focus();
txtUsername.Focus();
}
This works on most browsers except I couldn't get it to work on Safari.
Here is something I use:
public partial class LoginForm : ChildWindow
{
public LoginForm()
{
InitializeComponent();
Loaded += (s, e) =>
{
txtUsername.Focus();
};
}
}
And in XAML set your TabIndex to 1
For some reason that I do not have time to figure out, the Child Window in the Silverlight SDK opens, animates, and the focus moves to some unknown place. Most of the solutions suggested on this thread do not work for me because the control receives focus briefly (you can see the focus hit the text box) and then the focus moves to somewhere else (I am guessing that the storyboard on the ChildWindow template has something to do with that). So, I figured out this work around that is actual code that has been implemented and has been proven to work:
public NewChildWindow()
{
InitializeComponent();
this.GotFocus += NewChildWindow_GotFocus;
}
private void NewChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
this.GotFocus -= NewChildWindow_GotFocus;
txtBoxToFocusOn.Focus();
}
Make your textbox TabIndex="0" and in its Loaded event do the "YourtxtBox.Focus()"

Resources