Euphor2 Posted October 25, 2003 Posted October 25, 2003 I'm new to VB, and am currently enrolled in a class at my local community college, which uses VB .NET 2003. My instructor showed us something that wasn't in the textbook, but seemed extremely useful, using Sender.Focus() to return focus to a prior control. He demonstrated it, and all the students on all the computers in class used it successfully. However, when I try it at home, it doesn't work. In fact, the only method that intellisense seems to bring up for Sender is "GetType". However, there is no compile error if I try using Sender.Focus(), it just doesn't do anything at all. I've asked for help on several forums, and one helpful person said I should try: DirectCast(sender, Control).Focus() Unfortunately, this had the same effect...it did nothing. The statement before it and after it both execute. Can anyone help by telling me what I'm doing wrong? Quote
*Experts* mutant Posted October 26, 2003 *Experts* Posted October 26, 2003 The sender argument is an object argument, in case of controls a control instance is passed into the sender argument. Casting the sender object should work. What errors does it give you? If people at your school successfully used Sender.Focus() without doing anything to the objecty it means they dont use Option Strict which is a bad mistake. Quote
Euphor2 Posted October 26, 2003 Author Posted October 26, 2003 In class, we've actually gone over the fact that sender cannot be used with Option Strict On, but in this case it's not extremely important, as I'm still learning the very basics of Basic. As far as errors go, what's extremely strange is that I do not get any errors at all. It recognizes sender as an object, regardless of whether I use Sender.Focus() or the DirectCast statement. However, when I just use Sender.Focus() it does not autocorrect any capitalization or anything (though it doesn't give me any errors either!) When I use DirectCast, it WILL autocorrect the capitalization, on everything EXCEPT sender. However, neither statement has any noticable effect. Wierd. On another forum, someone mentioned that since Sender is an object, an objects have no method called Focus, THAT is the reason why it's not working. He says he's never even heard of being able to reference the control that just lost focus. But I swear to you, on all that is holy, this worked in my class, but apparently just not for me at home. :confused: Quote
wyrd Posted October 26, 2003 Posted October 26, 2003 Perhaps you missed a step and didn't realize it. It's not hard to forget some things. If you paste us the current code that you have now we'd be able to help further. It sounds like you're doing this via some event, but we don't know which event you're using (perhaps you accidently clicked the wrong event to create, which is why nothing is happening). If you're not using an event, then you need to explain further. :) Again, code would help. Assuming you have option script off (which you shouldn't, but your instructor may have since it sounds like this is a beginners course, and it would simplify things for those just learning), Sender.Focus() would work. Quote Gamer extraordinaire. Programmer wannabe.
Euphor2 Posted October 26, 2003 Author Posted October 26, 2003 Keeping in mind that this is a beginner's course, this is a no-frills program. Here's a sample of where I'm using it. It's written inside a button's click event. txtBasePay.Text = "Works" sender.focus() txtTotalItemsSold.Text = "Works" As mentioned before, both the line before and after work fine. Both textboxes will contain the literal "Works". The statement in the middle, the one intended to change the focus, neither autocorrects, nor does it seem to do anything at all when the program executes. Also, upon the suggestion of someone else, I tried this: txtBasePay.Text = "Works" DirectCast(sender, Control).Focus() txtTotalItemsSold.Text = "Works" Once again, statements 1 and 3 work fine. The only difference here is that while coding the line that changes focus, it does indeed autocorrect (all except the word sender), however it still has no noticable effect when the program runs. Quote
wyrd Posted October 26, 2003 Posted October 26, 2003 The sender in the click event is the button that was clicked. If you clicked the button, then it's already got the focus. So your code doesn't do anything. And VB.NET isn't case sensitive, so whether you use Sender or sender doesn't matter. What exactly are you trying to set the focus to? Quote Gamer extraordinaire. Programmer wannabe.
Euphor2 Posted October 26, 2003 Author Posted October 26, 2003 Yay! Thank you! That's exactly what the problem was! I think I understand it now... I wasn't quite clear what sender really was, I guess. I always looked at it as though the sender was whatever previous control had focus (i.e., sort of like the program saying "hey, that control over there is the one that 'sent' focus to me. That's the sender."). I just thought that you could use sender anywhere, under ANY event, to call methods for the last used control that had focus. Pretty stupid, huh? Anyways, I now recall that the teacher used it in things like validating events or lost focus events, etc, because they're still events for that particular control. Thank you Wyrd, and thank you everyone else for listening to a newbie's stupid questions. :) Quote
wyrd Posted October 26, 2003 Posted October 26, 2003 Nah.. the sender is the object that sent/triggered the event (hence the name). In a lost focus event, the sender is the object that lost the focus. In the click event, it's the object that was clicked. Etc.. BTW, what school are you taking this class at? Quote Gamer extraordinaire. Programmer wannabe.
Euphor2 Posted October 26, 2003 Author Posted October 26, 2003 Manchester Community College, located in Manchester, CT. Thanks again for all of your help by the way. Even though I spent hours on this, I came away with a better understanding of VB in general (regarding my original misunderstanding of the relationship that events have with the objects that these events are relate to). Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.