简体   繁体   中英

How to set variable in main Window from modal dialog

I´m sorry about my stupid question, but I´m stuck. I´m creating application, which will be working with SQL. In MainWindow I have a DataGrid which show result from SQL query. I want set the query in Modal Dialog. I´ve created it, but I don´t know how to connect string from TextBox in modal Dialog to string in Main Window.

assign string query from this window:

public partial class DB_conn_win : Window
    {
        public DB_conn_win()
        {
            InitializeComponent();
        }

        private void ask_DB_Click(object sender, RoutedEventArgs e)
        {
            string query = textBox1.Text();
        }
.....
}

to main window

public partial class MainWindow : Window
{

    string DB_query = DB_conn_win.query;


    public MainWindow()
    {
        InitializeComponent();
 .....

Thanks a lot for help!

public partial class DB_conn_win : Window 
{ 

    private void ask_DB_Click(object sender, RoutedEventArgs e) 
    { 
        this.Query = textBox1.Text(); 
    } 
    public string Query;
..... 
} 



public partial class MainWindow : Window 
{ 

  string DB_query = DB_conn_win.query; 

  public SomeButton_Click(object sender, RoutedEventArgs e)
  {
     var dialog = new DB_conn_win();
     if (dialog.ShowDialog() == true)
     {
       this.DB_query = dialog.Query;
     }
  }
public partial class DB_conn_win : Window { 
    public string query;
    private void ask_DB_Click(object sender, RoutedEventArgs e) { 
        this.query = textBox1.Text(); 
    } 
}

public partial class MainWindow : Window {
    string DB_query;

    public MainWindow() {
        InitializeComponent();
        Loaded += Window_Loaded;
    }

    void Window_Loaded(object sender, RoutedEventArgs e) {
        DB_conn_win dialog = new DB_conn_win();
        dialog.Owner = this;
        dialog.ShowDialog();
        if (dialog.DialogResult != null && dialog.DialogResult.Value)
            DB_Query = dialog.query;
     }   
}

The ShowDialog and DialogResult check depends on how your dialog is set up - see this link for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM