Find last control to have focus

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
LVL 7
NavicertsAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
 
Fernando SotoRetiredCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
NavicertsAuthor Commented:
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
 
NavicertsAuthor Commented:
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
 
Fernando SotoRetiredCommented:
Did you try the first solution?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
NavicertsAuthor Commented:
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
 
NavicertsAuthor Commented:
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
 
NavicertsAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
NavicertsAuthor Commented:
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
 
NavicertsAuthor Commented:
damn...  the code for the button finishes running rendering my changedtext on the text boxes useless :(
0
 
NavicertsAuthor Commented:
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
 
Fernando SotoRetiredCommented:

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
 
NavicertsAuthor Commented:
That last code i posted has everything in it.  It is the TextChanged functions that are not functioning properly anymore, not sure why.
0
 
NavicertsAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
NavicertsAuthor Commented:
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
 
Fernando SotoRetiredCommented:
Well that is great. :=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.