2

I've got two WPF windows and I want to do this: when user clicks on a button, which is located in the first window, a second window will open and in this window the user will click for example on button "Sword". Then in first window's textblock, text will show "Sword" immediately.

I know I can transfer text or string with text file or XML file or class. But I don't know how to do it immediately. Should I use timer or is there another way?

Thanks for reply.

kasperhj
  • 8,566
  • 19
  • 53
  • 103

2 Answers2

3

The simpiliest way is to pass first window as a parameter to the constructor of the second window and change properties of first one on the button click event handler.

public class SecondWindow : Window
{
    private readonly FirstWindow _owner;
    public SecondWindow(FirstWindow owner)
    {
        _owner = owner;
    }

    private void OnSwordButtonClick(object sender, EventArgs e)
    {
        _owner.TextBlockValue = "New value";
    }
}

You can use more commplex and universal solution with EventAggregator or other event-based patterns. Event aggregator helps to implement subscriber/publisher pattern in low-coupled app.

First create new event with property that you want to send between components (or without any properties if there is no data in this event).

Next subscribe first window to your event and change your data (string binded to the Label or other model property) in event handler with parameter received from handler parameter.

Finally raise (or publish in other terms) your event from your second window on button click event handler.

As you can see both of implementations is a part of MVVM libraries and this pattern can be useful for your app.

EDIT

Your code examples shows a problem with NullReferenceException. It happens because you are calling Window1 constructor in the wrong manner. I write full example below but try to repeat again with more details. Change your code to the next one:

public class Window1 : Window
{
    private readonly MainWindow _owner;

    // The only constructor of Window1 class
    public Window1(MainWindow owner)
    {
        InitializeComponent();
        _owner = owner;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        _owner.TbW1.Text = "Sword1";
    }
}

public class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        // create new instance of Window1 and pass current window as a constructor parameter
        var win1 = new Window1(this);
        win1.Show();
    }
}

Also, there are many recommendations for you.

  1. It's better to use sensible class/method/variable names for example OptionsWindow instead of Window1 OnChooseSwordButtonClick() instead of button_Click. Read more about Naming Guidelines on MSDN.
  2. Read more about C# Coding Conventions on MSDN.
  3. Read more about NullReferenceException.
Community
  • 1
  • 1
Vadim Martynov
  • 6,849
  • 5
  • 28
  • 39
  • Thank you! I've already read about MVVM pattern, it can be very usefull for me :) – Peter Happy Jan 27 '16 at 18:43
  • I have tryed it, but got some error, i used this : _owner.TbW1.Text = "SwordGold"; ERROR : "An unhandled exception of type 'System.NullReferenceException' occurred" I dont have idea, why :( – Peter Happy Jan 27 '16 at 20:01
  • Try to concrete what variable is actually null. May be the `owner` is not assign on the constructor or it's not passed from first window. Add your code of first and second windows and also the lines where you create new second window and pass first one (the line with new `SecondWindow(...)`). http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Vadim Martynov Jan 28 '16 at 05:48
  • [picture one](http://imageshack.cz/image/qx2), [picture two](http://imageshack.cz/image/qxI), [picture three](http://imageshack.cz/image/qxK), _owner seems to be null, but code doesn't show error even after build. – Peter Happy Jan 28 '16 at 15:42
  • @PeterHappy I extended my answer and also tries to help you with few additional links. – Vadim Martynov Jan 28 '16 at 19:12
  • Thanks for additional links,it's working now. I'am still newbie :) – Peter Happy Jan 28 '16 at 19:30
0

The term you are looking for is Interprocess Communications (IPC).

Here is an excerpt from Microsoft:

The Windows operating system provides mechanisms for facilitating communications and data sharing between applications. Collectively, the activities enabled by these mechanisms are called interprocess communications (IPC). Some forms of IPC facilitate the division of labor among several specialized processes. Other forms of IPC facilitate the division of labor among computers on a network.

Black Frog
  • 11,168
  • 1
  • 31
  • 64
  • Why so complex? You can just open new window in the first app. – Vadim Martynov Jan 27 '16 at 18:24
  • That is why I asked the @OP is this two applications/two executable or just one application with two windows form. And I suspect it's two executable because he mention text file. – Black Frog Jan 27 '16 at 18:30