communication between 2 windows
-
hello i have in my application 2 windows, mainwindow and window2, in window2 there is a bool variable and its false per default, after clicking a button in windows 2 it becomes true and another function in the mainwindow start and should change a buttoncontent in the mainwindow if the variable of windows is true.. the problem is the mainwindow class doesnt get the changing of the variable of window2 and still thinks its false and also the content isnt changing here is the code of the mainwindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public Window2 window2instance2 { get; set; }public void ChangeButton1Content() { if (window2instance2.key) { Button1.Content = "I Changed"; } } private void Openwindows2\_Click(object sender, RoutedEventArgs e) { Window2 window2instance = new Window2(); window2instance.MainWindowInstance = this; window2instance.Show(); } }
and this is the codebehind of window2:
public partial class Window2 : Window { public Window2() { InitializeComponent(); } public bool key = false; public MainWindow MainWindowInstance { get; set; } public void window2button\_Click(object sender, RoutedEventArgs e) { MainWindow mainwindow = new MainWindow(); mainwindow.window2instance2 = this; key = true; MainWindowInstance.ChangeButton1Content(); this.Close(); } }
this was my last try and compiler is telling me at " if (window2instance2.key)" that Object reference not set to an instance of an object. any tipps how can i resolve that? thanks and cheers
-
hello i have in my application 2 windows, mainwindow and window2, in window2 there is a bool variable and its false per default, after clicking a button in windows 2 it becomes true and another function in the mainwindow start and should change a buttoncontent in the mainwindow if the variable of windows is true.. the problem is the mainwindow class doesnt get the changing of the variable of window2 and still thinks its false and also the content isnt changing here is the code of the mainwindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public Window2 window2instance2 { get; set; }public void ChangeButton1Content() { if (window2instance2.key) { Button1.Content = "I Changed"; } } private void Openwindows2\_Click(object sender, RoutedEventArgs e) { Window2 window2instance = new Window2(); window2instance.MainWindowInstance = this; window2instance.Show(); } }
and this is the codebehind of window2:
public partial class Window2 : Window { public Window2() { InitializeComponent(); } public bool key = false; public MainWindow MainWindowInstance { get; set; } public void window2button\_Click(object sender, RoutedEventArgs e) { MainWindow mainwindow = new MainWindow(); mainwindow.window2instance2 = this; key = true; MainWindowInstance.ChangeButton1Content(); this.Close(); } }
this was my last try and compiler is telling me at " if (window2instance2.key)" that Object reference not set to an instance of an object. any tipps how can i resolve that? thanks and cheers
Well, in the MainWindow class, you define a property for Window2 that you don't assign to. In the code for Openwindows2_Click, you create a local variable version of Window2, and instantiate that. So, you can't get to Window2 outside of this method because it's not in scope. You do a similar thing in Window2 attempting to reference MainWindow. So, the first fix is to stop using the method level variable, and use the property. The second thing I'd look at is using a delegate, rather than relying on the second form directly calling a method on the first form. To do this, create a delegate in Window2 which your MainWindow class subscribes to, and then call this delegate from Window2, which will notify MainWindow that there's something to do. If this seems familiar to you, it's because this is how events work in .NET.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Well, in the MainWindow class, you define a property for Window2 that you don't assign to. In the code for Openwindows2_Click, you create a local variable version of Window2, and instantiate that. So, you can't get to Window2 outside of this method because it's not in scope. You do a similar thing in Window2 attempting to reference MainWindow. So, the first fix is to stop using the method level variable, and use the property. The second thing I'd look at is using a delegate, rather than relying on the second form directly calling a method on the first form. To do this, create a delegate in Window2 which your MainWindow class subscribes to, and then call this delegate from Window2, which will notify MainWindow that there's something to do. If this seems familiar to you, it's because this is how events work in .NET.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Thanks for the replay pete yes i know how delegates and stuff work in wpf but actually i m a little bit confused can u please show me with an example code how it will work? that will be easily for me to understand thanks very much jack
-
Thanks for the replay pete yes i know how delegates and stuff work in wpf but actually i m a little bit confused can u please show me with an example code how it will work? that will be easily for me to understand thanks very much jack
-
hello i have in my application 2 windows, mainwindow and window2, in window2 there is a bool variable and its false per default, after clicking a button in windows 2 it becomes true and another function in the mainwindow start and should change a buttoncontent in the mainwindow if the variable of windows is true.. the problem is the mainwindow class doesnt get the changing of the variable of window2 and still thinks its false and also the content isnt changing here is the code of the mainwindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public Window2 window2instance2 { get; set; }public void ChangeButton1Content() { if (window2instance2.key) { Button1.Content = "I Changed"; } } private void Openwindows2\_Click(object sender, RoutedEventArgs e) { Window2 window2instance = new Window2(); window2instance.MainWindowInstance = this; window2instance.Show(); } }
and this is the codebehind of window2:
public partial class Window2 : Window { public Window2() { InitializeComponent(); } public bool key = false; public MainWindow MainWindowInstance { get; set; } public void window2button\_Click(object sender, RoutedEventArgs e) { MainWindow mainwindow = new MainWindow(); mainwindow.window2instance2 = this; key = true; MainWindowInstance.ChangeButton1Content(); this.Close(); } }
this was my last try and compiler is telling me at " if (window2instance2.key)" that Object reference not set to an instance of an object. any tipps how can i resolve that? thanks and cheers
MainWindow mainwindow = new MainWindow();
is creating a new instance of MainWindow. It is not the MainWindow that created Window2. try:public partial class Window2 : Window
{
public MainWindow mainWindow;
public bool key = false;public Window2(MainWindow mainWindow ) { InitializeComponent(); this.mainWindow = mainWindow; } public void window2button\_Click(object sender, RoutedEventArgs e) { mainWindow.ChangeButton1Content(); this.Close(); } public void Dispose() { mainWindow = null; //Remove reference for garbage collection } }
[Edit] Sorry there is one other thing
private void Openwindows2\_Click(object sender, RoutedEventArgs e) { if (this.window2instance2 == null) this.window2instance2 = new Window2(this); this.window2instance.Show(); }
"You get that on the big jobs."
modified on Monday, March 14, 2011 6:34 PM
-
MainWindow mainwindow = new MainWindow();
is creating a new instance of MainWindow. It is not the MainWindow that created Window2. try:public partial class Window2 : Window
{
public MainWindow mainWindow;
public bool key = false;public Window2(MainWindow mainWindow ) { InitializeComponent(); this.mainWindow = mainWindow; } public void window2button\_Click(object sender, RoutedEventArgs e) { mainWindow.ChangeButton1Content(); this.Close(); } public void Dispose() { mainWindow = null; //Remove reference for garbage collection } }
[Edit] Sorry there is one other thing
private void Openwindows2\_Click(object sender, RoutedEventArgs e) { if (this.window2instance2 == null) this.window2instance2 = new Window2(this); this.window2instance.Show(); }
"You get that on the big jobs."
modified on Monday, March 14, 2011 6:34 PM
Thank you all regards
-
Thank you all regards
You're welcome.:thumbsup:
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads