How do you give an object focus? I found this in MSDN, but it didn't work:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Focus() End Sub
Thanks in advance,
Furry
Printable View
How do you give an object focus? I found this in MSDN, but it didn't work:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Focus() End Sub
Thanks in advance,
Furry
VB Code:
Private Sub frmTimeCompare_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated Secondtxt.Focus() End Sub
I tried many ways but I only got this way work.:D
It's in the "Activated Event" of the form
well, the problem with that is that the activate event wont only fire at form_load :( I guess you should declare a boolean variable like this so that it would only happen once, otherwise everytime your form gets activated, your focus will move to textbox1
VB Code:
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated Static firstActivate As Boolean = False If Not firstActivate Then TextBox1.Focus() firstActivate = False End If End Sub
Forgot that there is a big difference btw. Load and Activated:p
I was about to say that Mr.P.lol:D :D
Thanks for the help, guys. Unfortunately, it didn't work. The code you posted, MrPolite, is the same as follows, correct?
VB Code:
Static firstActivate As Boolean = False If firstActivate = True Then TextBox1.Focus() firstActivate = False End If
This requires the user to unselect, and then reselect the form, and every time they do so, it gives focus back to the text box. :confused:
well you have to change your code to if firstActive = false, otherwise it will never runQuote:
Originally posted by Fat_N_Furry
Thanks for the help, guys. Unfortunately, it didn't work. The code you posted, MrPolite, is the same as follows, correct?
VB Code:
Static firstActivate As Boolean = False If firstActivate = True Then TextBox1.Focus() firstActivate = False End If
This requires the user to unselect, and then reselect the form, and every time they do so, it gives focus back to the text box. :confused:
The code I wrote ^ would select the textbox only the first time the form is activated (when it's loaded). So I thought that's what you want, isnt it? did you try my code? that works
If you put that code in form_activate, it will select TextBox1 when the form is loaded and it would aviod doing the same thing if the form gets activated again.
Once it didn't work when I typed it in, I copied and pasted it in, and it still didn't work. Here's my current code:
VB Code:
Private Sub frmTagger_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated Static firstActivate As Boolean = False If Not firstActivate Then rtbEditor.Focus() firstActivate = False End If End Sub
It doesn't even give it focus if I just simply type
VB Code:
Private Sub frmTagger_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated rtbEditor.Focus() End Sub
So I think there might be something else wrong not having to do with the code.
Ok after trying for 15 minutes I finally found a way to do it.
Put TextBox1.Focus() in the TabPage paint event handler that the textbox is on.
tabpage was for another thread:D
btw puting code in paint event wouldnt be good. If the user switches to another program and then switches back to your app, it will cause a paint event to fire, which will then cause the focus to move! (ouch:D)
Thought it was the same thread :p, anyway off to watch SpiderMan:D.
LOL my stupid family is going to watch that possibly tonight on DVD for the second time. :rolleyes: I almost *never* watch a movie twice, because I know what's going to happen - good guys win, bad guys die or are put in prison for life, and have an evil twinkle in their eye at the end of the movie, forshadowing the sequel where they escape and wreak havoc. My SISTER, however, will watch a movie 10-15 times, literally! I swear her brain is a big gooey mush. And then there's my brother, which I will not discuss in public. :eek:Quote:
Originally posted by DevGrp
...anyway off to watch SpiderMan:D.
Anyway, now that I've given you information about my family that you probably didn't want to know, back to the topic of focus. Does anybody know what's wrong with it?
Furry
LOL:DQuote:
Originally posted by Fat_N_Furry
LOL my stupid family is going to watch that possibly tonight on DVD for the second time. :rolleyes: I almost *never* watch a movie twice, because I know what's going to happen - good guys win, bad guys die or are put in prison for life, and have an evil twinkle in their eye at the end of the movie, forshadowing the sequel where they escape and wreak havoc. My SISTER, however, will watch a movie 10-15 times, literally! I swear her brain is a big gooey mush. And then there's my brother, which I will not discuss in public. :eek:
Anyway, now that I've given you information about my family that you probably didn't want to know, back to the topic of focus. Does anybody know what's wrong with it?
Furry
umm you wanna upload a part of your project? maybe someone can fix it? ;)
Sure! Actually, I'll upload all of it! ;)
hi
that code just need a slight change and it works perfectly.
Private Sub frmTagger_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Static firstActivate As Boolean = False
If Not firstActivate Then
rtbEditor.Focus()
firstActivate = true ' change this line
End If
End Sub
dd
hi
that code just need a slight change and it works perfectly.
Private Sub frmTagger_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Static firstActivate As Boolean = False
If Not firstActivate Then
rtbEditor.Focus()
firstActivate = true ' change this line
End If
End Sub
and tab page was my thred!:D
hmmmmmmmmmm!!
why cant I set breakpoints in you app?!!!
Thanks for everyone's help. I really appreciate it. You're all going to call me a liar for saying this, but I'm going to say it anyway - it still doesn't work. :eek: Just to clarify that we're talking about doing the same thing, here, I'll restate what we're trying to do: we're trying to give the rich text box the focus when the form loads, so the users can all just start typing right away. Can anybody think of anything else that could be wrong with this form or the code we're using? Frankly, I'm starting to think I may have a virus or something... :eek:
Furry
Ah! I found a way to do it using the load event and a timer. It's a pretty cheap little trick, but it works.
VB Code:
Private Sub frmTagger_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmrTimer.Start() End Sub ------------------------------------------------------------------------------------ Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick rtbEditor.Focus() tmrTimer.Stop() End Sub
tmrTimer's interval is set to 1. It's too bad that it can't be done from the Form_Load event at this point.
Furry
Couldn't you just set the TabIndex of the RTB to 0?
doesnt work. Download his project... it's weird . And I also dont know why I cant set any breakpoints in his code :confused:
Very *VERY* weird. :eek: