petko_stankoski
petko_stankoski

Reputation: 10713

C# opening one window by clicking on another window's button

In MainWindow.xaml.cs I want to open another window. My code is this:

WordToHtml.Window1 newForm = new WordToHtml.Window1();
newForm.Show();
return newForm.imagePath;

In Window1.xaml I have label, button and textBox. I want the user to click on the button and then I read the content from the textbox. However, when the breakpoint comes to newForm.Show();, the Window1 is shown but its invisible. It doesn't have label/button etc. This is the code of Window1:

string imagePath;
public Window1() {
    InitializeComponent();
    label1.Content = @"Please enter path";
}
void button1_Click(object sender, RoutedEventArgs e) {
    //this is never entered because I can't see the button
}
public string newImagePath(string imagePath) {
    return imagePath;
}

Upvotes: 0

Views: 1896

Answers (1)

Jsinh
Jsinh

Reputation: 2579

The code snippet you are showing is not making somethings clear. I am pointing out my confusion & questions.

1). I am assuming that when user clicks on some button on MainWindow, a new windows should open "Window1" where there is a label, button and textbox. then user enters some path in the textbox and clicks on the button the window1 should close and the imagepath should be available at "MainWindow" form. Please correct me if i am wrong.

2). In this code snippet

  WordToHtml.Window1 newForm = new WordToHtml.Window1();
    newForm.Show();
    return newForm.imagePath;

You will not get anything at "newForm.imagePath" or null or empty string as you are trying to access this before the user enters any value in the textbox.

3). Using "SomeForm.Show()" method will open the new form which is not modal dialog means user can still get focus of "MainWindow" or click the button (that opens the new Window1 from). I suggest to use "newForm.ShowDialog()" window which returns focus to parent windows only when it is closed.

4). You shold use

newForm.Closing event to get the reference of the new form and before it is closed you can find the textbox control

string imagePath  = (newForm.FindName("nameOfTextBox") as TextBox).Content.ToString();

and get the imagepath in the MainWindow.

Upvotes: 1

Related Questions