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. how to know which control is focused

how to know which control is focused

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 5 Posters 0 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.
  • M Offline
    M Offline
    meghamaharshi
    wrote on last edited by
    #1

    hi, i have many text boxes on my web page, how can i get which text box is focused. Thank You

    OriginalGriffO P B H 4 Replies Last reply
    0
    • M meghamaharshi

      hi, i have many text boxes on my web page, how can i get which text box is focused. Thank You

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      foreach (Control c in Controls)
      {
      if (c.Focused)
      {
      // Found it!
      MessageBox.Show(c.Text);
      }
      }

      Remember to check if it is a TextBox, as buttons etc can also have the focus.

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • M meghamaharshi

        hi, i have many text boxes on my web page, how can i get which text box is focused. Thank You

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        Something like this should work: foreach (Control ctrl in Page.Controls) { if (ctrl is TextBox && ctrl.Focused) { ((TextBox)(ctrl)).Text = "FOUND YOU!!"; } }
        Kind regards,

        The only programmers that are better C# programmers, are those who look like this -> :bob:

        :java: Programm3r My Blog: ^_^

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          foreach (Control c in Controls)
          {
          if (c.Focused)
          {
          // Found it!
          MessageBox.Show(c.Text);
          }
          }

          Remember to check if it is a TextBox, as buttons etc can also have the focus.

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

          M Offline
          M Offline
          meghamaharshi
          wrote on last edited by
          #4

          i am receiving error- Cannot convert method group "Focus" to non delegate type 'bool'

          P 1 Reply Last reply
          0
          • M meghamaharshi

            i am receiving error- Cannot convert method group "Focus" to non delegate type 'bool'

            P Offline
            P Offline
            Programm3r
            wrote on last edited by
            #5

            Have you had a look at the TexBox Class[^]? Maybe you can find something there... Kind regards,

            The only programmers that are better C# programmers, are those who look like this -> :bob:

            :java: Programm3r My Blog: ^_^

            1 Reply Last reply
            0
            • M meghamaharshi

              hi, i have many text boxes on my web page, how can i get which text box is focused. Thank You

              B Offline
              B Offline
              benjymous
              wrote on last edited by
              #6

              Controls have an event that's triggered when they get focus, so you could just hook into this on every control, and store the last focused item yourself

              Help me! I'm turning into a grapefruit! Buzzwords!

              1 Reply Last reply
              0
              • M meghamaharshi

                hi, i have many text boxes on my web page, how can i get which text box is focused. Thank You

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                You would probably get better responses on the Web Development Forum, but for what it's worth in Windows Forms there is the ActiveControl property. Is that also available For ASP?

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                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