许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

将对象存入HashTable,然后读取

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace chapter11_DEMO
{
    
struct Employee
    
{
        
public string name;
        
public int         age;
        
public string address;
        
public decimal salary;
    }

    
/// <summary>
    
/// EmployeeManager 的摘要说明。
    
/// </summary>

    public class EmployeeManager : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.ListBox lstID;
        
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 txtName;
        
private System.Windows.Forms.TextBox txtAge;
        
private System.Windows.Forms.TextBox txtAddress;
        
private System.Windows.Forms.TextBox txtSalary;
        
private System.Windows.Forms.Button btnInital;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public EmployeeManager()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if(components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码
        
private Hashtable ht=new Hashtable();//

        
private void btnInital_Click(object sender, System.EventArgs e)
        
{
            Employee[] em
=new Employee[2];
            em[
0].name="xumh";
            em[
0].age=30;
            em[
0].address="yantai";
            em[
0].salary=10000.98m;

            em[
1].name="xuzh";
            em[
1].age=2;
            em[
1].address="yantai";
            em[
1].salary=-1000m;
            ht.Add(
"001",em[0]);
            ht.Add(
"002",em[1]);
            
//
            lstID.Items.Add("001");
            lstID.Items.Add(
"002");
        }


        
private void lstID_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
string strKey=lstID.SelectedItem.ToString();
            Employee em
=(Employee)ht[strKey];
            txtName.Text 
= em.name;
            txtAddress.Text
=em.address;
            txtAge.Text 
= em.age.ToString();
            txtSalary.Text 
=em.salary.ToString();
        }

    }

}

posted on   许明会  阅读(280)  评论(0编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示