7 Replies - 1352 Views - Last Post: 11 December 2012 - 10:43 AM Rate Topic: -----

#1 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Get Focus When Typing in C#

Posted 10 December 2012 - 10:33 AM

I want to activate a search textbox when the user starts to type something (even if the textbox isnt focused right then). I have come as far as setting KeyPreview on the form to true. Then in the KeyDown event handler, I have this:

if(!searchTextBox.Focused)
{
searchTextBox.Focus();
}

This almost works. The textbox is focused, but the first typed letter is lost. I guess this is because the textbox never really gets the event, since it wasn't focused when it happend. So, do anyone have a clever solution to how I could make this work like it should?

This post has been edited by tlhIn`toq: 10 December 2012 - 05:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Get Focus When Typing in C#

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Get Focus When Typing in C#

Posted 10 December 2012 - 11:30 AM

Have a look at the tab index of the controls.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: Get Focus When Typing in C#

Posted 10 December 2012 - 11:44 AM

You may need to have this focusing take place earlier: Line when the form is shown, so it takes place before someone starts typing.
Was This Post Helpful? 0
  • +
  • -

#4 TheBlueTorch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-November 12

Re: Get Focus When Typing in C#

Posted 10 December 2012 - 05:11 PM

One of the arguments in the Keypreview method captures the pressed key (I think is e -sorry, I'm new to C# programming and sometimes I forget those kind of things-). So, you can do what you are doing now but you need to add something like this:

if(!searchTextBox.Focused)
{
   searchTextBox.Text = e.Keycode;
   searchTextBox.Focus();
}



View PostTheBlueTorch, on 10 December 2012 - 05:08 PM, said:

One of the arguments in the Keypreview method captures the pressed key (I think is e -sorry, I'm new to C# programming and sometimes I forget those kind of things-). So, you can do what you are doing now but you need to add something like this:

if(!searchTextBox.Focused)
{
[b]searchTextBox.Text = e.Keycode;[/b]
searchTextBox.Focus();
}



The method I was referencing is Keydown.

This post has been edited by tlhIn`toq: 10 December 2012 - 05:10 PM
Reason for edit:: You can't bold inside of code tags

Was This Post Helpful? 0
  • +
  • -

#5 TheBlueTorch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-November 12

Re: Get Focus When Typing in C#

Posted 10 December 2012 - 05:29 PM

Or maybe you can do somthing like:

if(!searchTextBox.Focused)
{
   searchTextBox_keydown(this, e);
   searchTextBox.Focus();
}


Was This Post Helpful? 0
  • +
  • -

#6 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Re: Get Focus When Typing in C#

Posted 10 December 2012 - 10:41 PM

View PostTheBlueTorch, on 10 December 2012 - 05:11 PM, said:

One of the arguments in the Keypreview method captures the pressed key (I think is e -sorry, I'm new to C# programming and sometimes I forget those kind of things-). So, you can do what you are doing now but you need to add something like this:

if(!searchTextBox.Focused)
{
   searchTextBox.Text = e.Keycode;
   searchTextBox.Focus();
}



View PostTheBlueTorch, on 10 December 2012 - 05:08 PM, said:

One of the arguments in the Keypreview method captures the pressed key (I think is e -sorry, I'm new to C# programming and sometimes I forget those kind of things-). So, you can do what you are doing now but you need to add something like this:

if(!searchTextBox.Focused)
{
[b]searchTextBox.Text = e.Keycode;[/b]
searchTextBox.Focus();
}



The method I was referencing is Keydown.



its not working..
giving an error at e.keycode
Was This Post Helpful? 0
  • +
  • -

#7 TheBlueTorch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-November 12

Re: Get Focus When Typing in C#

Posted 10 December 2012 - 11:56 PM

Use this:

(char)e.Keycode


Note: Keycode returns the code of the key that is being pressed.
Was This Post Helpful? 0
  • +
  • -

#8 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Re: Get Focus When Typing in C#

Posted 11 December 2012 - 10:43 AM

View PostTheBlueTorch, on 10 December 2012 - 11:56 PM, said:

Use this:

(char)e.Keycode


Note: Keycode returns the code of the key that is being pressed.



perfectly work...thnxxx guys...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1