简体   繁体   English

从表格2获取数据到表格1并关闭

[英]Get data from Form 2 To Form1 And close

I have this class 我有这堂课

Account.cs Account.cs

namespace EasyFtp
{
    class Account
    {
        public String Username;
        public String Password;
        public String FtpServer;
    }
}

and i have MainWindow Form (Main window for my application ) and logForm with 3 textbox and button. 我有MainWindow窗体(我的应用程序的主窗口)和带有3个文本框和按钮的logForm。 I want to log to my ftp server before show my mainwindow , so i have to showdialog my logform and when thus user press button it get all information from logform and pass it to my mainwindow and save data in object of Account class ; 我想在显示我的主窗口前登录到ftp服务器,所以我必须显示我的日志对话框,当用户按下按钮时,它从日志列表中获取所有信息并传递到我的主窗口中,并将数据保存在Account类对象中; my question is how i pass the data. 我的问题是我如何传递数据。

MainWindow.cs MainWindow.cs

namespace EasyFtp
{
    public partial class MainWindow: Form
    {
        private Account myaccount; 
        LogInForm g; 
        public MainWindow()
        {
            InitializeComponent();
            g = new LogInForm();
            g.ShowDialog();



        }

        /* how i continue the code */
    }
}

LogInForm 登录表单

namespace EasyFtp
{

    public partial class LogInForm : Form
    {       

        public LogInForm()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            /*log in code (not created yet)*/
            this.Dispose();
        }




    }
}

Update: 更新:

Your Main window 您的主窗口

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textBox1.Text = "original text";
    }


    private void button1_Click(object sender, EventArgs e)
    {
        new Form2().ShowDialog();
    }
}

Your dialog-form, that will change the values in Form1 您的对话框形式,它将更改Form1中的值

public Form2()
{
    InitializeComponent();
    // Get the text from Form1
    textBoxOrg.Text = Application.OpenForms["Form1"].Controls["textBox1"].Text;
}

private void button1_Click(object sender, EventArgs e)
{
    // Change the text on Form1
    Application.OpenForms["Form1"].Controls["textBox1"].Text = textBox1.Text;
}

You probably wan't to change value on public properties instead of UI-elements. 您可能不会更改UI属性而不是UI属性的值。

Another, and cleaner way, is to pass fields to the form with a ref 另一种更简洁的方法是使用ref将字段传递给表单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM