press enter key and user control event happen - c#

I have a userControl(DT_Navigator). there is a button (btnNew) in it.
I wrote the click button event for it:
public void btnNew_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
}
I use the user control button in another project and wrote another click event for it:
public void btnNew_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
}
but, when I press the Enter key, just the first event that I defined in user control, executes. However I want both events to execute.
when I click on btnNew, both events trigger, but when I press Enter key, only one event triggers.
how can I fix the problem? Thanks...

Try this:
DT_Navigator.btnNew.Click += new EventHandler(DTnewItem);

Related

Windows form only executs when I press the buttons with the mouse

I m working at an uni project and I encountered a problem.(I m also a beginner with C#).
I created a windows form menu and I have to choose options via keyboard. But my program won't let me choose unless i clicked on the option prior. (I have to click the option then press the key, otherwise I can even spam the key and nothing will happen).
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if( e.KeyCode==Keys.D1 || e.KeyCode==Keys.NumPad1)
{
SidePanel.Height = button1.Height;
SidePanel.Top = button1.Top;
firstCustomControl1.BringToFront();
}
}
Here's a code snippet of how i choose an option based on the number.
Thank you :)
Firstly Set "KeyPreview" Option of your form on which you tend to perform action to true, it is false by default. It needs to be true to perform any function based on any keyboard event.
Secondly add Keydown event on same form.
private void YourForm_KeyDown(object sender, KeyEventArgs e)
{
YourCodeHere();
}
don't use the Keydown event on your Button!
Select the Form in the designer and add Keydown Event for entire Form.
Add Key Down Event on Form 1
then add your Code to the following auto generated Method
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
YourCodeHere();
}

How to know when user doesn't click anymore on a button WPF

I'm doing some password eye text box.
I want, that when the user would press on the eye button, the password would be visible and when he leaves the mouse button it would be unvisible again.
My question: How to know when the user stopped clicking the mouse button?
Image of how it looks like:
My code is roughly as follows:
private void eye_click(object sender , RoutedEventArgs e)
{
//here I making the password visible
}
I'm looking for something like:
private void user_doesnt_click_anymore_click(object sender , RoutedEventArgs e)
{
//return the password to be unvisible
}
You can react to different events, depending on your
Form-Example
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button1_MouseClick);
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("button1_Click");
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
Console.WriteLine("button1_MouseClick");
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("button1_MouseDown");
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
Console.WriteLine("button1_MouseUp");
}
MouseDown
Fires if you mouse is over the button an a mousebutton is pushed down.
First chance to see something
Here you want to show the pw.
MouseUp
Fires if you release the mousebutton which you "downed" on the button.
Fired when stopped clicking. You're not neccessarily on the button anymore!
Here you want to hide the pw.
MouseClick
Fires if a MouseDown AND MouseUp is performed and you're still hovering the button.
This prevents hitting buttons with the keyboard. Perhaps helpful in some situations?!
Click
Fires if the button is clicked (Pushed down and released). This could also be done my selecting the button by tabbing to it and pushing space.
This is what you normally want to use, when a button is clicked, like "submit form"-actions.
Well I guess you could go for something in the area of MouseDown/MouseUp
<Button x:Name="Btn" MouseDown="MouseDown" MouseUp="MouseUp" />

C# If Button IsEnabled, its triggering event on click

