本文整理汇总了C#中System.Windows.Forms.ListBox.Items属性的典型用法代码示例。如果您正苦于以下问题:C# ListBox.Items属性的具体用法?C# ListBox.Items怎么用?C# ListBox.Items使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.ListBox
的用法示例。
在下文中一共展示了ListBox.Items属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: button1_Click
private void button1_Click(object sender, System.EventArgs e)
{
// Create an instance of the ListBox.
ListBox listBox1 = new ListBox();
// Set the size and location of the ListBox.
listBox1.Size = new System.Drawing.Size(200, 100);
listBox1.Location = new System.Drawing.Point(10,10);
// Add the ListBox to the form.
this.Controls.Add(listBox1);
// Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = true;
// Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended;
// Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate();
// Loop through and add 50 items to the ListBox.
for (int x = 1; x <= 50; x++)
{
listBox1.Items.Add("Item " + x.ToString());
}
// Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate();
// Select three items from the ListBox.
listBox1.SetSelected(1, true);
listBox1.SetSelected(3, true);
listBox1.SetSelected(5, true);
// Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
// Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());
}
示例2: Form1
//引入命名空间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.ListBox displayListBox;
private System.Windows.Forms.TextBox inputTextBox;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button removeButton;
private System.Windows.Forms.Button clearButton;
public Form1() {
InitializeComponent();
}
private void addButton_Click( object sender, EventArgs e )
{
displayListBox.Items.Add( inputTextBox.Text );
inputTextBox.Clear();
}
private void removeButton_Click( object sender, EventArgs e )
{
if ( displayListBox.SelectedIndex != -1 )
displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
}
private void clearButton_Click( object sender, EventArgs e )
{
displayListBox.Items.Clear();
}
private void InitializeComponent()
{
this.displayListBox = new System.Windows.Forms.ListBox();
this.inputTextBox = new System.Windows.Forms.TextBox();
this.addButton = new System.Windows.Forms.Button();
this.removeButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// displayListBox
//
this.displayListBox.FormattingEnabled = true;
this.displayListBox.Location = new System.Drawing.Point(13, 12);
this.displayListBox.Name = "displayListBox";
this.displayListBox.Size = new System.Drawing.Size(119, 238);
this.displayListBox.TabIndex = 0;
//
// inputTextBox
//
this.inputTextBox.Location = new System.Drawing.Point(149, 12);
this.inputTextBox.Name = "inputTextBox";
this.inputTextBox.Size = new System.Drawing.Size(100, 20);
this.inputTextBox.TabIndex = 1;
//
// addButton
//
this.addButton.Location = new System.Drawing.Point(149, 56);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(100, 36);
this.addButton.TabIndex = 2;
this.addButton.Text = "Add";
this.addButton.Click += new System.EventHandler(this.addButton_Click);
//
// removeButton
//
this.removeButton.Location = new System.Drawing.Point(149, 109);
this.removeButton.Name = "removeButton";
this.removeButton.Size = new System.Drawing.Size(100, 36);
this.removeButton.TabIndex = 3;
this.removeButton.Text = "Remove";
this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
//
// clearButton
//
this.clearButton.Location = new System.Drawing.Point(149, 165);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(100, 36);
this.clearButton.TabIndex = 4;
this.clearButton.Text = "Clear";
this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
// ListBoxTestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(263, 268);
this.Controls.Add(this.clearButton);
this.Controls.Add(this.removeButton);
this.Controls.Add(this.addButton);
this.Controls.Add(this.inputTextBox);
this.Controls.Add(this.displayListBox);
this.Name = "ListBoxTestForm";
this.Text = "ListBoxTest";
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}