Solved

VB .NET 2010 find out which control has focus

Posted on 2010-08-15
10
1,840 Views
Last Modified: 2013-11-26
Vb .NET 2010

I have a inputpanel and i'd like to find out which control has current focus. Is there a way to do this ?
0
Comment
Question by:siacom
10 Comments
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33443292
can u please tell what you have to do ?
0
 

Author Comment

by:siacom
ID: 33443382
1) Toggle a save button between enabled and disabled as soon as a object within the inputpanel is changed.

Cuurently I am using this to do so:

    Private Sub inputComponent_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) _
             Handles inputTxtCompany.BoundValueChanged, txtContactName.BoundValueChanged, _
                     InputComboBox1.SelectedIndexChanged, InputComboBox2.SelectedIndexChanged, _
                     InputComboBox3.SelectedIndexChanged, InputComboBox4.SelectedIndexChanged, _
                     InputTxtClientNo.BoundValueChanged, lblContactName.BoundValueChanged, _
                     chkBothIndustries.CheckedChanged, InputTxtIndustry.BoundValueChanged, _
                     InputComboBox5.SelectedIndexChanged

        MainForm.cmdSave.Enabled = True
....

2) Only a few controls need further processing depenging on the change, like combo boxes and chekboxes for example.

So I tried this:

        Dim ctrl As Control = ActiveControl

        Debug.Print(ctrl.Name)


on load and because the form is a mdichild of the mainform where the ribbon buttons are
an error occur on the main form

An error occurred creating the form. See Exception.InnerException for details.  The error is: Object reference not set to an instance of an object.

The Me statement on the main form triggers an error.

If I step over this the compiler completes sucessfully and the program will run. Problem is the method above return nothing.

0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33443413
you may need to loop through the controls in the form and panel
0
 

Author Comment

by:siacom
ID: 33443432
back to the first questrion )

how do I find out the activecontrol (current focus) ?

tried this ....
        For Each ctrl In Me.Controls
            what statement resolves current focus ?

        Next
0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33443519
ctrl.Focus()
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:siacom
ID: 33443524
i thought thats sets the focus not returns it ?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33443558
try something like this...
Dim control As Control

        For Each control In Me.Controls

            If control.Focused Then

                MsgBox("Focused Control: " & control.Name)

            End If

        Next

Open in new window

0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 33443645
Try this code...

Dim control As Control  

        For Each control In Me.inputComponent.Controls  

            If control.Focused Then  

                MsgBox("Focused Control: " & control.Name)  

            End If  

        Next

Open in new window

0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33443818
Use Me.ActiveControl to get the active control.

Wayne
0
 
LVL 2

Accepted Solution

by:
larkvale earned 500 total points
ID: 33448879

The correct way is to handle the GotFocus event on the controls you are interested in ...
(You can also handle the LostFocus event if you want to know when something loses focus)


    Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
        ' TextBox1 got the focus
        ' Enable / Disable your button as required
    End Sub

    Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus
        ' ComboBox1 got the focus
        ' Enable / Disable your button as required
    End Sub

    Private Sub CheckBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.GotFocus
        ' CheckBox1 got the focus
        ' Enable / Disable your button as required
    End Sub

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now