C# 動態menu(ToolStripMenuItem)增加選項和指定事件
C# 動態menu(ToolStripMenuItem)增加選項和指定事件
MW_menuStrip.Items.Clear();//清空原本的選單
ToolStripMenuItem[] subItem = new ToolStripMenuItem[2];//改成陣列模式,為了擴充做準備 by jash.liao at 2016/09/20
//主選單01
subItem[0] = AddContextMenu(Language.m_StrWM_menu00,-1, MW_menuStrip.Items, null);
//主選單01的子選單
AddContextMenu(Language.m_StrWM_menu01, 0, subItem[0].DropDownItems, new EventHandler(MenuClicked));
AddContextMenu(Language.m_StrWM_menu02, 1, subItem[0].DropDownItems, new EventHandler(MenuClicked));
AddContextMenu(Language.m_StrWM_menu03, 2, subItem[0].DropDownItems, new EventHandler(MenuClicked));
AddContextMenu(Language.m_StrWM_menu04, 3, subItem[0].DropDownItems, new EventHandler(MenuClicked));
////////
private ToolStripMenuItem AddContextMenu(string text, int Imindex, ToolStripItemCollection cms, EventHandler callback)//利用程式增加Menubar選單
{
if (text == “-“)
{
ToolStripSeparator tsp = new ToolStripSeparator();
cms.Add(tsp);
return null;
}
else if (!string.IsNullOrEmpty(text))
{
ToolStripMenuItem tsmi = new ToolStripMenuItem(text);
if (Imindex >= 0)
{
tsmi.Image = MW_imageList1.Images[Imindex];
}
if (callback != null) tsmi.Click += callback;
cms.Add(tsmi);
return tsmi;
}
return null;
}
public void MenuClicked(object sender, EventArgs e)//Menubar選單回應函數
{
String StrBuf;
StrBuf = ((ToolStripMenuItem)sender).Text;
if (m_SettingWnd != null)
{
m_SettingWnd.Close();
m_SettingWnd = null;
}
if (m_ImportWnd != null)
{
m_ImportWnd.Close();
m_ImportWnd = null;
}
if (m_EditWnd != null)
{
m_EditWnd.Close();
m_EditWnd = null;
}
if (Language.m_StrWM_menu01 == StrBuf)//設定
{
m_SettingWnd = new SettingWnd(this);
m_SettingWnd.WindowState = FormWindowState.Normal;
m_SettingWnd.MdiParent = this;
m_SettingWnd.Show();
m_SettingWnd.Focus();
}
else if (Language.m_StrWM_menu02 == StrBuf)//下載
{
//SQLite.SQLite_clearDB();//先清空DB
m_CS_PHP.m_StrDomain = “http://” + m_SettingFile.m_StrServerIP + “/”;//設定連線資訊
m_CS_PHP.m_StrUsername = m_SettingFile.m_StrAccount;
m_CS_PHP.m_StrPassword = m_SettingFile.m_StrPassword;
Animation.createThreadAnimation(Language.m_StrImport_Msg01, Animation.Animation_Wait_GetData);//下載檔案
if (MainWnd.m_CS_PHP.m_StrResponse == “0”)//可以正常抓到資料
{
m_EditWnd = new EditWnd(this);
m_EditWnd.WindowState = FormWindowState.Maximized;
m_EditWnd.MdiParent = this;
m_EditWnd.Show();
m_EditWnd.Focus();
}
else//不能正常抓到資料
{
m_SettingWnd = new SettingWnd(this);
m_SettingWnd.WindowState = FormWindowState.Normal;
m_SettingWnd.MdiParent = this;
m_SettingWnd.Show();
m_SettingWnd.Focus();
}
}
else if (Language.m_StrWM_menu03 == StrBuf)//編輯
{
m_EditWnd = new EditWnd(this);
m_EditWnd.WindowState = FormWindowState.Maximized;
m_EditWnd.MdiParent = this;
m_EditWnd.Show();
m_EditWnd.Focus();
}
else if (Language.m_StrWM_menu04 == StrBuf)//上傳
{
}
}