📄 hashtableexample.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace HashtableExample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class HashtableExample : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblKey;
private System.Windows.Forms.Label lblValue;
private System.Windows.Forms.TextBox txtKey;
private System.Windows.Forms.TextBox txtValue;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.TextBox txtSelectedValue;
private System.Windows.Forms.Label lblAddedKey;
private System.Windows.Forms.Label lblSeletedValue;
private System.Windows.Forms.GroupBox grpAddKey;
private System.Windows.Forms.ListBox lstAddedKey;
private Hashtable _hashtable = new Hashtable();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public HashtableExample()
{
//
// 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.lblKey = new System.Windows.Forms.Label();
this.lblValue = new System.Windows.Forms.Label();
this.txtKey = new System.Windows.Forms.TextBox();
this.txtValue = new System.Windows.Forms.TextBox();
this.btnAdd = new System.Windows.Forms.Button();
this.txtSelectedValue = new System.Windows.Forms.TextBox();
this.lblAddedKey = new System.Windows.Forms.Label();
this.lblSeletedValue = new System.Windows.Forms.Label();
this.grpAddKey = new System.Windows.Forms.GroupBox();
this.lstAddedKey = new System.Windows.Forms.ListBox();
this.grpAddKey.SuspendLayout();
this.SuspendLayout();
//
// lblKey
//
this.lblKey.Location = new System.Drawing.Point(8, 16);
this.lblKey.Name = "lblKey";
this.lblKey.TabIndex = 0;
this.lblKey.Text = "键:";
this.lblKey.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// lblValue
//
this.lblValue.Location = new System.Drawing.Point(8, 48);
this.lblValue.Name = "lblValue";
this.lblValue.TabIndex = 1;
this.lblValue.Text = "值:";
this.lblValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtKey
//
this.txtKey.Location = new System.Drawing.Point(112, 16);
this.txtKey.Name = "txtKey";
this.txtKey.Size = new System.Drawing.Size(240, 21);
this.txtKey.TabIndex = 2;
this.txtKey.Text = "";
//
// txtValue
//
this.txtValue.Location = new System.Drawing.Point(112, 48);
this.txtValue.Name = "txtValue";
this.txtValue.Size = new System.Drawing.Size(240, 21);
this.txtValue.TabIndex = 3;
this.txtValue.Text = "";
//
// btnAdd
//
this.btnAdd.Font = new System.Drawing.Font("Tahoma", 8F);
this.btnAdd.Location = new System.Drawing.Point(112, 72);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 21);
this.btnAdd.TabIndex = 4;
this.btnAdd.Text = "添加";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// txtSelectedValue
//
this.txtSelectedValue.Location = new System.Drawing.Point(112, 288);
this.txtSelectedValue.Name = "txtSelectedValue";
this.txtSelectedValue.Size = new System.Drawing.Size(240, 21);
this.txtSelectedValue.TabIndex = 6;
this.txtSelectedValue.Text = "";
//
// lblAddedKey
//
this.lblAddedKey.Location = new System.Drawing.Point(0, 112);
this.lblAddedKey.Name = "lblAddedKey";
this.lblAddedKey.TabIndex = 7;
this.lblAddedKey.Text = "已添加键:";
this.lblAddedKey.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// lblSeletedValue
//
this.lblSeletedValue.Location = new System.Drawing.Point(8, 288);
this.lblSeletedValue.Name = "lblSeletedValue";
this.lblSeletedValue.TabIndex = 8;
this.lblSeletedValue.Text = "选中键的值:";
this.lblSeletedValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// grpAddKey
//
this.grpAddKey.Controls.Add(this.txtValue);
this.grpAddKey.Controls.Add(this.lblKey);
this.grpAddKey.Controls.Add(this.txtKey);
this.grpAddKey.Controls.Add(this.lblValue);
this.grpAddKey.Controls.Add(this.btnAdd);
this.grpAddKey.Dock = System.Windows.Forms.DockStyle.Top;
this.grpAddKey.Location = new System.Drawing.Point(0, 0);
this.grpAddKey.Name = "grpAddKey";
this.grpAddKey.Size = new System.Drawing.Size(368, 100);
this.grpAddKey.TabIndex = 9;
this.grpAddKey.TabStop = false;
this.grpAddKey.Text = "建立键值";
//
// lstAddedKey
//
this.lstAddedKey.ItemHeight = 12;
this.lstAddedKey.Location = new System.Drawing.Point(112, 112);
this.lstAddedKey.Name = "lstAddedKey";
this.lstAddedKey.Size = new System.Drawing.Size(240, 160);
this.lstAddedKey.TabIndex = 10;
this.lstAddedKey.SelectedIndexChanged += new System.EventHandler(this.lstAddedKey_SelectedIndexChanged);
//
// HashtableExample
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 333);
this.Controls.Add(this.lstAddedKey);
this.Controls.Add(this.grpAddKey);
this.Controls.Add(this.lblSeletedValue);
this.Controls.Add(this.lblAddedKey);
this.Controls.Add(this.txtSelectedValue);
this.Name = "HashtableExample";
this.Text = "HashtableExample";
this.grpAddKey.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new HashtableExample());
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
_hashtable.Add(txtKey.Text, txtValue.Text);
}
catch(ArgumentException)
{
MessageBox.Show("不能添加重复的键"
,"错误"
,System.Windows.Forms.MessageBoxButtons.OK
,System.Windows.Forms.MessageBoxIcon.Information); //有重复的键值
return; //给用户提示,插入动作终止
}
catch(Exception)
{
MessageBox.Show("产生系统错误。"); //产生位置错误,
Application.Exit(); //则关闭应用程序
}
lstAddedKey.Items.Add(txtKey.Text);
}
private void lstAddedKey_SelectedIndexChanged(object sender, System.EventArgs e)
{
string selectedKey = lstAddedKey.SelectedValue.ToString();
string selectedValue = (string)_hashtable[selectedKey];
txtSelectedValue.Text = selectedValue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -