By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

New to VB, need help with sender.focus()

P: n/a
I'm in school, taking a VB .NET 2003 course, and the
teacher taught us how to use Sender.Focus() to return
focus to the control from which focus was received.

However, when I got home, and tried it on my own copy of
VB .NET 2003, it doesn't seem to work, in fact the only
method that comes up with Intellisense is GetType. I
can't find any information anywhere either in the MSDN
Library, or anywhere else for that matter. I was hoping
someone here could help me! I'm desperate!
Nov 20 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
* "Euphor2" <an*******@discussions.microsoft.com> scripsit:
I'm in school, taking a VB .NET 2003 course, and the
teacher taught us how to use Sender.Focus() to return
focus to the control from which focus was received.

However, when I got home, and tried it on my own copy of
VB .NET 2003, it doesn't seem to work, in fact the only
method that comes up with Intellisense is GetType. I
can't find any information anywhere either in the MSDN
Library, or anywhere else for that matter. I was hoping
someone here could help me! I'm desperate!


Use this:

\\\
DirectCast(sender, Control).Focus()
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
-----Original Message-----
* "Euphor2" <an*******@discussions.microsoft.com>

scripsit:
I'm in school, taking a VB .NET 2003 course, and the
teacher taught us how to use Sender.Focus() to return
focus to the control from which focus was received.

However, when I got home, and tried it on my own copy of VB .NET 2003, it doesn't seem to work, in fact the only method that comes up with Intellisense is GetType. I
can't find any information anywhere either in the MSDN
Library, or anywhere else for that matter. I was hoping someone here could help me! I'm desperate!


Use this:

\\\
DirectCast(sender, Control).Focus()
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.


I just tried your advice, and unfortunately it doesn't
seem to work either. The program compiles fine, the line
before it and after it both execute, but this particular
statement does nothing.

Thank you for trying to help.
Nov 20 '05 #3

P: n/a
* <an*******@discussions.microsoft.com> scripsit:
\\\
DirectCast(sender, Control).Focus()
///
[...] I just tried your advice, and unfortunately it doesn't
seem to work either. The program compiles fine, the line
before it and after it both execute, but this particular
statement does nothing.


1. Where do you use this statement?

2. Which type of control do you want to set the focus to?

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a
-----Original Message-----
* <an*******@discussions.microsoft.com> scripsit:
\\\
DirectCast(sender, Control).Focus()
///[...]
I just tried your advice, and unfortunately it doesn't
seem to work either. The program compiles fine, the
line before it and after it both execute, but this particular statement does nothing.


1. Where do you use this statement?

2. Which type of control do you want to set the focus

to?
--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.


1. I'm using the statement on the else leg of an if
statement, and it looks like this:

Else
txtBasePay.Text = "Works"
sender.focus()
txtTotalItemsSold.Text = "Works"

The line before and after it both execute. If you'll
notice, the autocorrect didn't capitalize anything
(whereas if I substitute your DirectCast line in there,
it does autocorrect... everything but the word sender,
though, even though it seems to recognize it's an object
while just using the line above), however it does not
show up as an error, and compiles clean. It just doesn't
do anything.

2. I'm trying to send focus back to whatever control
just lost focus. It seemed like an easy way to return
the user back to whatever control he was in before doing
something like clicking a button (i.e., if the user is in
the middle of typing something into a textbox, and clicks
on a button, after clicking the button, I wanted focus to
return back to the textbox). I understand I can
specifically state the object to return to, but this
worked just fine in class. However I can't seem to get
this working at home.

Thank you again for your help.
Nov 20 '05 #5

P: n/a
* <an*******@discussions.microsoft.com> scripsit:
1. I'm using the statement on the else leg of an if
statement, and it looks like this:

Else
txtBasePay.Text = "Works"
sender.focus()
txtTotalItemsSold.Text = "Works"

The line before and after it both execute. If you'll
notice, the autocorrect didn't capitalize anything
(whereas if I substitute your DirectCast line in there,
it does autocorrect... everything but the word sender,
though, even though it seems to recognize it's an object
while just using the line above), however it does not
show up as an error, and compiles clean. It just doesn't
do anything.
That's because 'sender' is declared as 'Object' and the type 'Object'
doesn't provide a 'Focus' method.
2. I'm trying to send focus back to whatever control
just lost focus. It seemed like an easy way to return
the user back to whatever control he was in before doing
something like clicking a button (i.e., if the user is in
the middle of typing something into a textbox, and clicks
on a button, after clicking the button, I wanted focus to
return back to the textbox). I understand I can
specifically state the object to return to, but this
worked just fine in class. However I can't seem to get
this working at home.


This will not work using 'sender'. AFAIK there is no easy way to find
out the control that has the focus before the button got it. You will
have to capture the 'ListFocus' event of every control and store a
reference in a private variable of type 'Control'. Then you can call
the 'Focus' method for the control referenced in this variable.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
-----Original Message-----

That's because 'sender' is declared as 'Object' and the type 'Object'doesn't provide a 'Focus' method. This will not work using 'sender'. AFAIK there is no easy way to findout the control that has the focus before the button got it. You willhave to capture the 'ListFocus' event of every control and store areference in a private variable of type 'Control'. Then you can callthe 'Focus' method for the control referenced in this

variable.
Well, thank you for your help. I swear I'm not crazy,
this DID work in school. Oh well, I guess I'll wait till
next week, when I see the teacher again.
Nov 20 '05 #7

P: n/a
Hi Euphor,

sender.focus

This works for me too - you weren't seeing things at College! ;-)

Although you haven't said it, I'm presuming that the code you showed us
was from the LostFocus handler of the Control in question.

As Herfried mentioned, sender is an Object parameter which means the
Intellisense won't give you anything other than the members for Object.
However, 'internally' sender is whatever Control has just lost focus. When I
did it using a button, for instance, sender was a Button Control.

However, despite being an actual Button, it is an Object <parameter> and
so the Button methods are 'hidden' from the IDE. But they <are> still
available.

There are three straightforward ways that you can get these Button methods
from sender.

The first way is easy, if you know the name of the method you simply type
it - in this case focus. This, again because of sender being an Object, will
not be capitalised because Intellisense doesn't know about Focus. But it will
work at runtime by a process called late-binding. This is where the object
says to itself 'I need a Focus method, do I have one?' Being a Button the
answer is yes. So your original code is one correct way, and it uses
late-binding.

Another method is the one that Herfried showed - you can DirectCast sender
to the appropriate type. And then, because you are converting the type to
Button, Intellisense can work. This is called early-binding. At runtime, the
Object does need to check itself, it just goes directly to the Focus method.

The third method assumes that the event handler is dedicated to a single
Control. In this case you simply type the name of the Control and don't bother
with sender at all.

When I tried it just now it worked fine. Once the button had Focus it
wouldn't give it up. You might find it useful to post the code where you are
doing the refocus.

Regards,
Fergus
Nov 20 '05 #8

P: n/a
* <an*******@discussions.microsoft.com> scripsit:
out the control that has the focus before the button got

it. You will
have to capture the 'ListFocus' event of every control

and store a
reference in a private variable of type 'Control'. Then

you can call
the 'Focus' method for the control referenced in this

variable.

Well, thank you for your help. I swear I'm not crazy,
this DID work in school. Oh well, I guess I'll wait till
next week, when I see the teacher again.


I think this will be the best way. Please let us know if/how you were
able to solve the problem or if you have any further questions.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #9

P: n/a
* "Fergus Cooney" <fi*****@post.com> scripsit:
sender.focus

his works for me too - you weren't seeing things at College! ;-)


This only works with 'Option Strict Off'.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #10

P: n/a
Cor
Hi Herfried,
I think you make a mistake, I am of course not sure but:
The first way is easy, if you know the name of the method you simply type
it - in this case focus. This, again because of sender being an Object, willnot be capitalised because Intellisense doesn't know about Focus. But it willwork at runtime by a process called late-binding. This is where the object
says to itself 'I need a Focus method, do I have one?' Being a Button the
answer is yes. So your original code is one correct way, and it uses
late-binding. Another method is the one that Herfried showed - you can DirectCast sender
to the appropriate type. And then, because you are converting the type to
Button, Intellisense can work. This is called early-binding. At runtime, theObject does need to check itself, it just goes directly to the Focus method.
The third method assumes that the event handler is dedicated to a single
Control. In this case you simply type the name of the Control and don't botherwith sender at all.
"Herfried K. Wagner [MVP]" Wrote

This only works with 'Option Strict Off' .

I think that that is only for the first method

When I am right I think it is good to correct that for the OP

Cor
Nov 20 '05 #11

P: n/a
* "Cor" <no*@non.com> scripsit:
I think you make a mistake, I am of course not sure but:


Not a mistake. I included the "sender.focus" in the message. I only
wanted to add that this will only work if 'Option Strict Off' is set.
"Herfried K. Wagner [MVP]" Wrote
A very important line is missing here... Maybe you forgot to quote it?

