Ajustar la posición y tamaño de los controles automáticamente con Anchor
(código en c#)
Actualizado: 01/Abr/2001
/******************************************************************************
* Prueba de aplicación realizada con c#
*
* Autor: Guillermo 'guille' Som
* Fecha: 02/Abr/2001
*
*****************************************************************************/
namespace csWinApp1
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.TextBox txtFileName;
private System.Windows.Forms.ListBox lstLines;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.OpenFileDialog openFD;
private System.Windows.Forms.Button btnOpen;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.txtFileName.Text="";
// Añadir scroll horizontal al ListBox
this.lstLines.HorizontalScrollbar=true;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
public override void Dispose()
{
base.Dispose();
if(components != null)
components.Dispose();
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lstLines = new System.Windows.Forms.ListBox();
this.openFD = new System.Windows.Forms.OpenFileDialog();
this.btnOpen = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.btnBrowse = new System.Windows.Forms.Button();
this.txtFileName = new System.Windows.Forms.TextBox();
this.lstLines.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.lstLines.Font = new System.Drawing.Font("Courier New", 8F);
this.lstLines.ItemHeight = 14;
this.lstLines.Location = new System.Drawing.Point(12, 44);
this.lstLines.Size = new System.Drawing.Size(396, 214);
this.lstLines.TabIndex = 3;
this.btnOpen.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnOpen.Location = new System.Drawing.Point(416, 44);
this.btnOpen.TabIndex = 5;
this.btnOpen.Text = "Abrir";
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
this.btnExit.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.btnExit.Location = new System.Drawing.Point(416, 236);
this.btnExit.Size = new System.Drawing.Size(75, 24);
this.btnExit.TabIndex = 4;
this.btnExit.Text = "Salir";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Size = new System.Drawing.Size(60, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Fichero:";
this.btnBrowse.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnBrowse.Location = new System.Drawing.Point(416, 8);
this.btnBrowse.Size = new System.Drawing.Size(75, 24);
this.btnBrowse.TabIndex = 2;
this.btnBrowse.Text = "Examinar...";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
this.txtFileName.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.txtFileName.Location = new System.Drawing.Point(80, 12);
this.txtFileName.Size = new System.Drawing.Size(328, 20);
this.txtFileName.TabIndex = 1;
this.txtFileName.Text = "textBox1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(503, 276);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnOpen,
this.btnExit,
this.lstLines,
this.btnBrowse,
this.txtFileName,
this.label1});
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnExit_Click(object sender, System.EventArgs e)
{
// Terminar el programa
this.Close();
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
// Seleccionar el fichero a abrir
openFD.Filter = "Todos los ficheros (*.*)|*.*|Ficheros de texto (*.txt)|*.txt";
if(openFD.ShowDialog() == DialogResult.OK)
this.txtFileName.Text = openFD.FileName;
}
private void btnOpen_Click(object sender, System.EventArgs e)
{
// Abrir el fichero indicado en al caja de texto
String FileName = txtFileName.Text;
// Comprobar si existe
if(System.IO.File.Exists(FileName))
{
this.lstLines.Items.Clear();
System.IO.StreamReader sr = new System.IO.StreamReader(FileName,System.Text.Encoding.Default);
while(!(sr.Peek()==-1))
{
this.lstLines.Items.Add(sr.ReadLine());
}
sr.Close();
}
}
}
}