0

基本上; Form1 有 2 个按钮,Form2 有 1 个按钮。当您单击 Form2 的按钮时,它会检查您单击了 Form1 上的哪个按钮,根据您单击的按钮(在 Form1 上)打开 Form3 或 Form4。


所以我使用了 Mark Halls 在表单之间传递变量的第一种方法。现在是我封闭改进的后半部分。

表格1

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

在 Form2 上有两个单选按钮,我可以熟练使用该代码在打开表单时设置选项卡控件的焦点吗?IE。如果选中 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];
4

2 回答 2

0

在您的示例中,我看到的第一个问题是您正在关闭您的父表单,该表单关闭您的 Form1 并处理您的 Form2,我要做的是隐藏 Form1 而不是关闭它,然后我将在 Form2 上创建一个公共属性以传入被选中的按钮。但是,无论何时您打开和关闭多个表单都会变得混乱,我会为您的其他表单创建用户控件并将它们换出面板。第一个示例是如何按照您要求的方式进行操作。

形式

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        ShowForm2("login");
    }

    private void btnRegister_Click(object sender, EventArgs e)
    {
        ShowForm2("register");
    } 


    private void ShowForm2(string formtype)
    {
        Form2 f2 = new Form2(); // Instantiate a Form2 object.
        f2.CheckButtonClick = formtype;
        f2.Show(this); // Show Form2 and  
        this.Hide(); // closes the Form1 instance.  

    }
}

表格2

ublic partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string CheckButtonClick { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        if (CheckButtonClick == "login")
        {
            Form3 f3 = new Form3(); // Instantiate a Form3 object.  
            f3.Show(); // Show Form3 and  
            this.Close(); // closes the Form2 instance.  
        }
        else if (CheckButtonClick == "register")
        {
            Form4 f4 = new Form4(); // Instantiate a Form4 object.  
            f4.Show(); // Show Form4 and  
            this.Close(); // closes the Form2 instance.  
        } 

    }
}

Form3 和 Form4请注意,因为 Form1 早就被这些表格遗忘了,我搜索它以打开备份

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    private void Form3_FormClosed(object sender, FormClosedEventArgs e)
    {
        FormCollection frms = Application.OpenForms;
        foreach (Form f in frms)
        {
            if (f.Name == "Form1")
            {
                f.Show();
                break;
            }
        }
    }
}

UserControls 的第二个选项有一个带有面板的表单。它使用事件来通知表单以更改控件以及 UserControl2 上的公共属性

public partial class Form1 : Form
{
    string logonType;
    public Form1()
    {
        InitializeComponent();
    }

    private void userControl1_LoginOrRegisterEvent(object sender, LoginOrRegisterArgs e)
    {
        logonType = e.Value;
        userControl2.BringToFront();
    }


    private void userControl2_ControlFinshedEvent(object sender, EventArgs e)
    {
        if (logonType == "logon")
            userControl3.BringToFront();
        else if (logonType == "register")
            userControl4.BringToFront();
    }

    private void userControl3_ControlFinshedEvent(object sender, EventArgs e)
    {
        userControl1.BringToFront();
    }

    private void userControl4_ControlFinshedEvent(object sender, EventArgs e)
    {
        userControl1.BringToFront();
    }
}

用户控件1

public partial class UserControl1 : UserControl
{
   public delegate void LoginOrRegisterHandler(object sender, LoginOrRegisterArgs e);
   public event LoginOrRegisterHandler LoginOrRegisterEvent;

    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LoginOrRegisterArgs ea = new LoginOrRegisterArgs("logon");
        LoginOrRegisterEvent(sender, ea);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        LoginOrRegisterArgs ea = new LoginOrRegisterArgs("register");
        LoginOrRegisterEvent(sender, ea);
    }
}

public class LoginOrRegisterArgs
{
    public LoginOrRegisterArgs(string s) {Value = s;}
    public string Value {get; private set;}
}

用户控件2

public partial class UserControl2 : UserControl
{
    public delegate void ControlFinishedHandler(object sender, EventArgs e);
    public event ControlFinishedHandler ControlFinshedEvent;
    public UserControl2()
    {
        InitializeComponent();
    }
    public string SetLogonType { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        ControlFinshedEvent(sender, new EventArgs());
    }
}

UserControl3 & UserControl4完全相同,只是类名不同

public partial class UserControl3 : UserControl
{
    public delegate void ControlFinishedHandler(object sender, EventArgs e);
    public event ControlFinishedHandler ControlFinshedEvent;
    public UserControl3()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ControlFinshedEvent(sender, new EventArgs());
    }
}
于 2012-10-20T03:38:53.957 回答
0

正如我在评论中建议的那样,我知道在表单之间传递数据的最好方法之一是使用事件。

基本上,在“子”表单中,您声明将由“主”表单处理或监听的事件。

请参阅我评论中的参考答案,如果您对如何调整它有具体问题,请提出。

干杯

于 2012-10-20T02:16:37.497 回答