Results 1 to 6 of 6

Thread: [RESOLVED] [02/03] Incorrect form location at first .Show()

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Resolved [RESOLVED] [02/03] Incorrect form location at first .Show()

    In my project I have Form1, the main form, and Form2. Form2 is a info-bubble like panel which holds a PictureBox. I need to show Form2 at certain times, but its location is determined by the location of the mouse.

    It is working fine, except the first time Form2 always shows somewhere near the uppper left corner of the screen. Only after the first time Form2 is shown will it popup in the correct place. This example uses .Show and .Hide, I have also tried replacing those with .Visible = False and .Visible = True, respectively.

    In Form1:
    Code:
    Dim mypicbox as New Form2
    
        Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        Me.mypicbox.Location = New System.Drawing.Point(Me.Cursor.Position.X, Me.Cursor.Position.Y)
        Me.mypicbox.Show()
        End Sub
    And then I hide it with other code, but the problem is the first time Form2.Show() is called, it doesn't show in the correct location. Everytime after that it does.

    I was able to solve this problem by showing and hiding Form2 on Form1 load, but that makes Form2 flicker, and it looks unprofessional.

    Does anyone have a solution?
    Thanks
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [02/03] Incorrect form location at first .Show()

    Change the constructor of Form2 to include that location setup after the InitializeComponent() call and see if that helps.
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [02/03] Incorrect form location at first .Show()

    Sorry, I don't know what that means.

    I tried adding public variables LocationX and LocationY. I set them appropriately, and then put Me.Location = New System.Drawing.Point(Me.LocationX, Me.LocationY) at the end of InitializeComponent(). Is that what you meant? It didn't work anyway, but I think I didn't do what you suggested.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [02/03] Incorrect form location at first .Show()

    Try this. I'm not sure if it make any difference though...
    Code:
    Dim mypicbox as Form2 = Nothing
    
    Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        If Me.mypicbox Is Nothing Then
             Me.mypicbox = New Form2()
             Me.mypicbox.StartPosition = FormStartPosition.Manual
        End If
        Me.mypicbox.Location =  New System.Drawing.Point(Me.Cursor.Position.X, Me.Cursor.Position.Y)
        Me.mypicbox.Show()
    End Sub
    Last edited by stanav; Oct 24th, 2007 at 07:40 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Incorrect form location at first .Show()

    You need to set the StartPosition property of the form to Manual. Also, this:
    vb.net Code:
    1. Me.mypicbox.Location =  New System.Drawing.Point(Me.Cursor.Position.X, Me.Cursor.Position.Y)
    should be this:
    vb.net Code:
    1. Me.mypicbox.Location =  Windows.Forms.Cursor.Position
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [02/03] Incorrect form location at first .Show()

    Thanks everyone. I had never used StartPosition.Manual before. Apparently, neither had my friend. It works fine now. Thanks again.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

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