Código fuente
Option Explicit On
Option Strict On
Public Class Form1
Private Sub Centrar(ByVal Objeto As Object)
' Centrar un Formulario ...
If TypeOf Objeto Is Form Then
Dim frm As Form = CType(Objeto, Form)
With Screen.PrimaryScreen.WorkingArea ' Dimensiones de la pantalla sin el TaskBar
frm.Top = (.Height - frm.Height) \ 2
frm.Left = (.Width - frm.Width) \ 2
End With
' Centrar un control dentro del contenedor
Else
' referencia al control
Dim c As Control = CType(Objeto, Control)
'le establece el top y el Left dentro del Parent
With c
.Top = (.Parent.ClientSize.Height - c.Height) \ 2
.Left = (.Parent.ClientSize.Width - c.Width) \ 2
End With
End If
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Pasar formulario
Centrar(Me)
' Pasar control
Centrar(GroupBox1)
Centrar(GroupBox2)
Centrar(TextBox1)
End Sub
End Class
Option Strict On
Public Class Form1
Private Sub Centrar(ByVal Objeto As Object)
' Centrar un Formulario ...
If TypeOf Objeto Is Form Then
Dim frm As Form = CType(Objeto, Form)
With Screen.PrimaryScreen.WorkingArea ' Dimensiones de la pantalla sin el TaskBar
frm.Top = (.Height - frm.Height) \ 2
frm.Left = (.Width - frm.Width) \ 2
End With
' Centrar un control dentro del contenedor
Else
' referencia al control
Dim c As Control = CType(Objeto, Control)
'le establece el top y el Left dentro del Parent
With c
.Top = (.Parent.ClientSize.Height - c.Height) \ 2
.Left = (.Parent.ClientSize.Width - c.Width) \ 2
End With
End If
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Pasar formulario
Centrar(Me)
' Pasar control
Centrar(GroupBox1)
Centrar(GroupBox2)
Centrar(TextBox1)
End Sub
End Class
No hay comentarios:
Publicar un comentario