?
Solved

VB .NET 2010 find out which control has focus

Posted on 2010-08-15
10
Medium Priority
?
2,171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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