|
-
Feb 5th, 2006, 02:28 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Form2 does not get focus
Greetings
I am a VB6 programmer as well as of VB.NET
I can easily unload (close) a form and open other in VB6 but in .NET it seems a little tricky
I however have done that but
When my other form opens it is not focused
I tried to find get focus property but could not find it, I have also tried to close the first form but then the whole application is closed.
I also try to use hiding the object the code of hiding is correct as it shows no error but the form does not hide and I have to explicitly focus on other form
Public Class Form1
Inherits System.Windows.Forms.Form
Dim x As New Form2
Dim y as New Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x.Show()
y.hide()
End Sub
I have also used 'Me' keyword but in vain.
Q1. How do I get focus on the other form?
Q2. How can I close form1 without closing whole application?
Awaiting your reply and thanks in advance
-
Feb 5th, 2006, 02:34 AM
#2
Addicted Member
Re: Form2 does not get focus
Hi Abbas Haider,
For Q1:
Have you Tried:
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim x As New Form2
Dim y as New Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With x
.Show()
.BringtoFront()
End With
y.hide()
End Sub
Also, have you tried using an MDI? They work well for multiple form Applications in my opinion.
~Crush
EDIT: Also there is a Focus method to Forms.
So you could Try
Code:
With x
.Show()
.Focus()
End With
Last edited by Crushinator; Feb 5th, 2006 at 02:38 AM.
Using Framework 2.0, VB.Net 2005.
-
Feb 5th, 2006, 02:38 AM
#3
Thread Starter
Hyperactive Member
Re: Form2 does not get focus
CIAO
Tell me how to use MDI
Thanks for answering
A. Haider
-
Feb 5th, 2006, 02:41 AM
#4
Re: Form2 does not get focus
Welcome to VBF 
Load Event Summary: "Load: Occurs before a form is displayed for the first time."
Since all of this is happening before the Form is loaded, it will grab focus on completion. Also, you don't need to declare an instance of form1. The class your working in is that form. "Me" will reffer to Form1.
My first question is, what is Form2? Is it something you need them to complete before Form1 is shown? If that's the case, you can use showdialog:
VB Code:
Public Class Form1
Private frm2 As New Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
frm2.ShowDialog()
End Sub
End Class
This will lock the user into Form2 (Form1 won't be shown) until they close that form.
The other thing you can do is force the focus:
VB Code:
Public Class Form1
Private frm2 As New Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
frm2.Show()
End Sub
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
frm2.Focus()
End Sub
End Class
The Shown event is raised the first time form1 is displayed (it occurs after Load). I think this is will only work in VS2005 (I don't rember it in VS2003).
Last edited by sevenhalo; Feb 5th, 2006 at 03:07 AM.
Reason: Load is an event, not a method
-
Feb 5th, 2006, 02:43 AM
#5
Thread Starter
Hyperactive Member
Re: Form2 does not get focus
 Originally Posted by Crushinator
Hi Abbas Haider,
For Q1:
Have you Tried:
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim x As New Form2
Dim y as New Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With x
.Show()
.BringtoFront()
End With
y.hide()
End Sub
Also, have you tried using an MDI? They work well for multiple form Applications in my opinion.
~Crush
EDIT: Also there is a Focus method to Forms.
So you could Try
Code:
With x
.Show()
.Focus()
End With
The bring to front is still not working
-
Feb 5th, 2006, 02:50 AM
#6
Thread Starter
Hyperactive Member
Re: Form2 does not get focus
CIAO
Thanks a lot 7 Halo and crushinator
its done
Showdialog property works fine
-
Feb 5th, 2006, 02:54 AM
#7
Addicted Member
Re: Form2 does not get focus
Great! Glad to hear it. Yes I had forgotten ShowDialog forces Form2 on top, Thanks SevenHalo.
Abbas Haider, please remember to mark your threads as Resolved when the solution has been reached.
~Crush
Using Framework 2.0, VB.Net 2005.
-
Jul 6th, 2006, 05:55 AM
#8
Thread Starter
Hyperactive Member
Re: [RESOLVED] Form2 does not get focus
CIAO
Form1 is still in memory isnt it when you use showdialog method
How can I completely close a form without disturbing other form
Thanks in advance
A. Haider
-
Jul 6th, 2006, 05:41 PM
#9
Hyperactive Member
Re: [RESOLVED] Form2 does not get focus
If you close your primary form without setting a different primary form, your entire program will close. Fear not! You can change to primary form as described here.
-
Jul 7th, 2006, 12:31 AM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED] Form2 does not get focus
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|