How to display Multiple values from form1 to form3

2019-08-14 04:54发布

问题:

I'm new to C# and I want to display the Values of my textBox1, textBox2, and textBox3 from form1 to my labels in form 3. But it just show blank spaces. Can someone show me how to do it?

pic:

回答1:

You can do this, on button click in form 1,

 private void btnTransfer_Click(object sender, EventArgs e)
        {
            Form3 frmdisplay = new Form3(txtFirstName.Text.ToString(), txtSecondName.Text.ToString(), txtPay.Text.ToString());
            frmdisplay.Show();
        }

and show it in form 3 like this,

 public Form3(string Firstname,string Lastname ,string Pay)
        {
            InitializeComponent();
            lblName.Text = "Your name is" + Firstname +" "+ Lastname ;
            lblPayment.Text = "Your payment is" + Pay;

        }


回答2:

Assuming you are trying to load data into Form3 after Form1 presses the "Form 2" button, you need to: 1. Name your controls 2. Set your Form3 controls to the same values as those from Form1

Example which uses guesses for your names (since you posted no code):

var resultDialog = new Form3();
resultDialog.lblFullName.Text = this.txtFirstName.Text + " " + this.txtLastName.Text;
resultDialog.lblPayment.Text = this.txtPayment.Text;


回答3:

in your Form1 class code:

private void form2btn_Click(object sender, EventArgs e)
{
    Form form2 = new Form2(FirstName.Text + " " + SecondName.Text, Pay.Text);
    form2.ShowDialog();
}

your Form2 class code:

public class Form2 : Form
{
    public Form2(string name , string pay)
    {
        Form form3 = new Form3(name, pay);
        form3.ShowDialog();
    }
}

your Form3 class code:

public class Form3 : Form
{   
    public Form3(string name , string pay)
    {
        NameTextBlock.Text = name;
        PayTextBlock.Text = pay;
    }

}


回答4:

From3

    public void Message(string firstname, string lastname, string pay)
    {
        lblName.Text = "Your name is" + firstname + " " + lastname;
        lblPayment.Text = "Your payment is" + pay;
    }

From1

        public Form1()
        {
            InitializeComponent();
        }

        public delegate void SendMessage(string Firstname, string Lastname,string Pay);
        public event SendMessage OnSendMessage;

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 from3 = new Form3();
            OnSendMessage += from3.Message;
            OnSendMessage(Firstname.Text, Lastname.Text, Pay.Text);
            from3.Show(); 
        }


回答5:

Step 1: Add a property in form2 to set the labels' text

public string Name
 {
      set{label1.Text=value;}
 } 

Step 2: In form1's button click event handler add the following code.

private void button1_Click(object sender, System.EventArgs e)          
 {
      Form2 frm=new Form2();
      frm.Name=txt1.text;
      frm.Show();
 }           

You can add the properties on need basis