Arrays Dinamicos

Fecha: 09/Abr/2005 (08-04-05)
Autor: Ricardo Macrino  rmacrino@yahoo.com

 


Como Crear y redimensionar una Array con C#

En este sencillo ejemplo vemos como llenar un listbox con valores ingresados desde teclado a la vez que se llena un array (redimensionado Dinamicamente Muestra un form con dos listbox un text de input y un boton de mostrar se ingresan los valores que contendra el array en el textbox se van mostrando los valores ingresados en la lista 1 el array se redimensiona dinamicamente en el evento keypress del objeto textbox al presionar el boton en un loop se muestra el interior del Array en orden inverso en la lista 2

/// ..................................................................................................................................................................................
/// ejemplo de Array Redimensionado Dinamicamnete
///  Ricardo Macrino
/// Buenos Aires Argentina Abril del 2005
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
 
namespace array {
      /// <summary>
      /// Descripción breve de Form1.
      /// </summary> 
      public class Form1 : System.Windows.Forms.Form { 
            private System.Windows.Forms.ListBox listBox1; 
            private System.Windows.Forms.TextBox textBox1; 
            public string[] Arreglito; 
            private System.Windows.Forms.Label nada; 
            private System.Windows.Forms.Label label1; 
            private System.Windows.Forms.ListBox listBox2; 
            private System.Windows.Forms.Button button1; 
            /// <summary>
            /// Variable del diseñador requerida.
            /// </summary> 
            private System.ComponentModel.Container components = null; 
 
            public Form1() {
                  //
                  // Necesario para admitir el Diseñador de Windows Forms
                  //
                  InitializeComponent();
 
                  //
                  // TODO: agregar código de constructor después de llamar a InitializeComponent
                  //
            }
 
            /// <summary>
            /// Limpiar los recursos que se estén utilizando.
            /// </summary> 
            protected override void Dispose( bool disposing ) { 
                  if( disposing ) { 
                        if (components != null) { 
                             components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }
 
            #region Código generado por el Diseñador de Windows Forms
            /// <summary>
            /// Método necesario para admitir el Diseñador. No se puede modificar
            /// el contenido del método con el editor de código.
            /// </summary> 
            private void InitializeComponent() { 
                  this.listBox1 = new System.Windows.Forms.ListBox(); 
                  this.textBox1 = new System.Windows.Forms.TextBox(); 
                  this.nada = new System.Windows.Forms.Label(); 
                  this.label1 = new System.Windows.Forms.Label(); 
                  this.listBox2 = new System.Windows.Forms.ListBox(); 
                  this.button1 = new System.Windows.Forms.Button(); 
                  this.SuspendLayout(); 
                  // 
                  // listBox1 
                  // 
                  this.listBox1.Location = new System.Drawing.Point(24, 32); 
                  this.listBox1.Name = "listBox1"; 
                  this.listBox1.Size = new System.Drawing.Size(184, 199); 
                  this.listBox1.TabIndex = 0; 
                  // 
                  // textBox1 
                  // 
                  this.textBox1.Location = new System.Drawing.Point(248, 32); 
                  this.textBox1.Name = "textBox1"; 
                  this.textBox1.Size = new System.Drawing.Size(184, 20); 
                  this.textBox1.TabIndex = 1; 
                  this.textBox1.Text = ""; 
                  this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); 
                  // 
                  // nada 
                  // 
                  this.nada.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
                  this.nada.ForeColor = System.Drawing.Color.Brown; 
                  this.nada.Location = new System.Drawing.Point(240, 104); 
                  this.nada.Name = "nada"; 
                  this.nada.Size = new System.Drawing.Size(80, 23); 
                  this.nada.TabIndex = 2; 
                  // 
                  // label1 
                  // 
                  this.label1.Location = new System.Drawing.Point(240, 80); 
                  this.label1.Name = "label1"; 
                  this.label1.TabIndex = 3; 
                  this.label1.Text = "Dimensiones "; 
                  // 
                  // listBox2 
                  // 
                  this.listBox2.BackColor = System.Drawing.SystemColors.InactiveCaptionText; 
                  this.listBox2.ForeColor = System.Drawing.Color.Sienna; 
                  this.listBox2.Location = new System.Drawing.Point(32, 264); 
                  this.listBox2.Name = "listBox2"; 
                  this.listBox2.Size = new System.Drawing.Size(480, 134); 
                  this.listBox2.TabIndex = 4; 
                  // 
                  // button1 
                  // 
                  this.button1.Location = new System.Drawing.Point(248, 200); 
                  this.button1.Name = "button1"; 
                  this.button1.Size = new System.Drawing.Size(120, 23); 
                  this.button1.TabIndex = 5; 
                  this.button1.Text = "Mostar Arreglo"; 
                  this.button1.Click += new System.EventHandler(this.button1_Click); 
                  // 
                  // Form1 
                  // 
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
                  this.ClientSize = new System.Drawing.Size(536, 430); 
                  this.Controls.Add(this.button1); 
                  this.Controls.Add(this.listBox2); 
                  this.Controls.Add(this.label1); 
                  this.Controls.Add(this.nada); 
                  this.Controls.Add(this.textBox1); 
                  this.Controls.Add(this.listBox1); 
                  this.Name = "Form1"; 
                  this.Text = "Form1"; 
                  this.Load += new System.EventHandler(this.Form1_Load); 
                  this.ResumeLayout(false); 
 
            } 
            #endregion 
 
            /// <summary> 
            /// Punto de entrada principal de la aplicación.
            /// </summary> 
            [STAThread] 
            static void Main() { 
                  Application.Run(new Form1()); 
            } 
 
 
            private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { 
                  if(e.KeyChar == (char)13) { 
                        listBox1.Items.Add(textBox1.Text);     
                        Arreglito= (string []) aRedimensionar(Arreglito,Arreglito.Length+1);
                        Arreglito[Arreglito.Length-2]=textBox1.Text;
                        textBox1.Text="";
                        nada.Text= Arreglito.Length.ToString();
                  }
            } 
 
            
            private void Form1_Load(object sender, System.EventArgs e) { 
                  Arreglito =null; 
                  Arreglito=new string[1]; 
            } 
 
            public static Array aRedimensionar(Array orgArray, Int32 tamaño) { 
                  Type t = orgArray.GetType().GetElementType(); 
                  Array nArray = Array.CreateInstance(t, tamaño); 
                  Array.Copy(orgArray, 0, nArray, 0, Math.Min(orgArray.Length, tamaño)); 
                  return nArray; 
            } 
 
            private void button1_Click(object sender, System.EventArgs e) { 
                  listBox2.Items.Add("Arreglo"); 
                  listBox2.Items.Add("|"); 
                  for(int y=Arreglito.Length-1;y>0;y--) { 
                        if(Arreglito[y]!=null)
                             listBox2.Items.Add("|__"+Arreglito[y]+" ["+y.ToString()+"]");          
                        }
                  }
            }
      }
/// .........................................................................................

Cortar y pegar en un Proyecto nuevo del visual Studio Por la linea Punteada

hay dos puntos de interes, uno es el manejo en el evento Keypress del objeto textbox1

El otro es la funcioncita publica estatica de redimensionado del array



ir al índice