C# WPF child window (about window) - c#

I have an application on C#/WPF that I`m developing ... I have a btn named About which should open a new window that contains details about the application or whatever I will put in it.
When I click the btn, a new window (about) opens, when I click again, while the new window (about) is opened another one is opened, how can I prevent this from happening, I also want the application to be disabled when the About window is opened and to be enabled when the About window is closed, Like most of the applications when about is clicked.

You should use the ShowDialog Method: http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Code Sample:
// Instantiate window
AboutWindow aboutWindow = new AboutWindow();
// Show window modally
// NOTE: Returns only when window is closed
Nullable<bool> dialogResult = aboutWindow.ShowDialog();

Just use ShowDialog() method instead of Show()
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();

Related

Close Window after open another window?

I'm work on WPF project, let say I have two windows window1,window2, window1 call window2: in window1 code behind :
Window2 _window2 = new Window2();
_window2.ShowDialog();
What I want to know how I close first window (window1) after the secound window (window2) is opened ?
If "Window1" is not your main window you can just type this
Window2 _window2 = new Window2();
_window2.Show();
this.Close();
If your form is the main form than refer
(Windows Forms: Change application mainwindow at runtime)
Before you can close the main window (if it is), then you need to tell the app first which is the main window. Depending upon your shut down mode, if you close the main window, you shut down the app. You can set this using Application.Current.MainWindow property. See Here.
If you wish to close the first window after opening the second, you will need to hook up an event, or better still use a decoupled messaging system to inform the first window the second has opened.
Form1:
Window2 _window2 = new Window2();
this.Hide(); //Hides Form 1
_window2.ShowDialog();
Also see:
Close a Window from Another In Wpf
if you wanna have window1 closed.

Excel closes when I close the main window in wpf

I have a Excel AddIn project which parses excel workbook and pops a wpf window from excel ribbon when clicked on a button. the problem is that when I close the window (UI control of type window)pop up. closing action on Window closes the Excel too.
the code is something like this to show the window.
MainWindow main= new MainWindow();
main.ShowDialog();
can someone tell what's wrong here?
I have to question why you are creating a new instance of MainWindow?
What you should be doing is (http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx):
// Instantiate window
DialogBox dialogBox = new DialogBox();
// Show window modally
// NOTE: Returns only when window is closed
Nullable<bool> dialogResult = dialogBox.ShowDialog();

How can I proceed only if the window I just opened is closed?

I have two WPF windows.
Window1's button calling Window2, Window2 updates something, and then I want to proceed exactly where I stopped in Window1.
This is the code in Window1 inside a button_Click:
Window2 window = new Window2();
window.Show();
* wait until Window2 closes
//continue with other actions
How do I wait?
I saw this: wpf c# button wait for a button press
But how can I pass it from a different (already closed) window?
Use Window.ShowDialog method, it opens a window and and waits for window closing.
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Nullable<bool> dialogResult = window.ShowDialog();
// Test dialogResult: it is true if dialog was closed by OK button
Windows used as modal dialogs are usually contain OK and Cancel buttons, and programmed by such way, that pressing OK returns true from ShowDialog methid.
See also: Creating a Modal Custom Dialog Box
http://msdn.microsoft.com/en-us/library/aa969773.aspx
Use Window.ShowDialog() instead of Show(). This will open a modal window and will wait till it closes

Hide a window until it's needed again

I'm working on this WPF project. The main window has 8 buttons which all open another window. What I'd like to do, is when the user clicks one of the buttons, the main window is hidden(not closed), and the secondary window is open. Now, when the secondary window is closed, I want to unhide my main window. Here's the code I've got.
public WndwProjectSetup(Window mainWindow)
{
InitializeComponent();
_mainWindow = mainWindow;
_mainWindow.Visibility = Visibility.Hidden; // hides main window
}
private void WindowClosing(object sender, CancelEventArgs e)
{
_mainWindow.Visibility = Visibility.Visible; // unhides main window
Close(); // close Project Setup window
}
This seems simple and straight forward to me. Yet, I get this error:
Cannot set Visibility to Visible or call Show, ShowDialog, Close, or
WindowInteropHelper.EnsureHandle while a Window is closing.
My question is, why is this not acceptable? What do I need to look into to figure out how to do this?
You don't need to call Close, it's already closing. (If anything you can cancel it by setting e.Cancel to true)

C# WinForms - Cannot Access Main Form When Child Form Is Open

Scenario
I have a C# WinForms application with a main form. I also have a button on this main form, that, when clicked, creates and displays a new form.
The problem....
...Is that I cannot click on anything on the main form when the new form is open.
The Question
How do I solve this? Is it possible to use both forms simultaneously?
Code To Launch New Form
private void barBtnStatsMonitor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//XtraMessageBox.Show("This Feature Has Not Been Fully Implemented Yet!");
using (StatsMonitorForm frm = new StatsMonitorForm())
{
if (frm.ShowDialog() == DialogResult.OK)
{
}
}
}
ShowDialog() opens a modal dialog.
Show() opens non-modal.
Try frm.Show instead of ShowDialog. ShowDialog opens the new form as a modal dialog so you cannot access the base form until you close this one.
when closing the main form its like closing the app...
so a suggestion would be, disable the main form close button when there are child forms open... and just enable it again when there are no child forms open...
or create a global variable(a bool perhaps), that when a child form is open.. its set to true... so when pressing the close button on the main form... it checks this variable if its true it prompts to save.. else it just closes...
ShowDialog() displays the form in MODAL mode which means you must close new form opened.
private void barBtnStatsMonitor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//XtraMessageBox.Show("This Feature Has Not Been Fully Implemented Yet!");
using (StatsMonitorForm frm = new StatsMonitorForm())
{
frm.Show();
//do some work here to get the dialog result some other way..
}
}

Categories