0

Right now I'm making a GUI for several different controllers I've made. Each controller is in its own class and is a WPF. In order to use the controller, I need an IP address that the user types into a text box that the GUI contains. When I create an object of my GUI class and try to use the variable, it is whatever I initialize the variable as, rather than the updated variable.

The variable is updated in my GUI class. Not in my other classes.

I am very new to C#, help!

 public partial class MainWindow : Window {

    public String nao_ip = "";

    private void IPAddress_TextChanged(object sender, TextChangedEventArgs e)
    {
        nao_ip = IPAddress.Text;
    }

 }

  public partial class Test : Window
{
    MainWindow main = new MainWindow();

    public Test()
    {
        InitializeComponent();
    }
    public void Window_Loaded(object sender, RoutedEventArgs e)
    {
        String nao_ip = main.nao_ip;
        MessageBox.Show(nao_ip);
    }

}

They are in two separate .xaml files.

4 Answers4

0
public partial class Test : Window
{
    MainWindow main = new MainWindow();

    public Test()
    {
        InitializeComponent();
    }
    public void Window_Loaded(object sender, RoutedEventArgs e)
    {
        main.ShowDialog();

        String nao_ip = main.nao_ip;
        MessageBox.Show(nao_ip);
    }
}
  • This does not work quite as planned. With main.showDialog(), when the Test Window is opened, it opens up another instance of the main window. If I use the newly opened main window, and open up another instance of the Test Window with it, it will properly pass the ip address. Is there a way to avoid opening new instances of the same window? – user3403227 Apr 13 '14 at 01:33
  • You said that “when the Test Window is opened, it opens up another instance of the main window”. It actually opens an existing instance which was created here: ‘MainWindow main = new MainWindow();’ I assume from your comment that you don't want to use this newly created instance of main, but rather an existing instance, already visible when Test window is loaded. If my assumption is correct, then I suggest referencing this existing instance, instead of creating a new one by changing: ‘MainWindow main = new MainWindow();’ To something like: ‘MainWindow main = SomeExistingMainWindow;’. – Robert_Codes Apr 13 '14 at 03:58
  • I'm not sure how to reference an existing instance. When I try something like MainWindow main = MainWindow; It gives an error 'is a type, but used like a variable' – user3403227 Apr 13 '14 at 04:40
  • Can you tell me how the test window launched? Is it launched from MainWindow or from some other class? However it is launched, the test window will need a couple of changes to in order to allow it to reference an existing instance of MainWindow. Please take a look at my example code below. – Robert_Codes Apr 13 '14 at 12:37
0

I passed the instance of the main window via Data Bindings.

I found this previously asked question extremely helpful

Data Binding between two TexBoxes in different windows

Community
  • 1
  • 1
0
//The following allows the Test window to reference an existing instance of    
//Main window. All you need to do is instantiate Test window 
//using the example code in my notes below.

public partial class Test : Window
{
    MainWindow main = null;

    public Test(MainWindow existingMainWindow)
    {
        main = existingMainWindow;
        InitializeComponent();
    }
    public void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //main.ShowDialog(); 
        String nao_ip = main.nao_ip;
        MessageBox.Show(nao_ip);
    }
}
  • The following can be used to launch test window from MainWindow: 'Test testWindow = new Test(this); testWindow.Show();' The following can be used to launch test window from some other class (assuming this other class has access to an existing instance of MainWindow): 'Test testWindow = new Test(SomeExistingMainWindow); testWindow.Show();. By the way, I apologize for all the edits that I made to my comments. I'm new here and am just learning how to use this system... – Robert_Codes Apr 13 '14 at 12:38
-1

I would say: "use a constructor", but it cannot be accurate until you post some code.

EDIT: You mustn't set main to a new instance (via MainWindow main = new MainWindow();) but rather the same.

For example, you could pass this instance in parameter when opening the Test Window with this and just set the main variable with that value (representing an instance of the main window).

kiwixz
  • 1,380
  • 15
  • 23