如何通过读取文件创建多级MenuStrip?
How to create multiple-level MenuStrip by reading from a file?
文本文件:
1;Laptop
2;Dell;1
3;Asus;1
4;Dell Vostro;2
5;RAM
6;RAM Kington;5
7;RAM Kingmax;5
8;RAM Kington 3GB;6
9;RAM Kingmax 2GB;7
这是我的代码:
//Class item
class Item
{
public string Id { get; set; }
public string Name { get; set; }
public string Parent { get; set; }
}
//Read file from file1.txt
static List<Item> GetItems()
{
string[] lines = File.ReadAllLines("Item.txt");
List<Item> list = new List<Item>();
foreach (string line in lines)
{
string[] strItem = line.Split(';');
Item item = new Item
{
Name = strItem[1],
Id = strItem[0],
Parent = strItem.Length > 2 ? strItem[2] : null
};
list.Add(item);
}
return list;
}
//Process input from file1.txt
private void Form1_Load(object sender, EventArgs e)
{
List<Item> list = GetItems();
foreach (Item item in list)
{
if (item.Parent is null)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Name);
msItem.Items.Add(menuItem);
foreach (Item item2 in list)
{
if (item2.Parent == item.Id)
{
ToolStripMenuItem menuItem1 = new ToolStripMenuItem(item2.Name);
menuItem.DropDownItems.Add(menuItem1);
foreach (Item item3 in list)
{
if (item3.Parent == item2.Id)
{
ToolStripMenuItem menuItem2 = new ToolStripMenuItem(item3.Name);
menuItem1.DropDownItems.Add(menuItem2);
}
}
}
}
}
}
}
但是如果我想创建一个菜单,循环太多,里面有10层将是10个嵌套循环。
那么我该如何优化代码呢?
只需使用 Dictionary<string, ToolStripMenuitem>,这样您就可以在创建菜单时查找它们:
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<string, ToolStripMenuItem> menuLookup = new Dictionary<string, ToolStripMenuItem>();
List<Item> list = GetItems();
foreach (Item item in list)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Name);
if (!menuLookup.ContainsKey(item.Id))
{
menuLookup.Add(item.Id, menuItem);
}
if (item.Parent is null)
{
msItem.Items.Add(menuItem);
}
else if (menuLookup.ContainsKey(item.Parent))
{
menuLookup[item.Parent].DropDownItems.Add(menuItem);
}
}
}
文本文件:
1;Laptop
2;Dell;1
3;Asus;1
4;Dell Vostro;2
5;RAM
6;RAM Kington;5
7;RAM Kingmax;5
8;RAM Kington 3GB;6
9;RAM Kingmax 2GB;7
这是我的代码:
//Class item
class Item
{
public string Id { get; set; }
public string Name { get; set; }
public string Parent { get; set; }
}
//Read file from file1.txt
static List<Item> GetItems()
{
string[] lines = File.ReadAllLines("Item.txt");
List<Item> list = new List<Item>();
foreach (string line in lines)
{
string[] strItem = line.Split(';');
Item item = new Item
{
Name = strItem[1],
Id = strItem[0],
Parent = strItem.Length > 2 ? strItem[2] : null
};
list.Add(item);
}
return list;
}
//Process input from file1.txt
private void Form1_Load(object sender, EventArgs e)
{
List<Item> list = GetItems();
foreach (Item item in list)
{
if (item.Parent is null)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Name);
msItem.Items.Add(menuItem);
foreach (Item item2 in list)
{
if (item2.Parent == item.Id)
{
ToolStripMenuItem menuItem1 = new ToolStripMenuItem(item2.Name);
menuItem.DropDownItems.Add(menuItem1);
foreach (Item item3 in list)
{
if (item3.Parent == item2.Id)
{
ToolStripMenuItem menuItem2 = new ToolStripMenuItem(item3.Name);
menuItem1.DropDownItems.Add(menuItem2);
}
}
}
}
}
}
}
但是如果我想创建一个菜单,循环太多,里面有10层将是10个嵌套循环。
那么我该如何优化代码呢?
只需使用 Dictionary<string, ToolStripMenuitem>,这样您就可以在创建菜单时查找它们:
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<string, ToolStripMenuItem> menuLookup = new Dictionary<string, ToolStripMenuItem>();
List<Item> list = GetItems();
foreach (Item item in list)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Name);
if (!menuLookup.ContainsKey(item.Id))
{
menuLookup.Add(item.Id, menuItem);
}
if (item.Parent is null)
{
msItem.Items.Add(menuItem);
}
else if (menuLookup.ContainsKey(item.Parent))
{
menuLookup[item.Parent].DropDownItems.Add(menuItem);
}
}
}