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();
}
}
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 a Windows Forms Add-in on Revit using Revit-API and C#. The project contains two forms ( Main form and About form). the About form opens by a button click in the main form. The problem is that when I try to close the About form through the Control box or by using a button inside it About.ActiveForm.Close() it automatically closes all the Forms. I tried About.ActiveForm.Hide() and also doesn't work. Also, I tried to link the Cancel Button of the about form to that button and it didn't work.
Code of initiating the About form
private void button3_Click(object sender, EventArgs e)
{
About abt = new About();
abt.ShowDialog(this);
}
Code of Closing the About Form
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
I also tried this for the closing part
private void button3_Click(object sender, EventArgs e)
{
Close();
}
and I tried this also for the initiating part
private void button3_Click(object sender, EventArgs e)
{
About abt = new About();
abt.ShowDialog();
}
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();
}
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;
}
I need to detect whenever the MenuStrip (NOT a contextmenu) is opened and closed. The Enter and Leave events do not seem to work.
Why I need it:
I use a MenuStrip with options like "File", "Edit", "About", etc. But when the menu strip is active, and the user navigates through it using either the mouse or keyboard the other controls on the Windows Form also respond to it.
For example: I click the "Edit > Paste Special.. > Unformatted text" in my application using the mouse. But below the expanded menu item is a XNA 2d-rendering control that also detects the mouse input and does something that I don't want it to do. Now if I could just detect whenever the menu is opened I could disable/enable the appropriate controls.
Without knowing a little more about what you are doing, you can try the code below:
private void Form1_Load(object sender, EventArgs e)
{
menuStrip1.GotFocus += new EventHandler(MenuStrip1_GotFocus);
menuStrip1.LostFocus += new EventHandler(MenuStrip1_LostFocus);
}
private void MenuStrip1_GotFocus(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void MenuStrip1_LostFocus(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
This seems to be working for what you have described above. If you have the menu strip with tab stop on (true) then the got focus events will handle this case. If you only need to make changes if the mouse is used then Menu Active events should work.