Winform启动另一个项目传值的方法

本文实例讲述了Winform启动另一个项目传值的方法。分享给大家供大家参考。具体如下:

背景:从A项目中登陆后,跳转到B项目的某个页面(B不再登陆)。

A项目启动进程:


public Form1()
{
    InitializeComponent();
}
#region 调用进程
[DllImport("Shell32.dll")]
private static extern int ShellExecute(
     IntPtr hwnd,
     string lpOperation,      //多为"open"
     string lpFile,           //文件名称
     string lpParameters,   //参数
     string lpDirectory,      //文件路径
     int nShowCmd
     );
/// <summary>
/// 加载相应的应用程序
/// </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");//从这里跳转
}

B项目中:

/// <summary>
/// 应用程序的主入口点。
/// </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());
    }
}

备注:

1.其中B项目Main方法的参数 string[] args,只能接收args[0],这一个string串,而不是整个数组。所以A项目传值的时候,传递的是string(使用逗号,来分割)。

2. 重载方法Application.Run(new MainForm())来传递这三个参数:strArr[0], strArr[1], strArr[2]。

3.属性传值方法:


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; }
}

希望本文所述对大家的C#程序设计有所帮助。

若文章对您有帮助,帮忙点个赞!

0
-3
发布时间 2014-11-13 18:07:47
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)