当前位置:网站首页>WinForm forms use assembly instantiation and parameter transfer
WinForm forms use assembly instantiation and parameter transfer
2022-07-21 16:09:00 【I Can -Jiang】
It is similar to encapsulating functions instantiated from a form , It's really just a C# Self contained function , When I used it, I checked it online for a long time .
This function works well in combination with the database , That is, there is no need to instantiate the form repeatedly , Improve the simplicity of code .
I'm a little cumbersome here , Because I want the login interface not to display NarBarControl, I made another layer
This article mainly talks about NarBarControl and XtraTabControl and Assembly The combination of instantiated forms
NarBarControl Automatically add... In combination with the database Group and Item The example of has been mentioned earlier , But here's a small change , Here is the Item Of URL Replaced with the name of the form . The purpose of this is to combine Assembly Instantiate the form
database structure :
RibbonControl When switching focus pages, give NarBarControl add group
private void RBControl_SelectedPageChanged(object sender, EventArgs e)
{
if (RBControl.SelectedPage != null && RBControl.SelectedPage.Name.ToString().Trim() != "")
{
Frm_Split = Frm_Split.CreateFrom(this);
Frm_Split.TopLevel = false;
Frm_Split.Parent = Panel;
Frm_Split.Dock = DockStyle.Fill;
Frm_Split.Show();
page = RBControl.SelectedPage.Name.ToString().Trim();
Frm_Split.Menu.Groups.Clear();
DetailReportGroup(page, Frm_Split);
}
}
there Frm_Split It's actually NarBarControl and XtraTabControl Form , Added a SplitContainerControl Control implementation of NarBarControl Left contraction of .
Loop dynamic add NarBarControl Of Group and Item
// Loop add group
private void DetailReportGroup(string pageName, Frm_Split frm)
{
string sql = "select isAdmin from UserInf where userID='" + lbl_userID.Caption.Trim().ToLower() + "' and isUse='Y'";
DataTable dt;
if (!DB.AF.execSql(sql, sqlconnks, out dt))
{
MessageBox.Show(" Error connecting to server , Please check whether the network is normal !");
return ;
}
if (dt.Rows.Count == 0)
{
MessageBox.Show(" The user account does not exist or has been deactivated , Please enter the correct user !");
return ;
}
if (dt.Rows[0]["isAdmin"].ToString().Trim() == "Y")
{
sql = "select MenuID,MenuName from Sys_Menu where Flag = 'Group' and Isuse = 'Y' and Page = '" + pageName + "'";
}
else sql = "select a.MenuID MenuID,b.MenuName MenuName from sys_Menu_User a left join sys_Menu b on a.MenuID = b.MenuID where Flag = 'Group' and a.Isuse = 'Y' and b.Isuse = 'Y' and Page = '" + pageName + "' and userID = '" + lbl_userID.Caption.Trim() + "'";
dt.Clear();
if (!DB.AF.execSql(sql, sqlconn, out dt))
{
MessageBox.Show(" Failed to query menu group !");
return;
}
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
NavBarGroup nbGroup = new NavBarGroup();
nbGroup.Name = dt.Rows[i]["MenuID"].ToString();
nbGroup.Caption = dt.Rows[i]["MenuName"].ToString();
frm.Menu.Groups.Add(nbGroup);
DetailReportItem(nbGroup, pageName);// This call Item Function of
}
}
}
// Add... To the group loop Item
private void DetailReportItem(NavBarGroup GroupName, string pageName)
{
string sql = "select isAdmin from UserInf where userID='" + lbl_userID.Caption.Trim().ToLower() + "' and isUse='Y'";
DataTable dt;
if (!DB.AF.execSql(sql, sqlconnks, out dt))
{
MessageBox.Show(" Error connecting to server , Please check whether the network is normal !");
return;
}
if (dt.Rows.Count == 0)
{
MessageBox.Show(" The user account does not exist or has been deactivated , Please enter the correct user !");
return;
}
if (dt.Rows[0]["isAdmin"].ToString().Trim() == "Y")
{
sql = "select URL,MenuName from Sys_Menu where Flag = 'Item' and upID = '" + GroupName.Name + "' and Isuse = 'Y' and Page = '" + pageName + "'";
}
else sql = "select b.URL URL,b.MenuName MenuName from sys_Menu_User a left join sys_Menu b on a.MenuID = b.MenuID where Flag = 'Item' and upID = '" + GroupName.Name + "' and a.Isuse = 'Y' and b.Isuse = 'Y' and Page = '" + pageName + "' and userID = '"+lbl_userID.Caption.Trim()+"'";
dt.Clear();
if (!DB.AF.execSql(sql, sqlconn, out dt))
{
MessageBox.Show(" Query menu failed !");
return;
}
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
NavBarItem nbItem = new NavBarItem();
nbItem.Name = dt.Rows[i]["URL"].ToString();
nbItem.Caption = dt.Rows[i]["MenuName"].ToString();
nbItem.LinkClicked += Item_Click;
GroupName.ItemLinks.Add(nbItem);
}
}
}
I also covered a small part of the account menu permissions , Just look Admin Just the part of .
Next is Item Click events and Assembly The instantiated part of the form :
#region Form instantiation
private void Item_Click(object sender, NavBarLinkEventArgs e)
{
FormName = MidStrEx(e.Link.Item.Name,"_","_");
if (FormName.Length > 0)
{
bool bzj = false;
if (Frm_Split.tabs.Controls.Count != 0)
{
foreach (XtraTabPage page in Frm_Split.tabs.TabPages)
{
if (page.Name == e.Link.Item.Name)
{
bzj = true;
Frm_Split.tabs.SelectedTabPage = page;
return;
}
}
}
if (bzj == false)
{
XtraTabPage tabFrmLog = new XtraTabPage();
tabFrmLog.Text = e.Link.Caption;
tabFrmLog.Name = e.Link.Item.Name;
// Initialize the login interface
CreateForm(namespaceName + "." + FormName + "." + e.Link.Item.Name, namespaceName, tabFrmLog, Frm_Split, lbl_userID.Caption.Trim(), btn_UserName.Caption.Trim());
// Set the subform size to tab size
Frm_Split.tabs.TabPages.Add(tabFrmLog);
Frm_Split.tabs.SelectedTabPage = tabFrmLog;
}
}
}
/// <summary>
/// Open a new subform
/// </summary>
/// <param name="strName"> The class name of the form </param>
/// <param name="AssemblyName"> The name of the class library where the form is located </param>
public static void CreateForm(string strName, string AssemblyName , XtraTabPage tabFrmLog,Frm_Split Frm_Split,string userID,string UserName)
{
string path = AssemblyName;// Project Assembly Option name
string name = strName; // The name of the class
//XtraMessageBox.Show(path + ";" + name);
object[] args = new object[3];
args[0] = Frm_Split;
args[1] = userID;
args[2] = UserName;
Form doc = (Form)Assembly.Load(path).CreateInstance(name, true, System.Reflection.BindingFlags.Default, null, args, null, null);
doc.TopLevel = false;
doc.Dock = DockStyle.Fill;
doc.FormBorderStyle = FormBorderStyle.None;
tabFrmLog.Controls.Add(doc);
doc.Show();
}
public static string MidStrEx(string sourse, string startstr, string endstr)
{
string result = string.Empty;
int startindex, endindex;
try
{
startindex = sourse.IndexOf(startstr);
if (startindex == -1)
return result;
string tmpstr = sourse.Substring(startindex + startstr.Length);
endindex = tmpstr.IndexOf(endstr);
if (endindex == -1)
return result;
result = tmpstr.Remove(endindex);
}
catch (Exception ex)
{
XtraMessageBox.Show("MidStrEx Err:" + ex.Message);
}
return result;
}
I need to use MidStrEx() The reason for the function is because I put the form under the folder , If you put it directly under the project, you don't need this function
边栏推荐
- 2022 Shandong vision prevention and control conference, China eye care products exhibition, Jinan myopia correction equipment exhibition
- c || 文件操作
- In the e-commerce industry with fierce competition, why can the chain 2+1 mode survive for so long?
- CentOS 7上集群化部署Apache Druid 0.22实时分析数据库
- 2022 eye care lamp exhibition, traditional Chinese medicine eye conditioning exhibition, Shandong eye health exhibition, China eye Expo
- In the financial scenario, how to accurately select and quickly implement distributed databases?
- Three structures of C language
- 深度解析:链动2+1模式是如何玩转招商引流的
- 死锁
- In depth analysis: how chain 2+1 mode plays with investment attraction and drainage
猜你喜欢
[JS] dynamic rendering page
How can traditional retail enterprises promote consumption cycle and share purchase mode to enable the development of physical enterprises?
What aspects should we start from to learn the silver K-line chart
c || 文件操作
C | array and pointer
Wechat vaccine appointment applet graduation design (5) assignment book of applet completion work
TransUNet: Transformers Make Strong Encoders for Medical Image Segmentation
【STC15控制WS2812 RGB彩灯级联】
Training of head and neck segmentation networks with shape prior on small datasets
Blueprism tool menu bar user rights function description -rpa Chapter 2
随机推荐
Five reasons why developers use Klocwork for software security
2022第五届中国(济南)国际青少年眼健康产业展览会9月举办
目前最火的测试框架,pytest封神级讲解
[JS] mouse follow
專注跨境支付一體化服務 iPayLinks榮獲《財資》(The Asset)3A亞洲獎!
What if the download of Duoyu security browser is blocked? Turn off the method of download blocking
I2C client driver
手机股票开户有没有风险的?安全靠谱吗?
C | file operation
This domestic API artifact tool is too strong Let me give up postman
Wechat vaccine appointment applet graduation design of applet completion work (2) applet function
Solve the sudden jump out of the command line (flashing)
Three structures of C language
i2c adapter驱动
【微信小程序】switch开关选择器(81/100)
这款国产API神器工具也太强了吧...让我放弃了postman
[Yugong series] go teaching course in July 2022 014 arithmetic operators of operators
i2c client驱动之IIO设备驱动
Projection & local illumination
每日一题-LeetCode1260-二维网格迁移-数组-映射