This only works with 'Option Strict Off' .

I think that that is only for the first method

When I am right I think it is good to correct that for the OP


I hope the OP doesn't miss the line...

;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #12

P: n/a
Hi Herfried,

|| > sender.focus
|| > I'm trying to send focus back to whatever control
|| > just lost focus
||
|| This will not work using 'sender'

Euphor is only just learning VB. At college they don't use Option Strict
On as it introduces too many errors that the students haven't the knowledge to
deal with yet. Late binding works very well in most cases. sender.focus is no
exception.

And there's me thinking you might

please be so kind as to say thanks for pointing out your mistake. ;-)

Regards,
Fergus
Nov 20 '05 #13

P: n/a
* "Fergus Cooney" <fi*****@post.com> scripsit:
sender.focus
I'm trying to send focus back to whatever control
just lost focus

This will not work using 'sender'
Euphor is only just learning VB. At college they don't use Option Strict
On


Did he tell that to you? No, he didn't.
as it introduces too many errors that the students
haven't the knowledge to deal with yet. Late binding works
very well in most cases. sender.focus is no exception.
It works, but I am sure Euphor wants to _learn_.
please be so kind as to say thanks for pointing out your mistake. ;-)


It was your mistake because you didn't understand the meaning of my
message.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #14

P: n/a
Hi Herfried,

ROFL.

|| > Euphor is only just learning VB. At college
|| > they don't use Option Strict On
||
|| Did he tell that to you? No, he didn't.

Other posts from Euphor

|| I'm in school, taking a VB .NET 2003 course, and the
|| teacher taught us how to use Sender.Focus()

|| but this worked just fine in class. However I
|| can't seem to get this working at home.

It works at school.
Therefore it must have compiled successfully.
Therefore they can't be using Option Strict On.

---------------------------------------------------
|| It works, but I am sure Euphor wants to _learn_

Yes, yet it's best to leave 'advanced' stuff for later. Option Strict On
gets in the way when you're on the ground floor. DirectCasting make code look
complicated - beginners + complicated is not a good mix.

|| It was your mistake

ROFL. You right again, of course. What was that mistake, again?

Regards,
Fergus

Nov 20 '05 #15

P: n/a
* "Fergus Cooney" <fi*****@post.com> scripsit:
Euphor is only just learning VB. At college
they don't use Option Strict On

Did he tell that to you? No, he didn't.
Other posts from Euphor


He didnt' mention that he uses 'Option Strict Off'.
I'm in school, taking a VB .NET 2003 course, and the
teacher taught us how to use Sender.Focus()
Stupid teacher. He didn't tell them why IntelliSense won't work.
Yes, yet it's best to leave 'advanced' stuff for later.


I think that's the wrong way. 'Option Strict On' is not "advanced"
stuff.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #16

P: n/a
Hi Herfried,

|| He didnt' mention that he uses 'Option Strict Off'.

He didn't need to.

Elementary deduction, my dear Watson.

The code compiles.
It's code that won't compile under Option Strict On.
Therefore Option Strict is Off

|| Stupid teacher. He didn't tell them why IntelliSense won't work

Maybe s/he did. Not all students hear everything. They don't always
understand the ramifications of things that they are told. And often they have
so much new information that they prioritise certain stuff away without
realising it's importance. Different people process different sized chunks.

On the other hand, maybe s/he didn't.

There's insufficient information to judge him or her. It's usually better
to withold such judgmental statements until there are more facts available.
|| > best to leave 'advanced' stuff for later.
|| > Option Strict On gets in the way when
|| > you're on the ground floor.
||
|| 'Option Strict On' is not "advanced" stuff.

I was careful to use the phrase 'when you're on the ground floor'. Perhaps
that was too colloquial?

How long is it since you were a raw beginner? Perhaps you've forgotten
what it's like. An array - (nothing to us) - is advanced stuff when you don't
know how to use it.

Empathy, Herfried, is a wondeful thing to develop.

Regards,
Fergus
Nov 20 '05 #17

P: n/a
* "Fergus Cooney" <fi*****@post.com> scripsit:
He didnt' mention that he uses 'Option Strict Off'.


He didn't need to.

Elementary deduction, my dear Watson.

The code compiles.
It's code that won't compile under Option Strict On.
Therefore Option Strict is Off


I never said that it won't compile with 'Option Strict Off'. I only
added that the code won't compile if 'Option Strict On' is set. If you
have a problem with that, let me know by mail.

EOT.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #18

P: n/a
Go to bed little boy - you've got overwrought again and your logic units are
faiing.
Nov 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.