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
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. How to know the focused control

How to know the focused control

Scheduled Pinned Locked Moved .NET (Core and Framework)
helptutorial
2 Posts 2 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.
  • S Offline
    S Offline
    Sreepathi
    wrote on last edited by
    #1

    Hi, I used the following code to know the control which has focus: System.Windows.Forms.AccessibleObject.GetFocused The error I got is 'GetFocused' is not a member of 'AccessibleObject', but Documentation says, it is a member. Do you any one know how to find the focused control. Thanks, Sreepathi

    R 1 Reply Last reply
    0
    • S Sreepathi

      Hi, I used the following code to know the control which has focus: System.Windows.Forms.AccessibleObject.GetFocused The error I got is 'GetFocused' is not a member of 'AccessibleObject', but Documentation says, it is a member. Do you any one know how to find the focused control. Thanks, Sreepathi

      R Offline
      R Offline
      Roman Rodov
      wrote on last edited by
      #2

      This will return a currently focused control on a form IF the form does not have any container controls on it. public Control GetFocused() { foreach(Control ctrl in this.Controls) { if (ctrl.Focused) { return ctrl; } } return null; } This is a universal way of getting the currently focused control: public class MyForm : Form { [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)] internal static extern IntPtr GetFocus(); private Control GetFocusedControl() { Control focusedControl = null; // To get hold of the focused control: IntPtr focusedHandle = GetFocus(); if(focusedHandle != IntPtr.Zero) // Note that if the focused Control is not a .Net control, then this will return null. focusedControl = Control.FromHandle(focusedHandle); return focusedControl; } }

      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