índice de vb.net

Ajustar la posición y tamaño de los controles automáticamente con Anchor
(código en vb)


Actualizado: 01/Abr/2001




'------------------------------------------------------------------------------
' Prueba de controles con Anchor                                    (01/Abr/01)
' Para que al redimensionar el form, los controles se acomoden al nuevo tamaño
'
' Las asignaciones son:
' Para que se ajuste arriba y a la izquierda:   Top, Left 
' Para que se ajuste arriba y a la derecha:     Top, Right
' Para que se ajuste al ancho del form:         Top, Left, Right
' Para que se ajuste al ancho y alto:           Right, Bottom
' 
'
' Autor: Guillermo 'guille' Som
' Fecha: 01/Abr/2001
'------------------------------------------------------------------------------
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form
    '

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        '
        ' Asignar los valores de Anchor en tiempo de ejecución.
        ' Aunque es recomendable hacerlo en tiempo de diseño,
        ' ya que así también se ajusta el tamaño y posición de los controles
        ' en tiempo de diseño.
        '
        ' txtFileName se ajustará al ancho del formulario
        Me.txtFileName.Anchor = AnchorStyles.Top BitOr AnchorStyles.Left BitOr AnchorStyles.Right
        ' btnBrowse y btnOpen se posicionarán a la derecha del formulario,
        ' pero no cambiarán de tamaño
        Me.btnBrowse.Anchor = AnchorStyles.Top BitOr AnchorStyles.Right
        Me.btnOpen.Anchor = AnchorStyles.Top BitOr AnchorStyles.Right
        ' btnExit también permanecerá a la derecha del formulario,
        ' pero se ajustará en la parte inferior del mismo.
        Me.btnExit.Anchor = AnchorStyles.Bottom BitOr AnchorStyles.Right
        ' lstLines se ajustará al tamaño del formulario,
        ' pero permanecerá en la posición relativa con respecto a 
        ' los controles que tiene arriba y a su derecha.
        Me.lstLines.Anchor = AnchorStyles.Top BitOr AnchorStyles.Left BitOr AnchorStyles.Right BitOr AnchorStyles.Bottom
        '
        ' Añadirle un scroll horizontal al ListBox
        Me.lstLines.HorizontalScrollbar = True
        '
        Me.txtFileName.Text = ""
    End Sub

    'Form overrides dispose to clean up the component list.
    Public Overrides Sub Dispose()
        MyBase.Dispose()
        If Not (components Is Nothing) Then
            components.Dispose()
        End If
    End Sub
    Private WithEvents txtFileName As System.Windows.Forms.TextBox
    Private WithEvents label1 As System.Windows.Forms.Label
    Private WithEvents btnBrowse As System.Windows.Forms.Button
    Private WithEvents btnExit As System.Windows.Forms.Button
    Private WithEvents lstLines As System.Windows.Forms.ListBox
    Private WithEvents openFD As System.Windows.Forms.OpenFileDialog
    Private WithEvents btnOpen As System.Windows.Forms.Button
    Private WithEvents btnOpen As System.Windows.Forms.Button

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lstLines = New System.Windows.Forms.ListBox()
        Me.openFD = New System.Windows.Forms.OpenFileDialog()
        Me.btnOpen = New System.Windows.Forms.Button()
        Me.btnExit = New System.Windows.Forms.Button()
        Me.label1 = New System.Windows.Forms.Label()
        Me.btnBrowse = New System.Windows.Forms.Button()
        Me.txtFileName = New System.Windows.Forms.TextBox()
        Me.lstLines.Anchor = (((System.Windows.Forms.AnchorStyles.Top BitOr System.Windows.Forms.AnchorStyles.Bottom) _
            BitOr System.Windows.Forms.AnchorStyles.Left) _
            BitOr System.Windows.Forms.AnchorStyles.Right)
        Me.lstLines.Font = New System.Drawing.Font("Courier New", 8)
        Me.lstLines.HorizontalScrollbar = True
        Me.lstLines.ItemHeight = 14
        Me.lstLines.Location = New System.Drawing.Point(12, 44)
        Me.lstLines.Size = New System.Drawing.Size(400, 186)
        Me.lstLines.TabIndex = 3
        Me.btnOpen.Anchor = (System.Windows.Forms.AnchorStyles.Top BitOr System.Windows.Forms.AnchorStyles.Right)
        Me.btnOpen.Location = New System.Drawing.Point(420, 48)
        Me.btnOpen.TabIndex = 5
        Me.btnOpen.Text = "Abrir"
        Me.btnExit.Anchor = (System.Windows.Forms.AnchorStyles.Bottom BitOr System.Windows.Forms.AnchorStyles.Right)
        Me.btnExit.Location = New System.Drawing.Point(420, 240)
        Me.btnExit.TabIndex = 4
        Me.btnExit.Text = "Salir"
        Me.label1.Location = New System.Drawing.Point(12, 20)
        Me.label1.Size = New System.Drawing.Size(48, 16)
        Me.label1.TabIndex = 0
        Me.label1.Text = "Fichero:"
        Me.btnBrowse.Anchor = (System.Windows.Forms.AnchorStyles.Top BitOr System.Windows.Forms.AnchorStyles.Right)
        Me.btnBrowse.Location = New System.Drawing.Point(420, 12)
        Me.btnBrowse.TabIndex = 2
        Me.btnBrowse.Text = "Examinar..."
        Me.txtFileName.Anchor = ((System.Windows.Forms.AnchorStyles.Top BitOr System.Windows.Forms.AnchorStyles.Left) _
            BitOr System.Windows.Forms.AnchorStyles.Right)
        Me.txtFileName.Location = New System.Drawing.Point(64, 16)
        Me.txtFileName.Size = New System.Drawing.Size(348, 20)
        Me.txtFileName.TabIndex = 1
        Me.txtFileName.Text = "textBox1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(507, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnOpen, Me.btnExit, Me.lstLines, Me.btnBrowse, Me.txtFileName, Me.label1})
        Me.MinimumSize = New System.Drawing.Size(300, 200)
        Me.Text = "Form1"

    End Sub
    '
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' Cerrar el formulario
        Me.Close()
    End Sub
    '
    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Me.txtFileName.Text = ""
        ' Seleccionar un fichero para mostrar
        With Me.openFD
            .Filter = "Todos los ficheros (*.*)|*.*|Ficheros de texto (*.txt)|*.txt"
            If .ShowDialog() = Me.DialogResult.OK Then
                Me.txtFileName.Text = .FileName
            End If
        End With
    End Sub
    '
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        ' Abrir el fichero indicado en txtFileName
        Dim FileName As String = Me.txtFileName.Text
        ' Comprobar si existe
        If File.Exists(FileName) Then
            Me.lstLines.Items.Clear()
            ' leer el contenido del fichero y asignarlo al ListBox
            Dim sr As StreamReader = New StreamReader(FileName, System.Text.Encoding.Default)
            Do While Not sr.Peek = -1
                lstLines().Items.Add(sr.ReadLine())
            Loop
            sr.Close()
        End If
    End Sub
#End Region

End Class

Volver al artículo

Ir al índice de vb.net

la Luna del Guille o... el Guille que está en la Luna... tanto monta...