📄 frmarrrylistexample.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ArrayListExample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmArrarListExample : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.TextBox txtPersonName;
private System.Windows.Forms.ListBox lstMessage;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.TextBox txtSearchString;
private ArrayList _arrPerson = new ArrayList();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmArrarListExample()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnAdd = new System.Windows.Forms.Button();
this.txtPersonName = new System.Windows.Forms.TextBox();
this.lstMessage = new System.Windows.Forms.ListBox();
this.btnSearch = new System.Windows.Forms.Button();
this.txtSearchString = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnAdd
//
this.btnAdd.Font = new System.Drawing.Font("Tahoma", 8F);
this.btnAdd.Location = new System.Drawing.Point(168, 16);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(100, 21);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "增加姓名(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// txtPersonName
//
this.txtPersonName.Location = new System.Drawing.Point(8, 16);
this.txtPersonName.Name = "txtPersonName";
this.txtPersonName.Size = new System.Drawing.Size(160, 21);
this.txtPersonName.TabIndex = 1;
this.txtPersonName.Text = "";
//
// lstMessage
//
this.lstMessage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstMessage.ItemHeight = 12;
this.lstMessage.Location = new System.Drawing.Point(8, 40);
this.lstMessage.Name = "lstMessage";
this.lstMessage.Size = new System.Drawing.Size(456, 220);
this.lstMessage.TabIndex = 2;
//
// btnSearch
//
this.btnSearch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnSearch.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnSearch.Location = new System.Drawing.Point(368, 264);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(96, 21);
this.btnSearch.TabIndex = 3;
this.btnSearch.Text = "查找元素(&S)";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// txtSearchString
//
this.txtSearchString.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtSearchString.Location = new System.Drawing.Point(208, 264);
this.txtSearchString.Name = "txtSearchString";
this.txtSearchString.Size = new System.Drawing.Size(160, 21);
this.txtSearchString.TabIndex = 4;
this.txtSearchString.Text = "";
//
// frmArrarListExample
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 301);
this.Controls.Add(this.txtSearchString);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.lstMessage);
this.Controls.Add(this.txtPersonName);
this.Controls.Add(this.btnAdd);
this.Name = "frmArrarListExample";
this.Text = "数组演示";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmArrarListExample());
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
_arrPerson.Add(this.txtPersonName.Text);
lstMessage.Items.Clear();
lstMessage.Items.Add("数组的容量是:" + this._arrPerson.Capacity.ToString());
lstMessage.Items.Add("数组的元素个数是:" + this._arrPerson.Count.ToString());
lstMessage.Items.Add("----------------------------------------------------------------");
foreach(string personName in _arrPerson)
{
lstMessage.Items.Add("元素:" + personName);
}
lstMessage.Items.Add("----------------------------------------------------------------");
_arrPerson.TrimToSize();
lstMessage.Items.Add("整理后的数组容量是:" + _arrPerson.Capacity.ToString());
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
if(_arrPerson.Contains(txtSearchString.Text))
{
MessageBox.Show("存在这个元素。");
}
else
{
MessageBox.Show("不存在这个元素。");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -