Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB .NET 2010 find out which control has focus

Posted on 2010-08-15
10
Medium Priority
?
2,248 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 48

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

885 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