Practica de Informatica III: Uso de ListBox

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace UsingListBoxes { /// <summary> /// allows users to add, remove and clear list box items /// </summary> public class ListBoxTest : System.Windows.Forms.Form { // contains user-input list of elements private System.Windows.Forms.ListBox displayListBox; // user input textbox private System.Windows.Forms.TextBox inputTextBox; // add, remove, clear and exit command buttons private System.Windows.Forms.Button addButton; private System.Windows.Forms.Button removeButton;

// deafault constructor public ListBoxTest() { // // Required for Windows Form Designer support // InitializeComponent().Windows.SuspendLayout().Forms.Windows. this. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Button().Button exitButton. // // displayListBox // . /// </summary> private System. this. this. /// </summary> private void InitializeComponent() { this.displayListBox = new System. this.Forms.Button clearButton.Windows.Button().Container components = null.clearButton = new System.Button().addButton = new System.Forms.Windows.Windows.ListBox(). } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support .Forms.private System.Forms. this.TextBox().Forms. } /// <summary> /// Clean up any resources being used.inputTextBox = new System. /// <summary> /// Required designer variable.Dispose( disposing ).Forms. } } base.do not modify /// the contents of this method with the code editor.Windows. private System.Dispose().removeButton = new System.exitButton = new System.Windows.Button().Forms.ComponentModel.Windows. this.

addButton.clearButton.Drawing.displayListBox.FontStyle.Byte)(0))).displayListBox.Drawing.Drawing.addButton.Text = "Agregar". 48).addButton.Drawing. this. System. this. 48). 10F.TabIndex = 2.GraphicsUnit.Byte)(0))).removeButton.FontStyle.Location = new System.Location = new System.removeButton. this. this. this. this.Drawing.Drawing.Drawing.addButton_Click).Byte)(0))).clearButton.inputTextBox.Drawing.GraphicsUnit.Drawing.Click += new System. this. System. ((System. this. 290).Drawing.Regular. ((System.Point(16.Drawing. this.Location = new System.addButton.Name = "removeButton".Font = new System.Size(104.FontStyle.EventHandler(this.addButton.removeButton. this. System.Text = "". 48). 10F.EventHandler(this.inputTextBox.Point(168.Name = "clearButton".removeButton_Click).Size(104.clearButton.Name = "inputTextBox". this. 10F. ((System.Size(104.TabIndex = 1.addButton.Font = new System.Size = new System.Point(168. 176).inputTextBox.Drawing.GraphicsUnit. this.Point.Drawing. this. this.Font("Microsoft Sans Serif".removeButton.Drawing.displayListBox.Location = new System. // // inputTextBox // this.Point(168.this.addButton. System.Text = "Quitar". this.Font = new System.TabIndex = 0.Name = "addButton".Regular.Point(168. .Size(120.Font("Microsoft Sans Serif".Drawing. this.removeButton.Drawing. this.clearButton.Name = "displayListBox".Size = new System. System.Drawing.Drawing.Size = new System.Point. this. // // clearButton // this.Font("Microsoft Sans Serif".removeButton.Regular.displayListBox.removeButton. 16). this. this. // // addButton // this.Location = new System. // // removeButton // this.Click += new System. this. System.Size = new System.TabIndex = 3.inputTextBox. 16). 112). 48).Point.

Add(this. System. 48).Run( new ListBoxTest() ). this. this.Add(this. } #endregion /// <summary> /// The main entry point for the application.EventHandler(this. this.clearButton. 240).Text = "Salir".Font("Microsoft Sans Serif". 13).removeButton).Add(this. /// </summary> [STAThread] static void Main() { Application.inputTextBox).EventArgs e) { displayListBox. this. this.Controls.FontStyle.clearButton. this.EventHandler(this.addButton).Controls.this.Add(this. // // exitButton // this.exitButton.Point(168.GraphicsUnit.displayListBox).exitButton).exitButton. ((System.Click += new System.Drawing. this. this.Size(5.Point.Drawing.exitButton.Location = new System. this. this.Drawing.Drawing.Text = "Limpiar".exitButton. this.exitButton_Click).Size(292.Add(this.Controls.ResumeLayout(false). this.Add(this.Text = "ListBoxTest". this. this. System.Add( inputTextBox.Size(104.clearButton_Click).Controls.exitButton.exitButton. } // add new item (text from input box) // and clear input box private void addButton_Click( object sender.Byte)(0))).Drawing.Drawing.Name = "ListBoxTest". // // ListBoxTest // this.Items. 325).Name = "exitButton".clearButton.Regular.ClientSize = new System.Text ). this. .Drawing.Size = new System. 10F.exitButton.Click += new System. this. this. System.TabIndex = 5.TabIndex = 4.Font = new System.Controls.Controls.AutoScaleBaseSize = new System.clearButton). this.

EventArgs e ) { // remove only if item selected if ( displayListBox.inputTextBox. System.RemoveAt( displayListBox. System.Items.EventArgs e ) { Application. } // clear all items private void clearButton_Click( object sender.EventArgs e ) { displayListBox.SelectedIndex != -1 ) displayListBox. } } // end class ListBoxTest } . System.Clear().Clear().SelectedIndex ). } // exit application private void exitButton_Click( object sender.Items. } // remove item if one selected private void removeButton_Click( object sender.Exit().

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.