Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Communicating between windows

Communicating between windows

Scheduled Pinned Locked Moved C#
question
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason Coggins
    wrote on last edited by
    #1

    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

    K L C D 4 Replies Last reply
    0
    • J Jason Coggins

      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

      K Offline
      K Offline
      Ken Mazaika
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • J Jason Coggins

        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

        L Offline
        L Offline
        lisan_al_ghaib
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        • J Jason Coggins

          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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • J Jason Coggins

            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

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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)

            J 1 Reply Last reply
            0
            • D DaveyM69

              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)

              J Offline
              J Offline
              Jason Coggins
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • J Jason Coggins

                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

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                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)

                J 1 Reply Last reply
                0
                • D DaveyM69

                  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)

                  J Offline
                  J Offline
                  Jason Coggins
                  wrote on last edited by
                  #8

                  This worked perfectly. Thank you very much. Jason

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups