| |

VerySource

 Forgot password?
 Register
Search
View: 921|Reply: 5

Ask the form method dynamically.

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-17 18:30:01
| Show all posts |Read mode
One main form and N child forms
The main form opens the subform by calling the shwo subform name () method of the subform, but the main form has a lot of menu items, and the code for each call is almost the same. See if it can be simplified. I first give my code:

Main window call code (almost every call is written like this):
//Permission settings
private void MSys_Power_Click (object sender, System.EventArgs e)
{
Form frm = null;
for (int i = 0; i <this.MdiChildren.Length; i ++)
{
if (this.MdiChildren [i] .Name == "RolePowerManage")
{
frm = this.MdiChildren [i];
break;
}
}
if (frm == null)
{
this.stBarMain.Panels [0] .Text = "Loading permission settings form ...";
this.Refresh ();
SystemSet.RolePowerManage.ShowRolePowerManage (this);
this.stBarMain.Panels [0] .Text = "Ready";
}
else
{
frm.BringToFront ();
frm.Activate ();
}
}
Child window ShowRolePowerManage () method:
public static void ShowRolePowerManage (Form FName)
{
Form frmTip = LoadTip.ShowTipForm (FName, "Loading form ...");
RolePowerManage frm = new RolePowerManage ();
frm.MdiParent = FName;
frm.Show ();
frmTip.Close ();
}
I have all the methods of child window display: show + window name

I was wondering if I could write the code called by the main window as a method. I knew that I would use reflection. Unfortunately, I ’m not good at learning, please help me.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Singapore

Post time: 2020-6-19 21:45:01
| Show all posts
Reflection will reduce efficiency, try not to use
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-6-22 17:30:02
| Show all posts
Type type = Type.GetType("The complete class name of the form (MyNameSpace.MyForm)");
// Create a form instance
Object obj = type.InvokeMember(null,
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
// display form
type.InvokeMember("Show",System.Reflection.BindingFlags.InvokeMethod,null,obj,null);
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-23 17:30:02
| Show all posts
You can try to use public class
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-23 16:15:01
| Show all posts
Type type = Type.GetType("The complete class name of the form (MyNameSpace.MyForm)");
Form objFrm = System.Activator.CreateInstance(type);
objFrm.show();

reflection
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-31 12:15:02
| Show all posts
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-31 12:30:01
| Show all posts
Sorry, I sent it wrong.

btx221This guy's method is good! !
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list