How to not open multiple windows in WPF - c#

In my WPF application I use this code to open a new windows from a button:
private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
LoginDialog dialog = new LoginDialog();
dialog.Show();
}
But when there is already a LoginDialog open and I click the LoginBtn again it open a new LoginDialog windows. How do I code it so it override the previous one that is open, if any.

You can create a local variable of type Login dialog and check if its null
LoginDialog _dialog;
private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
if(_dialog == null)
{
_dialog = new LoginDialog();
}
_dialog.Show();
}

private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
LoginDialog _dialog = Application.Current.Windows.OfType<LoginDialog>().FirstOrDefault() ?? new LoginDialog();
_dialog.Show();
}

Use ShowDialog() instead of Show() to make the dialog modal (so that you cannot do anything else while the dialog is open).
That also allows you to get the return value, indicating whether the user (i.e.) pressed cancel or ok.

You may not be able to use this due to design restrictions however you could open the new window so that it disables the others.
private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
LoginDialog dialog = new LoginDialog();
dialog.ShowDialog();
}

Related

Close Form when not clicking on it

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);

Closing a window in WPF

I want to close a window in WPF after clicking a button that sends me to another window.
I tried with win3.Close(); but it doesn't work.
This is the Main Window that references to the second window.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window2 win3 = new Window2();
win3.Show();
}
Or it should be hidden?
You only need to input this.Close(); before showing next window.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window2 win3 = new Window2();
this.Close();
win3.Show();
}
You can use this.Close(), this will close WPF window as like winform.close();
But in your case you can use this code:
Window wpfForm = Application.Current.Windows.OfType<Window>().SingleOrDefault(win => win.Name == "YourNameOfWindow");
wpfForm.Close();
you could use
Application.Current.Windows[1].Close();

custom tooltip in c# .net win forms

I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}

How to call the mainwindow object from a second object created from main window in c# .net

Being a very first user in Windows Form Development I want to ask a simple question ...
I created a form(MainWindow.cs) within the solution which opens at the time of running that solution.
Latter I created a second form(SecondWindow.cs) and created a event so that it can be called from the first window by clicking a button.When the second window loded up the first window(MainWindow.cs) will be disabled.
Now I want to enable the MainWindow.cs when the second window is closed.
How to do That...
A simple solution I already have is to hide the MainWindow.cs and latter on closing the second window make a new object of first window and show it.But it is not a good way i think because there is already a object created by .net framework for first window automatically, so why we should create a new object of Mainwindow.cs .
Code Of First Window ( MainWindow.cs ) :
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow();
this.Enabled = false;
price.Show();
}
Code Of Second Window ( On closing SecondWindow.cs )
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
// what will goes here to make the MainWindow.cs to enable state
}
Use price.ShowDialog() to show second form as modal dialog. Main form will be disabled until you close second form:
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
using(SecondWindow price = new SecondWindow())
price.ShowDialog();
}
You can pass the main form as an owner to the second form
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow() { Owner = this };
this.Enabled = false;
price.Show();
}
Then you can reference it from the second form.
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
Owner.Enabled = true;
}

prevent multiple open .XMAL windows when button pressed

guys how can I prevent the opening already open WPF window (without using the user control and not adding in to windows form) when button press event ?
I have following codes, but each time I press the button it will open WPF window according to button pressed amount.
Dose anybody know how to prevent that error
Thank you,
private void button1_Click(object sender, RoutedEventArgs e)
{
win2 v2 = new win2();
v2.Show();
}
here , when each time i click the above button it will open a window, but when its already open; if I clicked the same button it will open another window instead of focusing to the already open window.
how can I prevent that?
(I'm using C# )
private void button1_Click(object sender, RoutedEventArgs e)
{
if(!Application.Current.Windows.OfType<win2>().Any())
{
win2 v2 = new win2();
v2.Show();
}
}
Application.Current.Windows lists all the currently open windows owned by your program. We can check if any are already open that are of the type win2, and if not, then we create a new one.
win2 v2 = null;
private void button1_Click(object sender, RoutedEventArgs e)
{
if (v2 == null)
{
v2 = new win2();
v2.Show();
}
else
v2.Activate();
}
You can use Application.Current.Windows to find all the open windows in your application.
If it contains your Window type Activate it to bing it into focus, if the collection does not contain your Window create a new one
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Windows.OfType<win2>().Any())
{
Application.Current.Windows.OfType<win2>().First().Activate();
}
else
{
new win2().Show();
}
}

Categories

Resources