C#中关于集合的一个小例子
2007-07-15 13:06
555 查看
这是一个在c#中使用集合的一个小小的例子,里面涉及到了 Hashtable,ArrayList,集合的排序,枚举器,集合的遍历,成员的查找。
例子如下:
Form1代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string rInfo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int rInfo2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hashtable hTable = new Hashtable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int flag = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(5, new animal(5, "red bull"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(6, new animal(6, "blue lion"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(4, new animal(4, "green fly"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(2, new animal(2, "yellow dog"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(3, new animal(3, "white horse"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(1, new animal(1, "black pig"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (Exception Ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show (Ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//用ArrayList对键进行排序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ArrayList aList = new ArrayList(hTable.Keys);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aList.Sort();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//遍历器打印排序后的结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IDictionaryEnumerator iDicEnumerator = hTable.GetEnumerator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Text.StringBuilder sb=new System.Text.StringBuilder();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (iDicEnumerator.MoveNext())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
animal oAnimal = (animal)hTable[iDicEnumerator.Key];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sb.AppendLine (oAnimal.code + " " + oAnimal.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show(sb.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//查找动物名字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form2 oF2 = new Form2(this, "0");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oF2.ShowDialog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (int sCode in aList)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
animal oAnimal = (animal)hTable[sCode];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (oAnimal.name == rInfo)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("查找结果:代码为" + oAnimal.code );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (flag == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("没有这个动物!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//查找动物代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form2 oF3= new Form2(this, "1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oF3.ShowDialog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (hTable.ContainsKey(rInfo2))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
animal s = (animal)hTable[rInfo2];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("查找结果:动物名字为" + s.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("没有找到这样的动物!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class animal
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int code;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public animal(int Code, string Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.code = Code;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = Name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
form2的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form2 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form1 kk;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string rFlag="";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form2(Form1 k,string flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kk = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rFlag = flag;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (rFlag == "1")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
textBox1.ReadOnly = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
textBox1.Text = "1";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.textBox1.Text.Trim() != "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (rFlag == "0")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kk.rInfo = this.textBox1.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kk.rInfo2 = Convert.ToInt32(this.textBox1.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void textBox1_MouseDoubleClick(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int liu = Convert.ToInt32(this.textBox1.Text) + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(liu>6)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.textBox1.Text="1";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.textBox1.Text = liu.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
例子如下:
Form1代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string rInfo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int rInfo2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hashtable hTable = new Hashtable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int flag = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(5, new animal(5, "red bull"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(6, new animal(6, "blue lion"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(4, new animal(4, "green fly"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(2, new animal(2, "yellow dog"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(3, new animal(3, "white horse"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hTable.Add(1, new animal(1, "black pig"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (Exception Ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show (Ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//用ArrayList对键进行排序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ArrayList aList = new ArrayList(hTable.Keys);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aList.Sort();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//遍历器打印排序后的结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IDictionaryEnumerator iDicEnumerator = hTable.GetEnumerator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Text.StringBuilder sb=new System.Text.StringBuilder();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (iDicEnumerator.MoveNext())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
animal oAnimal = (animal)hTable[iDicEnumerator.Key];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sb.AppendLine (oAnimal.code + " " + oAnimal.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show(sb.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//查找动物名字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form2 oF2 = new Form2(this, "0");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oF2.ShowDialog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (int sCode in aList)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
animal oAnimal = (animal)hTable[sCode];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (oAnimal.name == rInfo)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("查找结果:代码为" + oAnimal.code );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (flag == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("没有这个动物!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//查找动物代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form2 oF3= new Form2(this, "1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oF3.ShowDialog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (hTable.ContainsKey(rInfo2))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
animal s = (animal)hTable[rInfo2];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("查找结果:动物名字为" + s.name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("没有找到这样的动物!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class animal
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int code;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public animal(int Code, string Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.code = Code;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = Name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
form2的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form2 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Form1 kk;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string rFlag="";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form2(Form1 k,string flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kk = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rFlag = flag;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (rFlag == "1")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
textBox1.ReadOnly = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
textBox1.Text = "1";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.textBox1.Text.Trim() != "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (rFlag == "0")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kk.rInfo = this.textBox1.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kk.rInfo2 = Convert.ToInt32(this.textBox1.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void textBox1_MouseDoubleClick(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int liu = Convert.ToInt32(this.textBox1.Text) + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(liu>6)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.textBox1.Text="1";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.textBox1.Text = liu.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 收藏的一个关于C# ToString的方法集合
- asp.net2.0(c#)关于画图的一个例子;
- C#中关于在List集合类中使用泛型的例子
- 收藏的一个关于C# ToString的方法集合
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C# 关于反射类[System.Reflection]的一个小例子
- 关于C#2.0泛型应用的一个小例子
- 关于C#与Access的一个例子[转]
- C#中关于委托练习的一个例子
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C#中关于委托练习的一个例子
- 一个非常有用的关于集合的例子,实现集合的add,remove和this[index]方法
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。
- java关于new出一个对象的例子
- 关于ExpandableListView用法的一个简单小例子
- 一个例子帮你搞懂C#语言高级特性系列(02) --- 委托、事件和Lambda表达式
- 照着网上做了一个小例子关于spring mvc
- 关于同步、异步、阻塞、非阻塞一个通俗易懂的例子
- c#一个分页控件的例子