Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Figure out what textbox has focus

Status
Not open for further replies.

Kliot

Programmer
Jan 10, 2003
622
US
I have a form with a number of textboxes on it I want to be able to press a button and do something based on what textbox the cursor is in. It seems like this should be pretty simple, what am I missing? If I check to see what textbox.focused I get false because the button has focus.

Thanks
Perrin
 
You could set a variable to a certain value when a textbox gets the focus. This way when the focus is moved to the button, the variable will remain the same. Then you need to 'invent' your own system, determining the textbox when you got the value in the variable.
 
Thanks, that's exactly what I ended up doing, I was just hoping that there was a built in property that would do the same.
 
I don't think there's a built in function for getting the previous focussed item. But I think this code shouldn't be too hard to worry about.

good luck with it
 
That's essentially how my company handled the issue in a vb6 app (a module level control reference set through GotFocus). At least with dotnet and it's multicast events, you don't need to write a seperate event handler for every control...

mmilan
 

You can do something like this also:
Code:
private sub Textbox_KeyPressed(sender as object, e as eventargs) handles Textbox1.Keypressed, Textbox2.keypressed, ...
  dim txtFocused as textbox = ctype(sender, textbox)
  'txtFocused will now be pointing at which ever textbox the user clicked the button in.
end sub

I'm not positive on the eventargs type, there may be a specific keypressed event args. Easiest thing to do would be to double-click on the textbox in the design window so that the sub is created for you. Then just add the rest of your textboxes onto the end of the handles list.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I would still choose the gotFocus event rather than the key_pressed. If it's about the focus, and the user didn't enter anything in the textbox, your output would be wrong.
 
Ahh, I see, I miss-read the original post. I thought he was talking about pushing a key, not clicking on a button. My bust.

You are correct, the GotFocus event from the button would be the best way.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks all for the advice, i attached the code to the Enter event and it works fine, you mention the GotFocus event? Is there a reason to use that low-level event over the Enter event?

Perrin
 
Try this
Code:
Inherits post of Chrissie1 in thread796-1399892
Code:
Public Class Form1
    Dim CTL As Control
    Dim PrevControl As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(PrevControl)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each CTL In Me.Controls
            AddHandler CTL.GotFocus, AddressOf PreviousControl
        Next

    End Sub
    Sub PreviousControl(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each CTL In Me.Controls
            If TypeOf CTL Is TextBox And CTL.Focused = True Then
                PrevControl = CTL.Name.ToString
            Else
                '====?
            End If

        Next
    End Sub
End Class





________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Can you explain your code a little Zameer? i'm quite interested to know how AddressOf PreviousControl works.

Greetz,
Joeri
 
OK , I am here after holidays..

Joeri, It is clear..

We have a Sub (sub PreviousControl) with us. On Load of the form we add handler to the controls reference of the sub.
It is like a button added on runtime and adding a sub to one of it's events.

See more examples here.



________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Oh, i see.
thanks for the comparison and the links. They were very usefull. If I get AddressOf on my MCAD exams, I owe you one :).

* Gotta start studying :)

greetz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top