Solved

VB .NET 2010 find out which control has focus

Posted on 2010-08-15
10
2,061 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

635 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