Results 1 to 10 of 10

Thread: [RESOLVED] Form2 does not get focus

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Resolved [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

  2. #2
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Form2 does not get focus

    CIAO
    Tell me how to use MDI
    Thanks for answering
    A. Haider

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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:
    1. Public Class Form1
    2.  
    3.     Private frm2 As New Form2
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         frm2.ShowDialog()
    7.     End Sub
    8. 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:
    1. Public Class Form1
    2.  
    3.     Private frm2 As New Form2
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         frm2.Show()
    7.     End Sub
    8.  
    9.     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    10.         frm2.Focus()
    11.     End Sub
    12. 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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Form2 does not get focus

    Quote 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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Form2 does not get focus

    CIAO
    Thanks a lot 7 Halo and crushinator
    its done
    Showdialog property works fine

  7. #7
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    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

  9. #9
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    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.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: [RESOLVED] Form2 does not get focus

    thnx buddy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width