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. .NET (Core and Framework)
  4. communication between 2 windows

communication between 2 windows

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionhelp
7 Posts 3 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.
  • D Offline
    D Offline
    Defender NF
    wrote on last edited by
    #1

    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

    P R 2 Replies Last reply
    0
    • D Defender NF

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      My blog | My articles | MoXAML PowerToys | Onyx

      D 1 Reply Last reply
      0
      • P Pete OHanlon

        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

        My blog | My articles | MoXAML PowerToys | Onyx

        D Offline
        D Offline
        Defender NF
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • D Defender NF

          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

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          This[^] might help you get a better understanding.

          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

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • D Defender NF

            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

            R Offline
            R Offline
            RobCroll
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • R RobCroll

              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

              D Offline
              D Offline
              Defender NF
              wrote on last edited by
              #6

              Thank you all regards

              P 1 Reply Last reply
              0
              • D Defender NF

                Thank you all regards

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                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

                My blog | My articles | MoXAML PowerToys | Onyx

                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