0

I have a scientific calculator application in C#. I'd like to disable click highlight, you know, I click the button with mouse, and the button will be highlighted. It's very annoying, because I can't use the enter key event, when the button is highlighted, and in this case not working the execute function with enter. The calculator use the highlighted button instead of the execute function.

I hope my problem is understandable. What can I do?

Kovoliver
  • 238
  • 2
  • 13
  • 1
    Possible duplicate of [Disable mouse capturing by Button](https://stackoverflow.com/questions/35363778/disable-mouse-capturing-by-button) – Muhammad Saqlain Aug 12 '18 at 14:29
  • 1
    I am not sure what you are looking for, do you want to keep the focus on a different object, like textbox? In that case you can set focus back to textbox on click. – Aldert Aug 12 '18 at 14:47
  • You still should be able to receive keyevent even if your button is highlighted, the form still reveices keyevents. – Aldert Aug 12 '18 at 14:48
  • I have buttons. If I click the button, the button will be highlighted. After that I press the enter key, not the enter key event executing, but also the button click event what I pressed. Here's a picture about the problem below: https://ibb.co/i5x7d9 – Kovoliver Aug 12 '18 at 15:04
  • So after the button_click event you should set the focus back to the textbox: textbox.focus() – Aldert Aug 12 '18 at 16:24
  • @Aldert That doesn't make the button _completely_ unselectable. Kovoliver, Your solution is in the duplicate question. – 41686d6564 stands w. Palestine Aug 12 '18 at 17:08
  • If you ask me, Kovoliver is not looking to make the button completely unselectable. He is annoyed that after he clicks the button, he cannot type anymore. When you look at his screenshot it seems to me he wants the user to be able to click buttons and to enter by keyboard. – Aldert Aug 12 '18 at 17:12
  • @Aldert That's exactly what I meant by "completely unselectable". For example, click on the button and don't release, move the cursor away from it, then release, you'll see that the button _is_ selected (highlighted). Also, the button can still be selected using the Tab key in the keyboard (unless you set the `TabStop` property to false). The `ControlStyles.Selectable` approach, on the other hand, doesn't have the same problems. – 41686d6564 stands w. Palestine Aug 12 '18 at 17:20
  • Use [Form.ProcessCmdKey](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.processcmdkey%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). You can then intercept an `Enter` key, doesn't matter where the focus is. You'll have to handle it anyway if you want to use the keyboard to process numbers/symbols. – Jimi Aug 12 '18 at 18:03
  • The most times `buttons` with custom looks are not actually buttons but `labels`. You can get the mouse *enter, leave, down, click* events to make them look like buttons. – γηράσκω δ' αεί πολλά διδασκόμε Aug 13 '18 at 09:47

1 Answers1

0

In your form, try setting KeyPreview = true and override OnKeyDown:

public ExampleForm()
{
    InitializeComponent();

    KeyPreview = true;
}

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);

    if (e.KeyData == Keys.Enter)
    {
        e.Handled = true;

        // Execute calculator function
    }
}
Michael Csikos
  • 688
  • 9
  • 11