我正在开发一个项目,我必须在c#windows窗体应用程序的表单加载中运行时从数据库创建菜单项,我已创建菜单但现在问题是我必须在菜单中显示图标数据库,我搜查了实习生,但没有任何合适的东西,所以如果有人知道请指导我如何做到这一点。提前谢谢
private void toolstripform_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
StatusStrip sst = new StatusStrip();
this.Controls.Add(toolStrip2);
this.Controls.Add(toolStrip1);
string slctcmd = string.Format("SELECT TOP 3 [MAINMNU],[MENUPARVAL] FROM [Sales_db].[dbo].[MNU_PARENT]");
DataTable dt = qc.DataReaderTable(slctcmd);
string slctcmd1 = string.Format("SELECT * FROM MNU_PARENT where MENUPARVAL = '4' or MENUPARVAL = '5' or MENUPARVAL = '6' or MENUPARVAL = '7'");
DataTable dt1 = qc.DataReaderTable(slctcmd1);
foreach (DataRow dr in dt.Rows)
{
ToolStripMenuItem MnuStripItem = new ToolStripMenuItem(dr["MAINMNU"].ToString());
SubMenu(MnuStripItem, dr["MENUPARVAL"].ToString());
toolStrip1.Items.Add(MnuStripItem);
}
foreach (DataRow dr in dt1.Rows)
{
ToolStripMenuItem topmenu = new ToolStripMenuItem(dr["MAINMNU"].ToString());
toolStrip2.Items.Add(topmenu);
}
}
public void SubMenu(ToolStripMenuItem mnu, string submenu)
{
string slctcmd = string.Format("SELECT ID,FRM_NAME FROM MNU_SUBMENU WHERE MENUPARVAL='" + submenu + "'");
DataTable dt = qc.DataReaderTable(slctcmd);
foreach (DataRow dr in dt.Rows)
{
ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr["FRM_NAME"].ToString(), null, new EventHandler(ChildClick));
Sub_SubMenu(SSMenu, dr["ID"].ToString());
mnu.DropDownItems.Add(SSMenu);
}
}
public void Sub_SubMenu(ToolStripMenuItem S_Sub_Menu, string s_s_menu)
{
string slctcmd = string.Format("SELECT Listitems FROM SubMenu WHERE SubMenuID='" + s_s_menu + "'");
DataTable dt = qc.DataReaderTable(slctcmd);
foreach (DataRow dr in dt.Rows)
{
ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr["Listitems"].ToString());
S_Sub_Menu.DropDownItems.Add(SSMenu);
}
}
private void ChildClick(object sender, EventArgs e)
{
string slctcmd = string.Format("SELECT FRM_CODE FROM MNU_SUBMENU WHERE FRM_NAME= '" + sender.ToString() + "'");
DataTable dtransaction = qc.DataReaderTable(slctcmd);
Assembly frmAssembly = Assembly.LoadFile(Application.ExecutablePath);
foreach (Type type in frmAssembly.GetTypes())
{
if (type.BaseType == typeof(Form))
{
if (type.Name == dtransaction.Rows[0][0].ToString())
{
Form frmShow = (Form)frmAssembly.CreateInstance(type.ToString());
foreach (Form form in this.MdiChildren)
{
form.Close();
}
frmShow.MdiParent = this;
frmShow.StartPosition = FormStartPosition.CenterScreen;
frmShow.Show();
}
}
}
}