Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Eleкtro en 8 Enero 2013, 01:25 am



Título: [SOLUCIONADO] No me deja emparentar el form (Form.parent =)
Publicado por: Eleкtro en 8 Enero 2013, 01:25 am
En el evento OnMouseHover de un picturebox estoy intentando mostrar un form con la propiedad "CenterParent", pero cuando intento emparentar el form no me deja iniciar la aplicación y me salta error.

Código
  1.    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.        Try
  3.            Form2.Parent = Me
  4.        Catch ex As Exception
  5.            MsgBox(ex.Message)
  6.            ' Error: top-level control cannot be added to a control
  7.        End Try
  8.    End Sub


Título: Re: No me deja emparentar el form (Form.parent =)
Publicado por: Eleкtro en 8 Enero 2013, 17:34 pm
Ya lo he conseguido, os dejo la solución (Vivan los snippets xD):


Main form:

Código
  1.    Private Sub OnMouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
  2.        Form2.Show()
  3.    End Sub
  4.  
  5.    Private Sub OnMouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
  6.        ' Hay que liberarlo, con un form2.close o form2.hide solo conseguiremos que se centre la primera vez!
  7.        Form2.Dispose()
  8.    End Sub
  9.  
  10. #Region " CenterForm function "
  11.  
  12.    Function centerForm(ByVal Form_to_Center As Form, ByVal Form_Location As Point) As Point
  13.        Dim pLocation As New Point
  14.        pLocation.X = (Me.Left + (Me.Width - Form_to_Center.Width) / 2) '// set the X coordinates.
  15.        pLocation.Y = (Me.Top + (Me.Height - Form_to_Center.Height) / 2) '// set the Y coordinates.
  16.        Return pLocation '// return the Location to the Form it was called from.
  17.    End Function
  18.  
  19. #End Region

Form secundario:
Código
  1.    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.        Me.Location = Form1.centerForm(Me, Me.Location)
  3.    End Sub
  4.