Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find last control to have focus

Posted on 2006-05-25
20
Medium Priority
?
336 Views
Last Modified: 2010-04-23
Hello,

I am trying to find the last control to have focus.  I have a touch screen keypad that I want to be able to use but as soon as you hit a button the button has the focus so nothing happens.  Any ideas on how i can get the last control that had focus through code would be appreciated.

thanks!

-Navicerts
0
Comment
Question by:Navicerts
  • 11
  • 7
  • 2
20 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16762710
Hi Navicerts;

This is how you can do what you want.

    ' Define a class level control to hold the last control that had focus
    Private LastControl As Control

    ' Add all the controls that you want to return to after a button is clicked on
    ' to the Handles part of this Sub as shown below
    Private Sub LastControl_LostFocus(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles CheckBox1.LostFocus, ListBox1.LostFocus, _
        RadioButton1.LostFocus, TextBox1.LostFocus

        LastControl = CType(sender, Control)

    End Sub

    ' In each of the button events at the end set focus to LastControl
    Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Do what ever you need to here
        LastControl.Focus()

    End Sub


Fernando
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16762749
Another example...

Public Class Form1

    Private lastControl As Control

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is Button Then
                AddHandler ctl.Click, AddressOf Button_Click
            Else
                AddHandler ctl.GotFocus, AddressOf Control_GotFocus
            End If
        Next
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not IsNothing(sender) Then
            lastControl.Focus()
            SendKeys.Send(CType(sender, Button).Text)
        End If
    End Sub

    Private Sub Control_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        lastcontrol = sender
    End Sub

End Class

0
 
LVL 7

Author Comment

by:Navicerts
ID: 16762788
Ok, i tried to impliment something like this before but failed, let me give it another shot with these examples, ill post my code if i cant get it to work soon.

Thanks guys.

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

 
LVL 7

Author Comment

by:Navicerts
ID: 16762871
flaw in my plan here, i am in the .NET compact framework, i am working without the "SendKeys" function, evreything else is working ok (or at least compiles ok).
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16762921
Did you try the first solution?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16762997
What other controls are on your form?

For TextBoxes you could do...

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not IsNothing(sender) Then
            lastControl.Focus()
            If TypeOf lastControl Is TextBox Then
                Dim tb As TextBox = CType(lastControl, TextBox)
                tb.SelectedText = CType(sender, Button).Text
            End If
        End If
    End Sub


0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763010
Same thing, it is the "' Do what ever you need to here" part that i am having trouble with now.  Assuming the correct control has the focus how can i send it a character?  for my "1" button i put "LastControl.Text = LastControl.Text + "1"" in there but when i hit the button im erroring out.

I am compiling and installing the CAB every time i try the program ill try to run it in debug mode over the comm port so i can get an error message and ill post back.
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763074
Here is all the code sections that are involved that i have in right now....  It is pretty much ver-baitum what was provided.  I am crashing on this line "If TypeOf LastControl Is TextBox Then" with a NullRefrenceException.

The control that had focus was a textbox when i hit the button.


-----------------------------------------------------------------------------------------------
Private LastControl As Control


    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        ShowPanel(Panelwingband)

        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is Button Then
                AddHandler ctl.Click, AddressOf cmd1_Click
            Else
                AddHandler ctl.GotFocus, AddressOf Control_GotFocus
            End If
        Next
    End Sub


    Private Sub Control_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        LastControl = sender
    End Sub




    Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
        If PanelDefects.Left = 0 Then
            If Defects(0) = "Toes" Then
                Defects(0) = ""
            Else
                Defects(0) = "Toes"
            End If
            GetDefectLetters()
        End If
        If Not IsNothing(sender) Then
            LastControl.Focus()
            If TypeOf LastControl Is TextBox Then
                Dim tb As TextBox = CType(LastControl, TextBox)
                tb.SelectedText = CType(sender, Button).Text
            End If
        End If
    End Sub


0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763101
If i run msgbox(LastControl) it is blank.  I must be doing something wrong, i dont really have a full understanding of whats goin' on :)
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16763125
Hi Navicerts ;

If the controls are just TextBoxes then do this.

    Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Do what ever you need to here
        Dim tb As TextBox = CType(LastControl, TextBox)
        tb.AppendText("1")
        tb.Focus()

    End Sub

If all the controls that you want to set focus back to are text boxes then this code is better:

    Private LastControl As TextBox

    Private Sub LastControl_LostFocus(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus

        LastControl = CType(sender, TextBox)

    End Sub

    ' In each of the button events at the end set focus to LastControl
    Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Do what ever you need to here
        LastControl.AppendText("1")
        LastControl.Focus()

    End Sub

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16763220
Hi Navicerts;

The reason you were getting the error with the first solution I gave you is that I set the Class level variable to a type of Control. This is the parent class for all controls. So when you attempted to set text in a text box it failed because the Control Class knows nothing about a text property. You must cast the control to a TextBox, if in fact that is what the control is to get access to the text property itself.

Fernando
   
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763257
Ok, thats working, kind of...  It is working to enter in text.  I do not have the "AppendText" function so i had to use LastControl.text = LastControl.text + "1" instead.  I have textchanged event handlers on the text boxes as well, so that once it reachs a certain Len(ControlName.text) it moves onto the next field.

this is where it gets odd!  On my first text box (BeginWBIn.text) nothing happens, the text changed event handler wont fire.  Once i manually move the cursor to the EndWBIn.text textbox the changed event handler functions and moves on accordingly.
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763327
damn...  the code for the button finishes running rendering my changedtext on the text boxes useless :(
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763340
Posting code so you know where im at....


    Private LastControl As TextBox
    Private Sub LastControl_LostFocus(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles BeginWBIn.LostFocus, EndWBIn.LostFocus, WeightIn.LostFocus, BCIn.LostFocus, FeaIn.LostFocus
        LastControl = CType(sender, TextBox)
    End Sub

    Private Sub BeginWBIn_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeginWBIn.TextChanged
        If Len(BeginWBIn.Text) = 2 Then
            EndWBIn.Focus()
        End If
    End Sub
    Private Sub EndWBIn_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndWBIn.TextChanged
        If Len(EndWBIn.Text) = 4 Then
            smallwbw.Text = BeginWBIn.Text + EndWBIn.Text
            WBALL.Text = smallwbw.Text
            ShowPanel(Panelweight)
            WeightIn.Focus()
        End If
    End Sub

    Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
        If PanelDefects.Left = 0 Then
            If Defects(0) = "Toes" Then
                Defects(0) = ""
            Else
                Defects(0) = "Toes"
            End If
            GetDefectLetters()
        End If

        LastControl.Text = LastControl.Text + "1"
        LastControl.Focus()

    End Sub
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16763343

Not sure what you mean by this statement.

this is where it gets odd!  On my first text box (BeginWBIn.text) nothing happens, the text changed event handler wont fire.  Once i manually move the cursor to the EndWBIn.text textbox the changed event handler functions and moves on accordingly.

Or this this :

damn...  the code for the button finishes running rendering my changedtext on the text boxes useless :(


Can you post some code?

Fernando
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763501
That last code i posted has everything in it.  It is the TextChanged functions that are not functioning properly anymore, not sure why.
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763528
Figured it out.  I over looked the line "LastControl.Focus()" which was un-doing the code i had in place to move the focus to the next field.  Seems to be working now, thanks!

-Navicerts
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16763586
The code I had you add just modifies a form level variable and that is all until a button is pressed that sends the focus back to the last text box you were in. I do not see any conflicts with the TextChange events.

What is not working correctly?


Fernando
0
 
LVL 7

Author Comment

by:Navicerts
ID: 16763650
It is working fine now.  I had my textchanged event setting the focus to the next control after a certain amount of charcters were entered.  So when i reached that amount my changedtext function put the focus on the next control then the line "LastControl.Focus()" that i put in the button set the control back to the original.

I removed the "LastControl.Focus()" line and now it sends the focus to the next control once it reaches a set amount of characters as expected.

So it's working good now, no problems.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16763706
Well that is great. :=)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

810 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