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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To... 2

Status
Not open for further replies.

bburnell

Programmer
Sep 15, 2000
560
US
Hi all,

If I have a form set to "not shown in taskbar" and the user clicks off of it to lose focus, is there a way to keep the focus?

Also, when I re-show the original form, how do I force the form to "get" focus?

This is probably an easy and obvious solution, but I'm frustrated.

Thanks!
Brett

P.S. I am using VB 2005.
 
Windows works with only one form being in focus. But to force a form to be in focus you us MyForm.Focus(), it returns a boolean telling you if it got focus.
 
To keep the focus you normally call the .ShowDialog() instead of the .Show() method. That form will always have the focus untill by code you clode it (.Close() method) or by sending a DialogResult.

You need to let us know more on your problem, eg is there an mdi form, etc.
I dont know if the following is what you want, but have a look at the TopMost property.

Regards
 
I am already using ShowDialog, which works fine for the child form. When I close the child form, the parent doesn't loses focus.

My other plea for help is a way to keep a form "always on top." I have the code to do it in vb6, but not in .net.

Thanks!
Brett
 
As an FYI, {object}.focus is not working as advertised!

Thanks,
Brett
 
Yes, the .focus does not bring the form to front...

As for the "always on top", i had suggested to have a look at the TopMost property.
 
Am i right it saying that you have a parent form, that opens up a child form using ShowDialog, and that you are trying to force the parent to keep its focus?
If so then you won't be able to do it because the ShowDialog locks all your other forms until it has been closed. This also explains why my earlier suggestion didn't work.

The answer to your second question: Me.TopMost = True
 
No. I am trying to force the child form to have focus if they try to click off of it (outside the chld form). Does that make sense? Your second suggection may be the key. I'll try it. Thanks!
Brett
 
That worked. Thank you so much! Now if I could figure out why focus() only works now and then on my textbox. LOL.
Brett
 
Have a try..

Private Sub Form1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.LostFocus

Me.Activate()

End Sub

Tnx for your *
 
I get it now,
The only problem with your idea is that if for some reason your app crashes, the user will not be able to shut it down because every time they try to right click (show menu) on your app in the task bar it will re-show the form.

My question is, why stop the user selecting a different application???[ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top