Winform starts another method for project value passing
- 2020-11-20 06:13:05
- OfStack
The example in this article shows how Winform starts another project to pass values. Share to everybody for everybody reference. The details are as follows:
Background: After logging in from the A project, jump to a page on the B project (B is no longer logged in).
A Project startup process:
public Form1()
{
InitializeComponent();
}
#region The calling process
[DllImport("Shell32.dll")]
private static extern int ShellExecute(
IntPtr hwnd,
string lpOperation, // For more "open"
string lpFile, // The file name
string lpParameters, // parameter
string lpDirectory, // The file path
int nShowCmd
);
/// <summary>
/// Load the appropriate application
/// </summary>
private void StartApplication(string projname, string arg)
{
ShellExecute(IntPtr.Zero, "Open", projname, arg, Application.StartupPath + @"\", 1);
}
#endregion
private void btnJump_Click(object sender, EventArgs e)
{
StartApplication("B", "Doctor,00045,14092701");// Jump from here
}
In B project:
/// <summary>
/// The main entry point to the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length>0)
{
string[] strArr = args[0].ToString().Split(new char[] { ','});
Application.Run(new MainForm(strArr[0], strArr[1], strArr[2]));
}
else
{
Application.Run(new MainForm());
}
}
Remark:
Where B item Main method parameter string[] args, can only receive args[0], the 1 string string, rather than the entire array. So when the A project passes the value, it passes string(separated by a comma).
2. Overload Application. Run(new MainForm()) to pass the three parameters: strArr[0], strArr[1], strArr[2].
3. Attribute value transfer method:
public MainForm(string _module,string _userID,string _patientID)
{
InitializeComponent();
module = _module;
userID = _userID;
patientID = _patientID;
}
private string userID="";
public string UserID
{
get { return userID; }
set { userID = value; }
}
I hope this article has been helpful for your C# programming.