Communicating between windows
-
I have a program that opens a small input window which prompts the user to enter a name. I want to store the name entered in the input window in a string variable in the main window. How do I do that? Jason
-
I have a program that opens a small input window which prompts the user to enter a name. I want to store the name entered in the input window in a string variable in the main window. How do I do that? Jason
I think you have a few options. If the scenario is that the main form creates the sub form and displays it modally the easiest solution would be to just put it in a public variable, so once it closes the variable can be accessed. If its modeless it gets a little bit more tricky. I would make a class containing the string and an object. The main form constructor would initialize the class to some good default value, then you could make the form's constructor accept this new class. When the subclass wants to change the value, use one item in the class to lock the item (for threadsafeness) and then set the string value. The main class could then lock the string, and read it. Hope this helped, -Kenmaz
-
I have a program that opens a small input window which prompts the user to enter a name. I want to store the name entered in the input window in a string variable in the main window. How do I do that? Jason
A not very clean solution : Add a member to your dialog window public AForm : Form { private string _str; public string Str { get { return _str;} set { _str = value;} } DoSomethingToStringAndCloseWindow () { _str = "Hi there"; this.Hide(); } } public CallingForm : Form { void AMethod() { AForm frm = new AForm (); frm.Str = "a string"; frm.ShowDialog(this); string newString = frm.Str; frm.Close(); frm.Dispose(); } } } A Cleaner solution is to share the same object instance between forms...
-
I have a program that opens a small input window which prompts the user to enter a name. I want to store the name entered in the input window in a string variable in the main window. How do I do that? Jason
The easy and standard way to do this is to define a delegate so that your sub window can call a method on the main window to pass back the value
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
I have a program that opens a small input window which prompts the user to enter a name. I want to store the name entered in the input window in a string variable in the main window. How do I do that? Jason
As CG said above use a delegate. This is an example[^] I geve somebody else a couple of weeks ago.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
As CG said above use a delegate. This is an example[^] I geve somebody else a couple of weeks ago.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)Dave, I have tried the example but for some reason I get the following error: "Error 1 'System.Windows.Window' does not contain a definition for 'SendText' and no extension method 'SendText' accepting a first argument of type 'System.Windows.Window' could be found (are you missing a using directive or an assembly reference?) The line that I get the error on is as follows: req.SendText = new getArtistName(changeArtistName); I am using Windows Presentation Foundation instead of Windows Forms. Jason
-
Dave, I have tried the example but for some reason I get the following error: "Error 1 'System.Windows.Window' does not contain a definition for 'SendText' and no extension method 'SendText' accepting a first argument of type 'System.Windows.Window' could be found (are you missing a using directive or an assembly reference?) The line that I get the error on is as follows: req.SendText = new getArtistName(changeArtistName); I am using Windows Presentation Foundation instead of Windows Forms. Jason
This works for me (WpfApplication1 : Window1 : Window2 with textBox1): Window1.xaml.cs
using System.Windows;
namespace WpfApplication1
{
public delegate void UpdateText(string text);
public partial class Window1 : Window
{
public Window1() { InitializeComponent(); }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2();
window2.SendText = new UpdateText(Update);
window2.Show();
}
private void Update(string text)
{
this.Title = text;
}
}
}Window2.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class Window2 : Window
{
public UpdateText SendText;
public Window2() { InitializeComponent(); }
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
SendText(textBox1.Text);
}
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
This works for me (WpfApplication1 : Window1 : Window2 with textBox1): Window1.xaml.cs
using System.Windows;
namespace WpfApplication1
{
public delegate void UpdateText(string text);
public partial class Window1 : Window
{
public Window1() { InitializeComponent(); }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2();
window2.SendText = new UpdateText(Update);
window2.Show();
}
private void Update(string text)
{
this.Title = text;
}
}
}Window2.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class Window2 : Window
{
public UpdateText SendText;
public Window2() { InitializeComponent(); }
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
SendText(textBox1.Text);
}
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)This worked perfectly. Thank you very much. Jason