My app is multi-window, here is quickly how it works:
In main window I have a list of items, when I click on one, it opens another window where I can modify it. When I close that window, I want main window to refresh its content. I've tried many event handlers including GotFocus() but it doesn't want to launch my method to refresh the list. Any advise?
If you want something to happen when the other window is closed, you can subscribe to its closed event. This will fire when the windows is closed.
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window1();
wnd.Closed += wnd_Closed;
wnd.Show();
}
void wnd_Closed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
}
Related
I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);
I have an application which runs mostly within the taskbar, and its icon may be left clicked in order to open a Windows form.
The NotifyIcon also has a few MenuItems which can be accessed by right clicking, and each have their own click events to serve their functionality.
private void menuItem4_Click(object Sender, EventArgs e)
{
if (programEnabled == false)
{
programEnabled = true;
Console.WriteLine("Enabled!");
}
else
{
programEnabled = false;
Console.WriteLine("Disabled!");
}
}
//Icon clicked!
private void notifyIcon1_MouseClick(object Sender, EventArgs e)
{
//Reopen the form
Form f = new TemperForm();
}
Every time a MenuItem click event is activated however, NotifyIcon_Click also runs and I need that event for opening the Windows form. I don't want every time I click any MenuItem to also run the code that I wish to run when the icon is clicked.
How can I still use the NotifyIcon_Click event while not having it run every time I click a MenuItem?
I've been at this for hours and it's driving me up the wall!
I want to be able to press a button and have the program open up a new window and close the old one.
I have followed solutions from this link but i have never has success with any of them How do I open a second window from the first window in WPF?
Here is my work soo far:
Window editor = new Window();
editor.Show();
this.Close();
But this does nothing.
The program should open up a new window and close the old one.
The functionality you described will work just fine. The Problem there is would more likely be the function or Methode in which you call this function.
To write a Methode that would handle a Button press as you want is pretty good described here: https://www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release.
Hopefully, this will help you otherwise just ask
here is a small Implementation if that helps:
public partial class MainWindow : Window
{
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
Window editor = new MainWindow();
editor.Show();
this.Close();
}
private void MainWindow_KeyUP(object sender, KeyEventArgs e)
{
}
public MainWindow()
{
this.KeyDown += MainWindow_KeyDown;
this.KeyUp += MainWindow_KeyUP;
}
}
You have to call the second window from the first. This is something I did for a project where it popped up a new login panel window:
private void displayLoginPanel_Click(object sender, EventArgs e)
{
LoginPanel myLogin = new LoginPanel(this);
myLogin.Show();
this.Hide();
}
I used hide() instead of close() because you can see that I am sending a reference of the parent to the child LoginPanel in order to come back later. You can replace the Hide() with Close().
How to set the first window to be read only until I close the second one?
In button click i open new Window
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2 = new window2();
win2.Show();
}
Now the first window should be read-only.
After closing the second window, the first one should have full access again
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.