I have next issue, I am working on small c#-wpf application,
and on load I am disabling button which is ussualy used to do some action on click, and it looks like this:
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
if (String.IsNullOrEmpty(password.Password))
{
btnActivate.IsEnabled=false;
}
}
and somewhere I am checking my password field, for example:
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
if (password.Password == "drag0n")
{
btnActivate.IsEnabled = true;
}
else
{
btnActivate.IsEnabled = false;
}
}
}
And my problem is next, when user enter "drag0n" and press enter, button should be just enabled, but its not only enabled, its calling automatic his event _Click, I don't know why does that happening, because in that case, if my button is just enabled event _Click is also called, and if user clicks on that button, event is called again, so actually my event onclick is called twice.
My question is how can I stop calling my Click event if I set IsEnabled=true. When I set IsEnabled=true I just want it to be enabled for pressing and I don't want execute event _Click.
I want to execute event _Click only when my button is pressed as it should work and not on IsEnabled=true.
Thanks guys,
Cheers
On click event occurs when you press Enter key, because this button is the default control in a form.
If you don't want click event on Enter key, you should either make this button not default or not process Enter key pressing in your button click (e.Handled = true -> when Enter is pressed).
Or try to change your code:
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
if (password.Password == "drag0n")
{
e.Handled = true; // add this line
btnActivate.IsEnabled = true;
}
else
{
btnActivate.IsEnabled = false;
}
}
}

How to fire click event of panel programmatically?

I have a winforms app in vs2010 and a panel whose click event I would like to fire programatically. How do I do this? Button has a PerformClick but I cannot find the same in Panel.
Your panel's Click event is going to be attached to an event handler, right?
Then just call that event handler from the button's click event handler:
public void Panel1_Click(object sender, EventArgs e)
{
//Do whatever you need to do
}
public void Button1_Click(object sender, EventArgs e)
{
//Do anything you need to do first
Panel1_Click(Panel1, EventArgs.Empty);
}
The effect will be the same as clicking on the panel.

Why it is not Possible to call textBox1_KeyPress() event from some Button Click event.?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
MessageBox.Show("called btn 0 click..");
KeyPressEventArgs e0 = new KeyPressEventArgs('0');
textBox1_KeyPress(sender, e0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("called txtbox_keypress event...");
}
}
Am sorry if this is a silly question,I have just started to learn windows forms, I still find material on the internet confusing.I want to implement calculator. So when number button is pressed it should be filled in textbox. So I thought calling textBox1_keypress() event from button click event would work??? but its not working,
I can manually write the logic in button click event to fill text in text box but if i do so, i have to do the same thing in button1_KeyPress event too. so it would be duplication of code right??...so i thought solution was to call textBox1_KeyPress() event from both button click event and button key press event...but its not working .So what should i do???..is there any other approach which should i follow.
so it would be duplication of code right??
Yes, it would be. So you can do
private void btn0_Click(object sender, EventArgs e)
{
CommonMethod(e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
CommonMethod(e);
}
private void CommonMethod(EventArgs e)
{
//Your logic here.
}
The TextBox KeyPress event handler (textBox1_KeyPress) is called after the user presses a key. The KeyPressEventArgs parameter includes information such as what key was pressed. So calling it from your btn0_Click method isn't going to set the text for the TextBox.
Rather, you want to (probably) append whatever number the user pressed to the text already present in the TextBox. Something like
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
might be closer to what you're trying to accomplish.
You could put the logic in an extra function like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
NumberLogic(0),
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// I don't know right now if e contains the key pressed. If not replace by the correct argument
NumberLogic(Convert.ToInt32(e));
}
void NumberLogic(int numberPressed){
MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
}
}
You don't want to tie the events together like that.
A key-press is one thing, handled in one way.
A button click is something totally different and should be handled as such.
The basic reason is this,
The button doesn't know what number it is, you need to tell it that.
A key-press on the other hand, knows what number was pressed.
If you REALLY want to, for some reason, you could use SendKeys to trigger your key-press event in a round-about way, from the button.
SendKeys.SendWait("0");
I can suggest to you to use an Tag Property of the Buttons. Put in it the value of each button in Design mode or in Constructor, create one button event handler for all buttons and use Tag value:
Constructor:
button1.Tag = 1;
button2.Tag = 2;
button1.Click += buttons_Click;
button2.Click += buttons_Click;
Event hadler:
private void buttons_Click(object sender, EventArgs e)
{
textBox1.Text = ((Button)sender).Tag.ToString();
}

Categories