在表单之间传递值;精制 [英] Passing values between forms; Refining

查看:65
本文介绍了在表单之间传递值;精制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Basically; Form1 has 2 buttons, Form2 has 1 button. When you click Form2's button it checks which button on Form1 you clicked, opening Form3 or Form4 depending on which button you clicked (on Form1).

因此,我利用了Mark Halls在表格之间传递变量的第一种方法.现在到我的封闭式优化的下半年.

So I've utilized Mark Halls first method of passing variables between forms. Now for the second half of my closed refinement.

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

Form2

// 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.

On Form2 there are TWO radio buttons, can I adept that code to set the focus of a tab control when a form is opened? ie. if radClient is checked set focus on tabcontrol after opening winform, else if radStudent is checked set focus on tabcontrol (other page) after opening winform... and i guess don't open a winform if no radio is checked.

我相信这将成为重点;

I believe this will set the focus;

// Sets focus to first tab.
tabRegister.SelectedTab = tabRegister.TabPages[0];
// Sets focus to second tab.
tabRegister.SelectedTab = tabRegister.TabPages[1];

推荐答案

在您的示例中,我看到的第一个问题是您正在关闭父窗体,该父窗体关闭了Form1并处理了Form2,我要做的是改为隐藏Form1关闭它,然后我将在Form2上创建一个公共属性,以传递所选的Button.但是,无论何时打开和关闭多个窗体,它都会变得混乱,我要做的是为其他窗体创建UserControls并在面板中换出它们.第一个示例是如何按照您的要求进行操作.

In your example the first problem I see is you are closing your parent form which closes your Form1 and disposes of Your Form2, What I would do is Hide Form1 instead of Closing it, I would then create a public property on Form2 to pass in the Button that was selected. But anytime you are opening and closing multiple Forms it can get messy, what I would do would be to create UserControls for your additional Forms and swap them out in a Panel. The first example is how to do it the way that you asked.

表格

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.  

    }
}

Form2

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很久以来就被这些表格所遗忘,所以我搜索它以打开备份

Form3 and Form4 note since Form1 is long forgotten to these forms I search for it to Open back up

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的第二个选项具有一个带有面板的窗体.它使用事件向Form发出更改控件的信号,并在UserControl2上添加一个公共属性

The second Option with UserControls has one Form with a Panel on it. It uses events to signal the Form to Change Controls plus a public property on 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();
    }
}

UserControl1

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

UserControl2

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 完全相同,只是类名不同

UserControl3 & UserControl4 exactly the same except for different Class Name

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

这篇关于在表单之间传递值;精制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