.NET - Which Control has Focus? - Visual Studio 2008
I am currently translating a windows开发者_开发技巧 form application, written in VB6, to a webpage written in Visual Studio 2010.
In VB6 it is easy to find out which control has focus on the form using '.getFocus()'. I have come to the conclusion it is not that easy, if at all possible, in vb2008.
Is this possible to do?
In WinForms you can create a foreach loop like this and check if a specific control has focus:
foreach (Control item in this.Controls)
{
if (item.Focused)
//codes to handle the condition
}
Or like spacemonkeyes said, use this.ActiveControl
or VB's version Me.ActiveControl
However for a webpage I think you have to use JavaScript. The document object has a property named ActiveElement which as far as I know, only IE supports this property:
document.ActiveElement
It returns a reference to the focused control.
To use it on the server side, you have to have a form which is submitted to the server or you could use Ajax to submit that particular control. Pretty complex codings are required.
Me.ActiveControl
will give you a control object reference. This returns windows form control, but you should be able to use Me.ActiveControl.Name
or one of the other parameters to get what you require, returns nothing if no control has focus
But as pointed out, this only works in a windows form, web applications you will need to do some javascript skullduggery to get what you want.
精彩评论