[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
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.
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.
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
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:
Me.mypicbox.Location = New System.Drawing.Point(Me.Cursor.Position.X, Me.Cursor.Position.Y)
should be this:
vb.net Code:
Me.mypicbox.Location = Windows.Forms.Cursor.Position
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.