Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值传递到另一个表单c_C#_Visual Studio 2010 - Fatal编程技术网

C# 将值传递到另一个表单c

C# 将值传递到另一个表单c,c#,visual-studio-2010,C#,Visual Studio 2010,如何将txtFirstName.Text的值传递给其他表单?还有其他输入。我粘贴了我正在处理的两个表单。请帮帮我。还是使用多维数组更好?我在做一个类似于帐户登录注册的程序,在那里你可以查看你在一个最多5个帐户的配置文件中输入的信息。请帮忙。谢谢 public partial class frmInfo : Form { public frmInfo() { InitializeComponent(); } private void btnConf

如何将txtFirstName.Text的值传递给其他表单?还有其他输入。我粘贴了我正在处理的两个表单。请帮帮我。还是使用多维数组更好?我在做一个类似于帐户登录注册的程序,在那里你可以查看你在一个最多5个帐户的配置文件中输入的信息。请帮忙。谢谢

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

    private void btnConfirm_Click(object sender, EventArgs e)
    {
        if (txtFirstName.Text == String.Empty)
        {
            errorProvider1.SetError(txtFirstName, "Your First Name is important.");
        }
        else
        {
            errorProvider1.Clear();
        }

        if (txtLastName.Text == String.Empty)
        {
            errorProvider2.SetError(txtLastName, "Your Last Name is important.");
        }
        else
        {
            errorProvider2.Clear();
        }

        if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty)
        {
            frmProfile profile = new frmProfile();
            profile.Show();
            this.Hide();
        }
    }
}


//Other form
public partial class frmProfile : Form
{
    public frmProfile()
    {
        InitializeComponent();
    }

    private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmChangePassword changepass = new frmChangePassword();
        changepass.Show();
        this.Hide();
    }

    private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmMain logout = new frmMain();
        logout.Show();
        this.Hide();
    }
}
你能行

frmProfile profile = new frmProfile();
profile.txtFullName.Text = String.Format("{0} {1}",  
                                         txtFirstName.Text, 
                                         txtLastName.Text);
profile.Show();

您使用的是面向对象的语言,那么为什么不尝试使用类并传递此类的实例呢

首先用相关属性定义类

public class Profile
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    .... other properties will follow as you like...
}
现在在第一个窗体上的按钮的单击事件中

private void btnConfirm_Click(object sender, EventArgs e)
{
    Profile pf = new Profile();
    if (txtFirstName.Text.Trim() == String.Empty)
    {
        errorProvider1.SetError(txtFirstName, "Your First Name is important.");
        return; 
    }
    else
    {
        errorProvider1.Clear();
        pf.FirstName = txtFirstName.Text;
    }
    .......

    // Pass your Profile class instance to the constructor of the frmProfile
    frmProfile profile = new frmProfile(pf);
    profile.Show();
    this.Hide();
}
现在在frmProfile类中使用传递的实例

public partial class frmProfile : Form
{
    public frmProfile(Profile pf)
    {
        InitializeComponent();
        txtFirstName.Text = pf.FirstName;
        .....
    }
    .....
}

通过这种方式,您可以只传递一个包含所有数据的变量,而无需将每个文本框传递到frmProfile表单

您可以向frmProfile的构造函数添加两个参数。然后,在构造表单的新实例时,将文本框的值传递给表单

FRM文件的构造函数:

public frmProfile(string Firstname, string Lastname) {
    // Do something with Firstname and Lastname
    InitializeComponent();
}
然后在btnConfirm_单击功能中:

    if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty) {
        frmProfile profile = new frmProfile(txtFirstName.Text, txtLastName.Text);
        profile.Show();
        this.Hide();
    }

别指望我们帮你做家务。我不是叫你做家庭作业。我只是不知道如何解决它,我需要一些帮助。名称txtFirstName在当前上下文中不存在名称txtLastName在当前上下文中不存在以避免使用只包含空格的FirstName。不是空的,但肯定不是有效的名称。如何在数组中使用此名称?我会在课堂上做同样的声明吗?不清楚,但我建议发布一个新的问题,明确说明你的要求。这样会有更多的人关注你的问题。请随时在这里给我留言,并链接到你的新问题。我刚刚被禁止提问。嗯,我明白了,对你的问题投了很多反对票。我建议从你的问题中删除不必要的代码。例如,所有这些空事件处理程序也会修改您的其他问题,以检查它们是否可以变得更清楚。。。。看见