2

I was trying to send a label value form the first form to the label value of the second form, but when I press the button in the first form to open the second form, the label is null, no value pass from the first form to the second form.

Here it is the first form codes:

private void btnFirstForm_Click(object sender, RoutedEventArgs e)
{
    SenderClass fl = new SenderClass();
    fl.setFLname(lblFLName.Content);
    secondForm.Show();
    this.Hide();
}

And here it is the class that transfer the value from the first form to second form:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class SenderClass
{
    object flName;

    public SenderClass() {}

    public void setFLname(object flNAME)
    {
        flName = flNAME;
    }

    public object getFLname()
    {
        return flName;
    }
}

and here it is the codes of the second form:

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    SenderClass fl=new SenderClass();
    lblFLname.Content = "Welcome" + fl.getFLname();
}
Prescott
  • 7,312
  • 5
  • 49
  • 70

4 Answers4

0

You are dealing with two instances of the SenderClass and a member variable within those instances. You need to make flName property static if you want to have the same value independently of instances.

matt-dot-net
  • 4,204
  • 21
  • 24
0

You create a new instance of SenderClass in both forms. Therefore the two forms are working on another fl.

Instead, create a public write only property in the second form

public string FLnameContent
{
    set {
        lblFLname.Content = value;
    }
}

In the first form write:

var secondForm = new SecondForm();
secondForm.FLnameContent = lblFLName.Content;
secondForm.Show();

Forms are just class instances and you can transfer data between them just as with any other class. The fact that they are forms makes no difference.


And also, see this answer which explains how to write properties in C#: https://stackoverflow.com/a/9854944/880990

Community
  • 1
  • 1
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • `secondForm` is a class not an instance of that class so you need to create an instance to access the `public property`, isn't it? – sujith karivelil Aug 09 '15 at 17:24
  • You must create an instance with `secondForm = new SecondForm();` as with any non-static class. A class is a blueprint for objects. Such an object is a class instance. – Olivier Jacot-Descombes Aug 09 '15 at 17:27
0

In btnFirstForm_Click as well as in Grid_Loaded, you are creating a separate instance of the class. even though the object names(f1) are the same, the compiler is treated them as individual objects since these are separate instances of the same class. so I suggest you use the following:

create a static Property in the secondForm() class.

you should define a static property since even public property requires a reference.

public class secondForm
{
    public string setFLname
     {
      set{lblFLname.Content = value;}
     }
}

Then re-write your button and click like the following:

 private void btnFirstForm_Click(object sender, RoutedEventArgs e)
 {
    secondForm.setFLname= lblFLName.Content;
    SecondForm secondFormInstant = new SecondForm();
    secondFormInstant.Show();
    this.Hide();
 }

and your Grid_Loaded() method will be like the following

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    SenderClass fl=new SenderClass();
    lblFLname.Content = "Welcome" + setFLname;
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

You created two different instances for SenderClass, that's why fl.getFLName() is null. SenderClass instance could be created outside the functions as a variable.

SenderClass fl = new SenderClass();

private void btnFirstForm_Click(object sender, RoutedEventArgs e)
{
    fl.setFLname(lblFLName.Content);
    secondForm.Show();
    this.Hide();
}

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    lblFLname.Content = "Welcome" + fl.getFLname();
}
currarpickt
  • 2,290
  • 4
  • 24
  • 39