Solved

VB .NET 2010 find out which control has focus

Posted on 2010-08-15
10
1,796 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Can I fix my MouseHover Event? 14 27
Separate a number and letter from a string 25 27
Hide Tab Page 3 20
Showdialog 8 21
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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