0
votes

I have a small projet in which I want several buttons to behave differently when Clicked or Ctrl+Clicked. To achieve that, each of those buttons has this kind of function attached to their Click() event :

private void Button1_Click(object sender, EventArgs e)
    {
        int value = 10 //default value
        bool boool = false; 
        if (ModifierKeys == Keys.Control)
        {
            using (var form = new NUP_Popup(0, value, value, "maximum ?")) 
            { //creates a simple window that allows user to change the value of 'value'
                form.ShowDialog();
                if (form.DialogResult == DialogResult.OK)
                {
                    value = form.retVal;
                    boool = true;
                }
                else return;
            }
        }
        //do stuff here, either with default or user value
    }

Now, Clicking or Ctrl+Clicking behaves as intended with this function. My problem is that this behaviour doesn't apply when my buttons are activated using the Enter key : Enter key alone triggers the "normal" behaviour but Ctrl+Enter does nothing (button is not activated).

I already have overriden the ProcessDialogKey() function to close the window when Escape is pressed, so I thought I could use it to make Enter key presses trigger the Click() event function :

protected override bool ProcessDialogKey(Keys keyData)  //Allows quit when Esc is pressed
    {
        if (Form.ModifierKeys == Keys.None && keyData == Keys.Escape)
        {
            this.Close();
            return true;
        }
        if (keyData == Keys.Return)
        {
            this.OnClick(new EventArgs());
        }
        return base.ProcessDialogKey(keyData);
    }

And that's where I'm stuck. Of course this doesn't do anything but I don't really have an idea of what to type inside my second condition to make it work.

Maybe I'm using the wrong approach ? Can someone point me in the right direction to do it ?

3
What do you mean by your second condition? And I'd use something like the KeyDown event of your form to do this. - MetaColon

3 Answers

0
votes

Assuming you placed a Label with ID Label1 and a button with ID Button1, following will do:

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = "Button Clicked";
        if (Control.ModifierKeys == Keys.Control) label1.Text += " with Ctrl";
    }

    private void button1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '\n') button1_Click(sender, new EventArgs());
    }

to your solution, simply add a KeyPress event to your Button1 and apply following code inside the keypress event Button1_KeyPress:

if (e.KeyChar == '\n') Button1_Click(sender, new EventArgs());
0
votes

I found a solution that enables you to either click Enter or Ctrl+Enter:

private void txtIP_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\n' || e.KeyChar == '\r') btnStart_Click(sender, new EventArgs());
        }
-1
votes

Okay, so I finally found a working solution by adding this to my overriden ProcessDialogKey() method :

if (keyData == (Keys.Enter | Keys.Control))
        {
            (this.ActiveControl as Button).PerformClick();
        }

I don't know if it qualifies as "clean" code, but it has the merit of fulfilling my 2 requirements : making Ctrl+Enter function as Ctrl+Click without having to declare 2 methods per Button.