How to close a hidden window (WPF application)? - c#

In my application (WPF) i have this window:
public partial class Window1 : Window and in the Xaml x:Class="WpfApplication1.Window1"
Now, when i switch to and from the main to window 1 and back, i us the Visibility.Hidden and Visibility.Visible to hide them and make them show again to the user.
What i try to do now, is make a test button in the main window, that says: Close Window1.
This window is hidden, but i really want to close it in the background.
at first i though to just use the Window.Close(); but that does not seem to do the trick.
So, how should i do this in a correct way ?
Thank you very much in advance.
EDIT 1 - making question more clear
To open the window1 in my Main window, i use this part
Window1 W1 = null; // Initialise Field.
public void CalcTabel_Click(object sender, RoutedEventArgs e)
{
if (W1 == null)
{
W1 = new Window1();
W1.Hoofdmenu = this;
W1.checkLang();
W1.Show();
}
else
{
W1.checkLang();
W1.Visibility = Visibility.Visible;
}
this.Visibility = Visibility.Hidden;
}
On window 1 there is a Back button, that has this snip-it of code in it (Where "Hoofdmenu" us the main window):
Hoofdmenu.updateStatistics();
Hoofdmenu.Visibility = Visibility.Visible;
this.Visibility = Visibility.Hidden;
But again, this time when standing in the main window (so window 1 is hidden) i want to close down that window 1. but using W1.Close() does not seem to work. So i am looking for a way to Close that window 1, not change its visibility.
EDIT 2 - Solution
So using W1.Close(); did not work, although a small change this.W1.Close(); did work in fact :)

You can create object of Form2 in window and intialize its visiblity to false.
On click of button you can simpliy say
public partial class MainWindow : Window
{
private Window1 window2;
public MainWindow()
{
InitializeComponent();
this.window2 = new Window1();
this.window2.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.window2.Visibility = System.Windows.Visibility.Hidden;
}
}
to make it visible again

after reading your code, thats not possible the way you want it. the window1 instance is local object. so you cant reach it out side if this method.
the best was is to have a close button on window1 with this.close()
or you create a global instance in your main window and then check if not null then close it.

Related

C# usercontrol show

I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}

How to flash a WPF Window? [duplicate]

If I have a Xaml Window, how does one open it as a child window, and then have the parent window wait for the child to close before the parent window continues executing?
Did you try showing your window using the ShowDialog method?
Don't forget to set the Owner property on the dialog window to the main window. This will avoid weird behavior when Alt+Tabbing, etc.
A lot of these answers are simplistic, and if someone is beginning WPF, they may not know all of the "ins-and-outs", as it is more complicated than just telling someone "Use .ShowDialog()!". But that is the method (not .Show()) that you want to use in order to block use of the underlying window and to keep the code from continuing until the modal window is closed.
First, you need 2 WPF windows. (One will be calling the other.)
From the first window, let's say that was called MainWindow.xaml, in its code-behind will be:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
Then add your button to your XAML:
<Button Name="btnOpenModal" Click="btnOpenModal_Click" Content="Open Modal" />
And right-click the Click routine, select "Go to definition". It will create it for you in MainWindow.xaml.cs:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
}
Within that function, you have to specify the other page using its page class. Say you named that other page "ModalWindow", so that becomes its page class and is how you would instantiate (call) it:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
ModalWindow modalWindow = new ModalWindow();
modalWindow.ShowDialog();
}
Say you have a value you need set on your modal dialog. Create a textbox and a button in the ModalWindow XAML:
<StackPanel Orientation="Horizontal">
<TextBox Name="txtSomeBox" />
<Button Name="btnSaveData" Click="btnSaveData_Click" Content="Save" />
</StackPanel>
Then create an event handler (another Click event) again and use it to save the textbox value to a public static variable on ModalWindow and call this.Close().
public partial class ModalWindow : Window
{
public static string myValue = String.Empty;
public ModalWindow()
{
InitializeComponent();
}
private void btnSaveData_Click(object sender, RoutedEventArgs e)
{
myValue = txtSomeBox.Text;
this.Close();
}
}
Then, after your .ShowDialog() statement, you can grab that value and use it:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
ModalWindow modalWindow = new ModalWindow();
modalWindow.ShowDialog();
string valueFromModalTextBox = ModalWindow.myValue;
}
Window.Show will show the window, and continue execution -- it's a non-blocking call.
Window.ShowDialog will block the calling thread (kinda [1]), and show the dialog. It will also block interaction with the parent/owning window. When the dialog is dismissed (for whatever reason), ShowDialog will return to the caller, and will allow you to access DialogResult (if you want it).
[1] It will keep the dispatcher pumping by pushing a dispatcher frame onto the WPF dispatcher. This will cause the message pump to keep pumping.
Given a Window object myWindow, myWindow.Show() will open it modelessly and myWindow.ShowDialog() will open it modally. However, even the latter doesn't block, from what I remember.

XAML enable/disable view from another view

I'm a beginner and having some dicciculties with XAML.
I have a main view A, which has a button to open a pop-up-window B. When this happens, Window A should still be visible and openened, but disabled. I've done this in code behind (maybe not the cleanest way, but the only way I know how). The code i used for this is the following:
//Code behind from view A
private void X-Button_Click(object sender, RoutedEventArgs e)
{
var BWindow = new BView();
BWindow.Show();
this.IsEnabled = false;
}
I would like to get window A enabled again once i close window B, but i can t seem to get this work. Any help would be very much appreciated.
You could do it in the following way.
You register yourself on the Closed event of the window, and when it gets closed, you unregister the event, and re-enable the this form.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window BWindow = new BWindow();
BWindow.Show();
BWindow.Closed += BWindow_Closed;
this.IsEnabled = false;
}
void BWindow_Closed(object sender, EventArgs e)
{
Window win = sender as Window;
if (win != null)
{
win.Closed -= BWindow_Closed;
}
this.IsEnabled = true;
}
I assume you are looking for modal window. See similar question asked here: How do make modal dialog in WPF?
The solution is in using ShowDialog method from Window class. See here for reference:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Modal window is the concept when you open new window B from the existing window A. While the B is open, the A is disabled and cannot be used. Window A become active only when the B is closed.

how to pass button_click to child window in C#?

Can anyone here do me a favor?
I have a MainWindow with a HideButton and RetrieveButton. When I click on either one button, it will goes to another ChildWindow. My ChildWindow have a OKButton.
The question here,
How to set if else statement in C# for pseudocode below?
private void OKButton_Click(object sender, EventArgs e)
{
// pseudocode
if (HideButton in MainWindow is clicked)
{
// Perform the works
}
if(RetrieveButton in MainWindow is clicked)
{
// Perform other works
}
Thanks in advance.
By, Aeris
If you are creating the child window on Retrieve or Hide you can easily pass parameters then either via the constructor or by setting a custom property:
ChildWindow child = new ChildWindow();
child.Retrieving = true;
child.Show();

WPF event when a window is no longer on top

I have a WPF window (window1) whose owner is window2. If a user clicks on window2, or the desktop, or anything else to make window1 not on top of the z-order, I want to set window1's visibility to hidden. I.e., the window either needs to be on top, or hidden. Is this possible?
Yes.
public Window1()
{
InitializeComponent();
this.Deactivated += new EventHandler(Window1_Deactivated);
}
void Window1_Deactivated(object sender, EventArgs e)
{
Visibility = Visibility.Collapsed;
}
Note that this will also remove it from the TaskBar.

Categories

Resources