Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# VS 2008 3.5 Winform SetFocus to textbox on Ctrl + L

C# VS 2008 3.5 Winform SetFocus to textbox on Ctrl + L

Scheduled Pinned Locked Moved C#
csharpvisual-studiotutorialquestion
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    Good morning. I am trying to figure out how to set the focus to a textbox when a user clicks on ctrl + L. I have the following:

    private void button1_Click(object sender, EventArgs e)
    {
    this.tstbAddress.Focus();
    }

    When I tried this in and of the key methods, it didn't work. I have a webbrowser control on the form. I thought it might have something to do with (lack of) focus. Any suggestions? Thank you, WHEELS

    T 1 Reply Last reply
    0
    • W Wheels012

      Good morning. I am trying to figure out how to set the focus to a textbox when a user clicks on ctrl + L. I have the following:

      private void button1_Click(object sender, EventArgs e)
      {
      this.tstbAddress.Focus();
      }

      When I tried this in and of the key methods, it didn't work. I have a webbrowser control on the form. I thought it might have something to do with (lack of) focus. Any suggestions? Thank you, WHEELS

      T Offline
      T Offline
      TheFoZ
      wrote on last edited by
      #2

      Hi Wheels In the forms KeyDown event you will have access to more in the KeyEventArgs so you can identify if the control key and L key are being pressed at the same time. You will need to set the KeyPreview property to true on the form then the following will work

          private void Form1\_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.Control && e.KeyValue == 76)
                  textBox1.Focus();
          }
      

      The FoZ

      W L 2 Replies Last reply
      0
      • T TheFoZ

        Hi Wheels In the forms KeyDown event you will have access to more in the KeyEventArgs so you can identify if the control key and L key are being pressed at the same time. You will need to set the KeyPreview property to true on the form then the following will work

            private void Form1\_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Control && e.KeyValue == 76)
                    textBox1.Focus();
            }
        

        The FoZ

        W Offline
        W Offline
        Wheels012
        wrote on last edited by
        #3

        I put the following code in the Form1_Load:

        this.KeyPreview = true;
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

        and I have the following event:

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
        if (e.Control && e.KeyValue == 76)
        this.tstbAddress.Focus();
        }

        Unfortunately this only works when the form has focus. I'm not sure how to give the form focus. I used a button to give the form focus and the Ctrl+L worked. Any suggestions? WHEELS

        T 1 Reply Last reply
        0
        • W Wheels012

          I put the following code in the Form1_Load:

          this.KeyPreview = true;
          this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

          and I have the following event:

          private void Form1_KeyDown(object sender, KeyEventArgs e)
          {
          if (e.Control && e.KeyValue == 76)
          this.tstbAddress.Focus();
          }

          Unfortunately this only works when the form has focus. I'm not sure how to give the form focus. I used a button to give the form focus and the Ctrl+L worked. Any suggestions? WHEELS

          T Offline
          T Offline
          TheFoZ
          wrote on last edited by
          #4

          I would guess that you could put the KeyDown event code on the form that has the button. Is this an MDI application?

          The FoZ

          W 1 Reply Last reply
          0
          • T TheFoZ

            I would guess that you could put the KeyDown event code on the form that has the button. Is this an MDI application?

            The FoZ

            W Offline
            W Offline
            Wheels012
            wrote on last edited by
            #5

            I would guess that you could put the KeyDown event code on the form that has the button. Is this an MDI application? The FoZ This is not a MDI app. Just a Winform with a web browser control. Someone types in a URL it the address textbox and it navigates to the webpage. The KeyDown code is in Form1_KeyDown. The button I created is temporary and I used it just to set the focus to the form to test the Ctrl + L. WHEELS

            T 1 Reply Last reply
            0
            • T TheFoZ

              Hi Wheels In the forms KeyDown event you will have access to more in the KeyEventArgs so you can identify if the control key and L key are being pressed at the same time. You will need to set the KeyPreview property to true on the form then the following will work

                  private void Form1\_KeyDown(object sender, KeyEventArgs e)
                  {
                      if (e.Control && e.KeyValue == 76)
                          textBox1.Focus();
                  }
              

              The FoZ

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              TheFoZ wrote:

              e.KeyValue == 76

              what is this nonsense? you should NEVER have magic constants in the middle of code. How about e.KeyCode==Keys.L? :|

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Merry Christmas and a Happy New Year to all.


              T 1 Reply Last reply
              0
              • L Luc Pattyn

                TheFoZ wrote:

                e.KeyValue == 76

                what is this nonsense? you should NEVER have magic constants in the middle of code. How about e.KeyCode==Keys.L? :|

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Merry Christmas and a Happy New Year to all.


                T Offline
                T Offline
                TheFoZ
                wrote on last edited by
                #7

                Luc Pattyn wrote:

                what is this nonsense? you should NEVER have magic constants in the middle of code. How about e.KeyCode==Keys.L?

                True. Still being quite new C# I did not know about the Keys.L. Thanks for the info

                The FoZ

                1 Reply Last reply
                0
                • W Wheels012

                  I would guess that you could put the KeyDown event code on the form that has the button. Is this an MDI application? The FoZ This is not a MDI app. Just a Winform with a web browser control. Someone types in a URL it the address textbox and it navigates to the webpage. The KeyDown code is in Form1_KeyDown. The button I created is temporary and I used it just to set the focus to the form to test the Ctrl + L. WHEELS

                  T Offline
                  T Offline
                  TheFoZ
                  wrote on last edited by
                  #8

                  Hi Wheels If the form does not have focus am I right to assume that the application does not have focus? If this is the case then you will not need the button to make your form have focus, just click on the form :-D

                  The FoZ

                  W 1 Reply Last reply
                  0
                  • T TheFoZ

                    Hi Wheels If the form does not have focus am I right to assume that the application does not have focus? If this is the case then you will not need the button to make your form have focus, just click on the form :-D

                    The FoZ

                    W Offline
                    W Offline
                    Wheels012
                    wrote on last edited by
                    #9

                    Good morning The FoZ. I believe the web browser may have the focus, but I added a toolbar menu item which triggers on Ctrl + L and made it hidden. Seems to work well. Thank you for the assistance, WHEELS

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups