I want to search any visible child window with this simple code but Message keep saying Window not found. Can anyone has an idea about searching visible child window in main window?
Here’s the code:
private HomeWindow NewHomeWindow = new HomeWindow();
string ReturnWindowName;
private void btnhome_Click(object sender, RoutedEventArgs e)
{
ReturnWindowName = "NewHomeWindow";
NewHomeWindow.Owner = this;
NewHomeWindow.Show();
}
private void btnsearchwindow_Click(object sender, RoutedEventArgs e)
{
ChangeWindow();
}
public void ChangeWindow()
{
Window mySearchWindow = (Window)this.FindName(ReturnWindowName);
if (mySearchWindow != null)
{
MessageBox.Show("Window Found");
}
else
{
MessageBox.Show("Window Not Found");
}
}
Since you are assigning ownership to your Windows, can you make use of the OwnedWindows Property to iterate through the Parents Owned Windows to find the one you are looking for? In further looking at your code you are creating a class level variable named
NewHomeWindowyou are not assigning anything to theNameProperty, FindName is searching for a child element, not an owned window. If you add a Name to your Window and use something like this you should be able to locate it.