Phill Cookie
Phill Cookie

Reputation: 239

Switch on sender for shared LostFocus event handler

I have a form with seven textboxes, all describing different attributes of a used car. In the shared LostFocus event of my textboxes, I need a case statement which determines the particular TextBox that lost focus. I then need to perform different various tasks, but I don't think they matter to my question.

How can I tell which textbox lost focus in my shared LostFocus event handler?

Select Case ________???

Upvotes: 2

Views: 692

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

You can do one of two things.

The first is to select on the name

Dim tb as TextBox = CType(sender,TextBox)

Select Case tb.Name
    Case "TextBox1"

The second is what I prefer to do, it is to use the Tag property of the TextBox and put an unique number in it. So in that case your Select statement would look like.

Dim tb as TextBox = CType(sender,Textbox)

Select Case CInt(tb.tag)
    Case 1

    Case 2
    .... 

The other thing that you mentioned was how to make sure of which TextBox lost its focus. There is a LostFocus Event you can Handle also in addition to the Leave event you are handling now.

Upvotes: 1

Ry-
Ry-

Reputation: 224913

That would be Select Case DirectCast(sender, TextBox).

Upvotes: 2

Related Questions