bdg
bdg

Reputation: 465

Passing data between two wpf forms as strings

trying to get data from the main form to form 2. The main form has a textbox and a button. when the button is pressed it opens form 2 which will display the data entered in the main form as a series of text blocks.

However I cant get the data to transfer between the forms. the code is bellow. can anyone help or suggest anything I can do differently?

WPF 1 main form:

public partial class MainWindow : Window
{



    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnOpenForm_Click(object sender, RoutedEventArgs e)
    {
        //btnset: Takes the values contained in the text boxes and updates   
        //the student class
        //properties.
        Student.sFname = firstname.Text;
        Student.sSname = secondname.Text;
        Window1 details = new Window1();
        details.Show();
    }

WPF 2 code:

 public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void details_Load(object sender, EventArgs e)
    {
        Fname.Text = Student.sFname;
        Sname.Text = Student.sSname;
    }
    private void Close_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

Upvotes: 0

Views: 543

Answers (2)

Ultron_111
Ultron_111

Reputation: 49

I assume you have a class in MainWindow like:

`Public class Student
{
public static string sFname;
public static string sSname;
}`

When you click open button you are assigning values to those variable, but if you want to access them in another window mention the window name and then class name. Check this code if its working?

`public partial class Window1 : Window
{
public Window1()
{
    InitializeComponent();
}

private void details_Load(object sender, EventArgs e)
{
    Fname.Text = MainWindow.Student.sFname;
    Sname.Text = Mainwindow.Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}
}`

Upvotes: 0

Tom
Tom

Reputation: 2390

There are a number of ways to "pass data" between 2 classes. The easiest way is to expose property or method on Window1 and just set the text you need passed. Another way is to create a constructor on Window1 that takes in the data as parameters. Here is code that demonstrates these approaches.

public class Program
{
    public static void Main(string[] args)
    {
        var c1 = new Class1();

        c1.DoStuff();
    }
}

public class Class1
{
    public void DoStuff()
    {
        var c = new Class2("stuff");

        var c2 = new Class2();
        c2.AcceptStuff("stuff2");

        c.Print();
        c2.Print();

        c2.MyData = "stuff3";
        c2.Print();
    }
}

public class Class2
{     
    private string _myData;

    public Class2() 
    {

    }

    public Class2(string myData)
    {
        _myData = myData;
    }        

    public string MyData
    {
        set { _myData = value;}
    }

    public void AcceptStuff(string myData)
    {
        _myData = myData;
    }

    public void Print()
    {
        Console.WriteLine(_myData);
    }
}

Prints

stuff
stuff2
stuff3

Upvotes: 1

Related Questions