這篇文章主要介紹了Winform啟動(dòng)另一個(gè)項(xiàng)目傳值的方法,通過調(diào)用進(jìn)程來實(shí)現(xiàn)項(xiàng)目之間的傳值,需要的朋友可以參考下
本文實(shí)例講述了Winform啟動(dòng)另一個(gè)項(xiàng)目傳值的方法。分享給大家供大家參考。具體如下:
背景:從A項(xiàng)目中登陸后,跳轉(zhuǎn)到B項(xiàng)目的某個(gè)頁面(B不再登陸)。
A項(xiàng)目啟動(dòng)進(jìn)程:
復(fù)制代碼代碼如下:
public Form1()
{
InitializeComponent();
}
#region 調(diào)用進(jìn)程
[DllImport("Shell32.dll")]
private static extern int ShellExecute(
IntPtr hwnd,
string lpOperation, //多為"open"
string lpFile, //文件名稱
string lpParameters, //參數(shù)
string lpDirectory, //文件路徑
int nShowCmd
);
/// <summary>
/// 加載相應(yīng)的應(yīng)用程序
/// </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");//從這里跳轉(zhuǎn)
}
B項(xiàng)目中:
復(fù)制代碼代碼如下:
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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項(xiàng)目Main方法的參數(shù) string[] args,只能接收args[0],這一個(gè)string串,而不是整個(gè)數(shù)組。所以A項(xiàng)目傳值的時(shí)候,傳遞的是string(使用逗號(hào),來分割)。
2. 重載方法Application.Run(new MainForm())來傳遞這三個(gè)參數(shù):strArr[0], strArr[1], strArr[2]。
3.屬性傳值方法:
復(fù)制代碼代碼如下:
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; }
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。