首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

HashTable有关问题,产生错误是什么原因呢

2011-12-15 
HashTable问题,产生异常是什么原因呢?usingSystemusingSystem.DrawingusingSystem.CollectionsusingSys

HashTable问题,产生异常是什么原因呢?
using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;

namespace   HashTable
{
///   <summary>
///   Form1   的摘要说明。
///   </summary>
public   class   Form1   :   System.Windows.Forms.Form
{
private   Hashtable   _hashtable=new   Hashtable();
private   System.Windows.Forms.GroupBox   groupBox1;
private   System.Windows.Forms.Label   label1;
private   System.Windows.Forms.Label   label2;
private   System.Windows.Forms.Label   label3;
private   System.Windows.Forms.Label   label4;
private   System.Windows.Forms.TextBox   txtKey;
private   System.Windows.Forms.TextBox   txtValue;
private   System.Windows.Forms.Button   btnAdd;
private   System.Windows.Forms.ListBox   lstAddedKey;
private   System.Windows.Forms.TextBox   txtSelectedValue;
///   <summary>
///   必需的设计器变量。
///   </summary>
private   System.ComponentModel.Container   components   =   null;

public   Form1()
{
//
//   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.groupBox1   =   new   System.Windows.Forms.GroupBox();
this.btnAdd   =   new   System.Windows.Forms.Button();
this.txtValue   =   new   System.Windows.Forms.TextBox();
this.txtKey   =   new   System.Windows.Forms.TextBox();
this.label2   =   new   System.Windows.Forms.Label();
this.label1   =   new   System.Windows.Forms.Label();
this.label3   =   new   System.Windows.Forms.Label();
this.lstAddedKey   =   new   System.Windows.Forms.ListBox();
this.label4   =   new   System.Windows.Forms.Label();
this.txtSelectedValue   =   new   System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//  
//   groupBox1
//  
this.groupBox1.Controls.Add(this.btnAdd);
this.groupBox1.Controls.Add(this.txtValue);
this.groupBox1.Controls.Add(this.txtKey);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location   =   new   System.Drawing.Point(8,   8);
this.groupBox1.Name   =   "groupBox1 ";
this.groupBox1.Size   =   new   System.Drawing.Size(280,   120);
this.groupBox1.TabIndex   =   0;
this.groupBox1.TabStop   =   false;


this.groupBox1.Text   =   "建立键值 ";
//  
//   btnAdd
//  
this.btnAdd.Location   =   new   System.Drawing.Point(96,   88);
this.btnAdd.Name   =   "btnAdd ";
this.btnAdd.TabIndex   =   4;
this.btnAdd.Text   =   "添加 ";
this.btnAdd.Click   +=   new   System.EventHandler(this.btnAdd_Click);
//  
//   txtValue
//  
this.txtValue.Location   =   new   System.Drawing.Point(96,   56);
this.txtValue.Name   =   "txtValue ";
this.txtValue.Size   =   new   System.Drawing.Size(176,   21);
this.txtValue.TabIndex   =   3;
this.txtValue.Text   =   " ";
//  
//   txtKey
//  
this.txtKey.Location   =   new   System.Drawing.Point(96,   24);
this.txtKey.Name   =   "txtKey ";
this.txtKey.Size   =   new   System.Drawing.Size(176,   21);
this.txtKey.TabIndex   =   2;
this.txtKey.Text   =   " ";
//  
//   label2
//  
this.label2.AutoSize   =   true;
this.label2.Location   =   new   System.Drawing.Point(16,   56);
this.label2.Name   =   "label2 ";
this.label2.Size   =   new   System.Drawing.Size(29,   17);
this.label2.TabIndex   =   1;
this.label2.Text   =   "值: ";
//  
//   label1
//  
this.label1.AutoSize   =   true;
this.label1.Location   =   new   System.Drawing.Point(16,   24);
this.label1.Name   =   "label1 ";
this.label1.Size   =   new   System.Drawing.Size(29,   17);
this.label1.TabIndex   =   0;
this.label1.Text   =   "键: ";
//  
//   label3
//  
this.label3.AutoSize   =   true;
this.label3.Location   =   new   System.Drawing.Point(8,   136);
this.label3.Name   =   "label3 ";
this.label3.Size   =   new   System.Drawing.Size(72,   17);
this.label3.TabIndex   =   1;
this.label3.Text   =   "已经添加值: ";
//  
//   lstAddedKey
//  
this.lstAddedKey.ItemHeight   =   12;
this.lstAddedKey.Location   =   new   System.Drawing.Point(96,   136);
this.lstAddedKey.Name   =   "lstAddedKey ";
this.lstAddedKey.Size   =   new   System.Drawing.Size(192,   112);
this.lstAddedKey.TabIndex   =   2;
this.lstAddedKey.SelectedValueChanged   +=   new   System.EventHandler(this.lstAddedKey_SelectedValueChanged);
this.lstAddedKey.SelectedIndexChanged   +=   new   System.EventHandler(this.lstAddedKey_SelectedIndexChanged);
//  
//   label4
//  
this.label4.AutoSize   =   true;
this.label4.Location   =   new   System.Drawing.Point(16,   264);
this.label4.Name   =   "label4 ";
this.label4.Size   =   new   System.Drawing.Size(60,   17);
this.label4.TabIndex   =   3;
this.label4.Text   =   "选中的值: ";
//  
//   txtSelectedValue


//  
this.txtSelectedValue.Location   =   new   System.Drawing.Point(96,   256);
this.txtSelectedValue.Name   =   "txtSelectedValue ";
this.txtSelectedValue.Size   =   new   System.Drawing.Size(192,   21);
this.txtSelectedValue.TabIndex   =   4;
this.txtSelectedValue.Text   =   " ";
//  
//   Form1
//  
this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);
this.ClientSize   =   new   System.Drawing.Size(292,   293);
this.Controls.Add(this.txtSelectedValue);
this.Controls.Add(this.label4);
this.Controls.Add(this.lstAddedKey);
this.Controls.Add(this.label3);
this.Controls.Add(this.groupBox1);
this.Name   =   "Form1 ";
this.Text   =   "HashTableExam ";
this.Load   +=   new   System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///   <summary>
///   应用程序的主入口点。
///   </summary>
[STAThread]
static   void   Main()  
{
Application.Run(new   Form1());
}

private   void   Form1_Load(object   sender,   System.EventArgs   e)
{

}

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);
//txtKey.Text= " ";
//txtValue.Text= " ";
}

private   void   lstAddedKey_SelectedIndexChanged(object   sender,   System.EventArgs   e)
{

}

private   void   lstAddedKey_SelectedValueChanged(object   sender,   System.EventArgs   e)
{
string   selectedKey=lstAddedKey.SelectedValue.ToString();
string   selectedValue=(string)_hashtable[selectedKey];
txtSelectedValue.Text=selectedValue;
}
}
}


[解决办法]
private void lstAddedKey_SelectedValueChanged(object sender, System.EventArgs e)
{
string selectedKey=lstAddedKey.SelectedItem.ToString(); //修改的地方
string selectedValue=(string)_hashtable[selectedKey];
txtSelectedValue.Text=selectedValue;
}
[解决办法]
报什么错误 哪行报错?
[解决办法]
namespace HashTable ----------------------??????改一下看看
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form

[解决办法]
同一楼

string selectedKey=lstAddedKey.SelectedValue.ToString();

改为

string selectedKey=lstAddedKey.SelectedItem.ToString();

热点排行