1

I am implementing a search function in a windows form in c#. I have set KeyPreviewto true on the form and have added an event handler for KeyDown so I can catch things like ctrl+f, esc and enter.

I am catching these keys just fine and I'm able to make my text box appear, but I am unable to type into the box. All of the keys are going to PortsTraceForm_KeyDown(...) but they never make it to the text box. According to the msdn page about KeyPreview, setting e.Handled to false should cause the event to pass to the view in focus (the text box), but this isn't happening. I have not registered a KeyDown event for the text box, so it should be using the default behavior. Have I missed something?

KeyDown event:

    private void PortsTraceForm_KeyDown(object sender, KeyEventArgs e)
    {
        e.SuppressKeyPress = true;
        e.Handled = false;

        if (e.KeyData == (Keys.F | Keys.Control)) // ctrl+f
        {
            e.Handled = true;
            ShowSearchBar();
        }
        else if (e.KeyCode == Keys.Escape) // esc
        {
            e.Handled = true;
            HideSearchBar();
        }
        else if (e.KeyCode == Keys.Enter) // enter
        {
            if (searchPanel.Visible)
            {
                e.Handled = true;
                if (searchShouldClear)
                    SearchStart();
                else
                    SearchNext();
            }
        }
    }

show search bar:

    private void ShowSearchBar()
    {
            FindBox.Visible = true;
            FindBox.Focus(); // focus on text box   
    }

hide search bar:

    private void HideSearchBar()
    {
            this.Focus(); // focus on form
            FindBox.Visible = false;
    }
H H
  • 263,252
  • 30
  • 330
  • 514
mtmurdock
  • 12,756
  • 21
  • 65
  • 108
  • first I would subscribe on Lost ficus event of TextBox, and check if after FindBox.Focus(); call somehow TextBox looses the focus by the way.. – Tigran Sep 12 '11 at 18:11
  • That is a good suggestion, except the cursor is still visible and flashing, so I'm fairly positive it isn't losing focus. – mtmurdock Sep 12 '11 at 18:15

2 Answers2

1

Your TextBox likely does not have focus even though you are calling Focus(). From the documentation:

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

You can check the return value of Focus() for success, but I have had little luck in the past using that method to set focus to an arbitrary control. Instead, try using the method that the documentation suggests, i.e., call Select().

EDIT:

Nevermind (though it's still valid advice), I think I see your problem:

e.SuppressKeyPress = true

Why are you doing this? Again, from the docs:

[SuppressKeyPress] Gets or sets a value indicating whether the key event should be passed on to the underlying control

So you are intentionally preventing the TextBox from getting key events. If you want to pass the event through you shouldn't be setting that property to false.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • I appreciate the answer but its unrelated to my problem. Even when I click on the text field and begin typing it doesn't work. The events still aren't making it to the text field. – mtmurdock Sep 12 '11 at 18:19
  • Oh geez... I was trying to supress the 'ding' noise and must have copied the wrong line of code. Do you happen to know how to supress the ding? – mtmurdock Sep 12 '11 at 18:27
  • @mtmurdock: You mean the sounds that is emitted when the user presses enter or escape? That's actually a low level OS thing, but what you have should do it. If you set e.Handled to true when the Enter key is pressed it should stop the sound as the TextBox shouldn't process the key press. I'm guessing though and may be wrong. If I am, just set SuppressKeyPress to true inside of the code that handles the Enter and Escape keys, or create your own TextBox class that overrides OnKeyPress and doesn't call the base implementation for those keys. – Ed S. Sep 12 '11 at 18:42
0

try this example , of overrides method.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        // your code here

        // this is message example
        MessageBox.Show(keyData.ToString());
        return base.ProcessCmdKey(ref msg, keyData);
    }

Regards.