.
這方法就跟它的名稱一樣,叫做字典,主要功能就是可以對其加入索引(index)跟值(value),之後只要輸入該索引就可以取得想要的內容,以下範例為取得檔案中的內容存至List中,接著再分別塞入dictionary。事先就先定義好每個索引的列舉,然後在讀入檔案的內容會去跟列舉做比對,如果有與事先定義的不同就會顯示錯誤,可以在編譯階段就找出問題,降低在run time時產生錯誤的危險性。
最後就是列出每個讀入的項目為一個清單,點選個別清單會取得相對應的內容。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Dictionary
{
public partialclass Form1 : Form
{
privateList<point_name.name> _action = new List<point_name.name>();
string[]_action_name = new string[100];
point_name_point_name = new point_name();
Dictionary<point_name.name,point_info> _dictionary =
newDictionary<point_name.name, point_info>();
int_action_count = 0;
int _c =0;
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
{
get_point_file();
get_action_file();
this.comboBox1.Items.Clear();
for (int i = 0; i < _c; i++)
{
this.comboBox1.Items.Add(newdropdownList(_action_name[i], i));
}
this.comboBox1.SelectedIndex = 0;
}
privatevoid get_point_file()
{
point_infopi = new point_info();
stringline;
string[]data = { };
char[]split = {'\t'};
stringstr = System.Windows.Forms.Application.StartupPath;
stringfile_name = str + "\\action_point.txt";
FileStream file_WR = newFileStream(file_name, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
System.IO.StreamReaderfile = new System.IO.StreamReader(file_WR);
while((line = file.ReadLine()) != null)
{
data= line.Split(split);
pi= new point_info();
pi.type= data[1];
pi.reference= data[2];
pi.x= Int32.Parse(data[3]);
pi.y= Int32.Parse(data[4]);
pi.z= Int32.Parse(data[5]);
pi.explanation= data[6];
try
{
_dictionary.Add((point_name.name)Enum.Parse(typeof(point_name.name), data[0]), pi);
}
catch
{
MessageBox.Show("indexerror1");
return;
}
}
file.Close();
file_WR.Close();
}
privatevoid get_action_file()
{
stringline;
string[]data = { };
char[]split = { '\t' };
stringstr = System.Windows.Forms.Application.StartupPath;
stringfile_name = str + "\\action_mode.txt";
FileStream file_WR = newFileStream(file_name, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
System.IO.StreamReaderfile = new System.IO.StreamReader(file_WR);
while((line = file.ReadLine()) != null)
{
data= line.Split(split);
_action_name[_c]= data[0];
for(inti=1 ; i<data.Length-1 ; i++)
{
try
{
_action.Add(((point_name.name)Enum.Parse(typeof(point_name.name), data[i])));
}
catch
{
MessageBox.Show("indexerror2");
return;
}
}
_c++;
}
_action_count= data.Length - 1;
file.Close();
file_WR.Close();
}
privatevoid button1_Click(object sender, EventArgs e)
{
richTextBox1.Text="";
dropdownListddL = (dropdownList)this.comboBox1.SelectedItem;
intselect_index;
for(int i = 0; i < _action_count; i++)
{
if(ddL.value == i)
{
select_index= i * _action_count;
for(intj=1 ; j <_action_count;j++)
{
richTextBox1.Text+= "《m " +_action_name[i] + " 》n"
+_dictionary[_action[j + select_index]].type + " "
+_dictionary[_action[j + select_index]].reference + " "
+_dictionary[_action[j + select_index]].x + " "
+_dictionary[_action[j + select_index]].y + " "
+_dictionary[_action[j + select_index]].z + " "
+_dictionary[_action[j + select_index]].explanation + " "
+"\n";
}
}
}
}
}
}
讀取的檔案
執行結果