none
How to set a textbox's text in a different window?

    Question

  • I have two windows: win1 and win2. win 1 has a textbox (tbMfrName). win2 also has a textbox (tbOemName) and a button(btnOK). When btnOK is pressed, the code should set win1.tbMfrName.Text with win2.tbOemName.Text.

    What is the best way to accomplish this task? Both windows would be open when this operation takes place.

    Thanks,
    Hamid

    Wednesday, February 29, 2012 8:39 PM

Answers

  • Some other options would be to use a dataStore.

    You could also refrence your MainWindow like such...

    public partial class Window1 : Window
    	{
    		public MainWindow win = System.Windows.Application.Current.MainWindow as MainWindow;
    		
    		public Window1()
    		{
    			this.InitializeComponent();
    		}
    
    		private void setOem(object sender, System.Windows.RoutedEventArgs e)
    		{
    			win.tbMfrName.Text = this.tbOemName.Text;
    		}
    	}

    ~Christine
    • Marked as answer by HE2012 Wednesday, February 29, 2012 9:47 PM
    Wednesday, February 29, 2012 9:06 PM

All replies

  • Hello,

    If both windows share the same DataContext instance (maybe a ViewModel) and the TextBlocks are bound to the same model property then each time you update a textblock the other one will be refreshed automatically.

    Remind that the ViewModel or DataContext should implement INotifyPropertyChanged.

    Good Luck.

    Wednesday, February 29, 2012 8:54 PM
  • Some other options would be to use a dataStore.

    You could also refrence your MainWindow like such...

    public partial class Window1 : Window
    	{
    		public MainWindow win = System.Windows.Application.Current.MainWindow as MainWindow;
    		
    		public Window1()
    		{
    			this.InitializeComponent();
    		}
    
    		private void setOem(object sender, System.Windows.RoutedEventArgs e)
    		{
    			win.tbMfrName.Text = this.tbOemName.Text;
    		}
    	}

    ~Christine
    • Marked as answer by HE2012 Wednesday, February 29, 2012 9:47 PM
    Wednesday, February 29, 2012 9:06 PM
  • Christine and Homero,

    Thank you very much for your seggestiions. I will work on them.

    Hamid

    Wednesday, February 29, 2012 9:47 PM