I would like to display a messagebox or second window on top of the main window at the time the main window opens without user doing something. This is more or less a reminder message/window to the user to do something. Once closed by the user control should go back to the main window. The method below opens before the main window is open, which doesn't help.
private static void CheckHomePosition()
{
if (LocalSystem.MountType != 0 && !TelescopeHardware.AtHome) return;
var homeDialog = new HomeDialogWindow
{
Owner = Application.Current.MainWindow,
ShowInTaskbar = false
};
homeDialog.ShowDialog();
}
A WPF Window provides a ContentRenderedevent for cases like this. You can just override the OnContentRendered method within your window.
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
CheckHomePosition();
}
Related
I have two windows. Main Window & Window1.
On Main Window, there is a button1. When it is clicked, it gets disabled and open Window1. But i want to enable button1 on Main Window when Window1 is closing or get closed.
Create A Public Button in Window1
public Button mainBtn ;
on mainWindow in the button click event
private void button_click(object sender , RoutedEventArgs e){
Window1 win = new Window1();
this.button.IsEnabled = false;
win.mainBtn = this.button;
win.Show();
}
add on closing event to Window1
private void Window_closing(object sender , CancelEventArgs e){
mainBtn.IsEnabled = true;
}
the idea is to pass the MainWindow Button to the Window1 Button
then you can control it as like you want .
I guess you are using WinForms. In that case you have an event handler for the click on button1:
private void OnButton1Clicked(object sender, ...)
{
// show window 1
}
Now there are two methods to show a Form. You can show it as a modeless dialog box or as a modal dialog box.
Modal dialog boxes, which require the user to respond before continuing the program
Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities
Most dialog boxes you see are Modal: If you press file save, you'll have to finish the Save-File-Dialog box before you can continue editing.
The modal dialog box is the easiest
- Show them using using Form.ShowDialog.
- ShowDialog returns when the form is closed.
If you use a modal dialog box your code would look sequential:
private void OnButton1Clicked(object sender, ...)
{
using (Window1 window1 = new Window1())
{
// if needed window1.SetValues...
var dlgResult = window1.ShowDialog(this);
// if here, window 1 is closed
if (dlgResult = DialogResult.OK)
{ // ok button pressed
// if needed: window1 read resulting values
}
} // because of using window 1 automatically disposed
}
However if window1 is shown as a modeless dialog box, window1 will have to tell others that it is closed. Use event Form.Closed:
private Window1 window1 = null;
private void OnButton1Clicked(object sender, ...)
{
if (this.window1 != null) return; // window1 already shown
this.window1 = new Window1())
this.window1.Closing += this.OnFormClosed;
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
Debug.WriteLine("window1 closed");
if (this.window1.DialogResult = DialogResult.OK)
{
// process dialog results
}
this.window1.Dispose();
this.window1 = null;
}
Data Binding is the strongest tool in WPF:
Add the button and bind the IsEnabled property to a public property in your view model or code behind. In the secondary window - when closing - update the property to reflect the new state.
Do not forget to implement INotifyPropertyChanged
I've got a modal dialog box and (when user presses its OK button) I want to hide it, show another modal dialog box (MessageBox for example) and then show it back again. My problem is that when the dialog is hidden, its DialogResult gets false and of course its getting closed right after the button's handler method ends. I've even tried to set Opacity to 0 instead of Hide() but that doesn't work at all (it's still visible).
Is there a way to hide a modal dialog box for a moment without closing it?
Workaround that is working for me:
To prevent the window from being closed once you set the DialogResult, create your own DialogResult instead:
public new bool DialogResult;
Now you can still set the variable and choose Hide() instead of Close().
So all the places where DialogResult is set I add a
Hide();
So i looks like this:
DialogResult=true;
Hide();
or
DialogResult=false;
Hide();
That way I can do a new ShowDialog() again.
So if I need to reopen the window until the content is correct (if validation happens after closing), it would look something like this:
public void ShowDialog()
{
var dialog = new MyDialog();
bool ok = false;
while (!ok)
{
dialog.ShowDialog();
if (dialog.DialogResult)
{
ok = DoSomeValidation();
}
else
{
ok = true;
}
}
}
This does not deal with the result but see how to return data from a Page
PageModal is a Page
You use NavigationWindow for the modal part
public partial class MainWindow : Window
{
private PageModal pageModal = new PageModal();
public MainWindow()
{
InitializeComponent();
}
private void btnLaunchModal(object sender, RoutedEventArgs e)
{
NavigationWindow navWindow = new NavigationWindow();
navWindow.Content = pageModal;
navWindow.ShowDialog();
}
}
Ok, the opacity IS working. I just had it blocked by finished animation (with HoldEnd behavior) and I didn't knew about it. So, if anyone has the same problem and needs to hide a modal window, the Opacity = 0; is the solution.
I have the following code in App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
var window = new WelcomeWindow();
if (window.ShowDialog() == true)
{
var mainWindow = new MainWindow();
mainWindow.ShowDialog();
}
}
The second window never shows. Instead, the application simply closes when the Welcome window is closed. How do I ensure a second window can be shown after a first one is closed?
This is because default value of Application.ShutdownMode is OnLastWindowClose. This means when your WelcomeWindow is closed the application shuts down and you see nothing more.
To solve this set ShutdownMode to OnExplicitShutdown and call Shutdown explicitly if you want to exit your app.
public App()
{
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
}
What about to show WelcomeWindow on Initialized event of MainWindow and close last if Dialog is not true. This was you let MainWindow to stay the MainWindow of Application.
private void Window_Initialized(object sender, EventArgs e)
{
// at this moment MainWindow is Initialized but still nonvisible
if ((new WelcomeWindow()).ShowDialog()!=true)
{
this.Close();
}
}
When you load any window Application_Startup it become The MainWindow of application. And it will closed on this window closing.
I've checked that even if you have StartupUri="MainWindow.xaml" in you app.xaml it have no effect if some else window have been shown on Application StartUp event.
You may do it yourself. Just make breakpoint on your firstloaded window Loaded event handler and look in debuger on "Aplication.Current.MainWindow == this" expression result. It will be true.
all,, I have a project that is built in VB.Net 2010 and WPF 4. I have an option to return to a sign-in screen, yet this option can only be accessed from a separate (quit confirmation) window. This function can be called when one of about a hundred windows are open behind the quit confirmation window.
What I want to do is close the quit confirmation window, and the other window that is open, but open the sign-in window. I know to use "Me.Close()" to close the quit confirmation window, and I know how to open the sign-in window, but I do not know how to close the other window that is open.
Help?
Couldn't you give the constructor of the quit-confirmation-window a reference to the window it should close when that option is chosen?
Edit: Two examples of how to do it:
Often you'd like to open a confirmation-dialogue when the window is being closed, so you'd create it in the Closing event-handler like this:
private void Window_Closing(object sender, CancelEventArgs e)
{
ConfirmationDialog diag = new ConfirmationDialog();
diag.ShowDialog();
switch (diag.Result)
{
...
}
}
Here your window waits for the confirmation dialogue to return (diag.Show would not wait), if that is the case you do not need any information about the window in the dialogue itself. (Result is a custom property that you can define in your dialogue if the DialogResult bool is not differentiated enough)
If your dialogue is opened whenever and you cannot wait for it to return you can create it with a reference:
private void OpenConfirmDialog()
{
ConfirmationDialog diag = new ConfirmationDialog(this);
diag.Show()
}
and in your dialogue code:
public ConfirmationDialog(Window owner)
{
Owner = owner;
}
public void OpenWelcomeScreenThing()
{
this.Close();
Owner.Close();
new WelcomeScreen().Show();
}
or something like that. Alternatively you could work with events as well.
Winforms - why does a "Show()" after a system tray double click end up in my app minimized?
How do I ensure Inthe notifyicon double click event that my hidden main form comes back visible as normal, not minimized (nor maximised for that matter too)
I would guess that you put your application in tray on minimize action. In that case, Show just restores visibility.
Try adding form.WindowState = Normal before Show().
Hiding your form with the NotifyIcon is often desirable so your app starts in the tray right away. You can prevent it from getting visible by overriding the SetVisibleCore() method. You also typically want to prevent it from closing when the user clicks the X button, override the OnFormClosing method to hide the form. You'll want a context menu to allow the user to really quit your app.
Add a NotifyIcon and a ContextMenuStrip to your form. Give the CMS the Show and Exit menu commands. Make the form code look like this:
public partial class Form1 : Form {
bool mAllowClose;
public Form1() {
InitializeComponent();
notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
showToolStripMenuItem.Click += notifyIcon1_DoubleClick;
exitToolStripMenuItem.Click += (o, e) => { mAllowClose = true; Close(); };
}
protected override void SetVisibleCore(bool value) {
// Prevent form getting visible when started
// Beware that the Load event won't run until it becomes visible
if (!this.IsHandleCreated) {
this.CreateHandle();
value = false;
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!this.mAllowClose) { // Just hide, unless the user used the ContextMenuStrip
e.Cancel = true;
this.Hide();
}
}
void notifyIcon1_DoubleClick(object sender, EventArgs e) {
this.WindowState = FormWindowState.Normal; // Just in case...
this.Show();
}
}