基本上; Form1有2個按鈕,Form2有1個按鈕。 當您單擊Form 2的按鈕時,它將檢查您單擊的Form 1上的哪個按鈕,根據您單擊的按鈕(在Form 1上)打開Form 3或Form 4。在表單之間傳遞值;精煉
所以我利用了Mark Halls第一種在表單之間傳遞變量的方法。現在我的封閉式改進的後半部分。
Form1中
private void btnLogin_Click(object sender, EventArgs e)
{
// Call function while storing variable info.
Account("login");
}
private void btnRegister_Click(object sender, EventArgs e)
{
// Call function while storing variable info.
Account("register");
}
// Function used to pass Variable info to Account form while opening it as instance.
private void Account(string formtype)
{
// Generate/Name new instant of form.
frontend_account frmAcc = new frontend_account();
// Pass variable to instance.
frmAcc.CheckButtonClick = formtype;
// Show form instance.
frmAcc.Show(this);
// Hide this instance.
this.Hide();
}
窗體2
// String Variable to store value from Login.
public string CheckButtonClick { get; set; }
private void btnContinue_Click(object sender, EventArgs e)
{
// If statement to open either Main form or Registration form, based on Login variable.
if (CheckButtonClick == "login")
{
// Generate/Name new instant of form.
frontend_main frmMain = new frontend_main();
// Show form instant.
frmMain.Show();
// Close this instant.
this.Close();
}
else if (CheckButtonClick == "register")
{
// Generate/Name new instant of form.
frontend_register frmReg = new frontend_register();
// Show form instant.
frmReg.Show();
// Close this instant.
this.Close();
}
}
Form 2上有TWO單選按鈕,我可以ADEPT該代碼被打開的形式何時設置一個選項卡控制的焦點?即。如果選中了radClient,請在打開winform後將焦點設置爲tabcontrol,否則,如果選中了radStudent,請在打開winform後將焦點設置在tabcontrol(其他頁面)上......並且如果沒有選中廣播,我想不打開一個winform。
我相信這會成爲焦點;
// Sets focus to first tab.
tabRegister.SelectedTab = tabRegister.TabPages[0];
// Sets focus to second tab.
tabRegister.SelectedTab = tabRegister.TabPages[1];
看看這個帖子的方式來傳遞形式之間的消息如出一轍:http://stackoverflow.com/questions/12976205/best-way-to-handle-passing- of-control-checked-state-between-forms/12977226#12977226 –