In my WPF application, I have one main window (Window.xaml). Which has a button, if user click the button we want to open same window again(Window.xaml). Once again the user clicks the same button we want to open the same window again.
so how do I open the same window again?
You can create new instance of your current window in button click event and achieve it.
Code snippet:
private void button_Click(object sender, RoutedEventArgs e)
{
var currentWindow = new Window();
currentWindow.Show();
}
You have to create a new instance of the window (e.g. MainWindow class) and call the show method.
Just some pseudo code:
void Button_clicked()
{
new MainWindow().Show();
}
Related
I have a button that opens a WPF window which shows some calculation.
public void SomeButton_Click(object sender, RoutedEventArgs e)
{
Window1 win2 = new Window1();
win2.Show();
win2.Topmost = true;
}
I have a problem that if I change values in the program and press the button again, I have two open windows.
Is there a way that I can close win1 before opening the new version of win2?
I tried with win2.Close(), but since win2 is not known before the application creates it won't obviously work.
Thanks.
You could store a reference to the window in a variable in your class:
private Window2 win2;
public void SomeButton_Click(object sender, RoutedEventArgs e)
{
win2?.Close();
win2 = new Window2();
win2.Show();
win2.Topmost = true;
}
This will close any previous instance of Window2 that you have created and opened in the click event handler before you open a new window.
I am creating a C# application . I am new to .NET programming. It's basically a big Windows Forms Application which displays various forms, all interlinked with each other based on user control. My main form is login page to validate the user to go to the Menu Form(second form), where there are options for the user to decide. So the main activity starts from the Menu Form(second form), it contains a label holding the username. From the Menu Form(second form), it goes to the third form which is a pop-up form, which leads to the fourth form. Basically, hide the second form when moving to each form. Now from the fourth form, I want to go back to Menu Form(second form) without creating a new instance. I tried to do this without new instances but no luck. See code below:
Second form (Menu):
private void button_Click(object sender, EventArgs e)
{
PopUp form3 = new Popup();
form3.Show();
// Hides the Menu Form(second form)
this.Hide();
}
Third Form:
private void button_Click(object sender, EventArgs e)
{
var menu = new Menu();
menu.Hide();
// Hide Form #3
Hide();
form4.Show();
// Hide Form #3
Close();
}
Fourth Form:
private void button_Click(object sender, EventArgs e)
{
if(grpSaved == false)
{
Form5 form5 = new Form5();
form5.Show();
form5.FormClosed += new FormClosedEventHandler(unsaved_FormClosed);
grpSaved = true;
}
else
{
var menu = new Menu();
Close();//closes fourth form
menu.Show();
}
}
This code creates a new instance of the Menu Form(second form). Please help me to get around this problem.
While on the second form (Your main form) if you select a button or click a check box to access the third and fourth forms, try using a show dialog cod instead for the new form and then place you this.Show(); after the show dialog to ensure that when the form you opened is closed it will return you to the form you opened it from.
Example:
Instead of:
PopUp form3 = new Popup();
form3.Show();
//hides the second (Menu) form
this.Hide();
Try:
//Hide the second form
this.Hide();
//Bring up your PopUp form
using (PopUp form3 = new PopUp())
form3.ShowDialog();
//When your PopUp form closes the code should continue and show the second form again.
this.Show();
So to stack the forms try the following:
//Menu Form
private void button_Click(object sender, EventArgs e)
{
this.Hide();
using(PopUp form3 = new Popup())
form3.ShowDialog();
this.Show();
}
//PopUp Form
private void button_Click(object sender, EventArgs e)
{
this.Hide();
using(Task form4 = new Task())
form4.ShowDialog();
this.Close();
}
//Task Form
private void button_Click(object sender, EventArgs e)
{
this.Close();
}
The last button only needs to close the task form. Once it closes the code on the PopUp form will continue which will close the PopUp form and return you to your original Menu form without loading a new form, and clearing the form stack in the process.
It's a clean solution except that it does not inherently save the data from the other two forms. Clicking on the button on the Menu page again will bring up a new PopUp form.
i have a main window and when users click a button i create a new window to ask informations.
But after how i get these informations in my main window????
//This is the button event handler in main window
private void Button_Click(object sender, RoutedEventArgs e)
{
AddServer window = new AddServer();
window.Show();
}
thank you
I have tried to set up a click event for a button that opens another window,but the error I'm getting at NavigationService is that the project doesn't contain a definition for it.
This is how I'm trying to call the page at present:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("TrainingFrm.xaml", UriKind.RelativeOrAbsolute));
}
Can someone point me in the right direction with this or show alternatives to this method for window navigation?
NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm.
To go to a different window, you should do this:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
var newForm = new TrainingFrm(); //create your new form.
newForm.Show(); //show the new form.
this.Close(); //only if you want to close the current form.
}
If, on the other hand, you want your WPF application to behave like a browser, then you would need to create Pages instead of Forms, and then use a Frame in your application to do the navigation. See this example.
If you want to navigate from Window to Window:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
Window1 window1 = new Window1();
// window1.Show(); // Win10 tablet in tablet mode, use this, when sub Window is closed, the main window will be covered by the Start menu.
window.ShowDialog();
}
If you want to navigate from Window to Page:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
NavigationWindow window = new NavigationWindow();
window.Source = new Uri("Page1.xaml", UriKind.Relative);
window.Show();
}
In order to use NavigationService you should use the Page and not the Window class
I have a window that I want to allow only one instance of it to be open at a time. They can open / close the window, but can't have multiple copies of the same window open at a time.
I have a menu with an option that when clicked, opens the ProductSelection window. ListProductList is my button:
private void ListProductListCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = _productListCanExecute;
//_productListCanExecute = !_productListCanExecute;
}
private void ListProductList(object sender, ExecutedRoutedEventArgs e)
{
_productListCanExecute = false;
ProductSelection pl = new ProductSelection(productCategoryList, productStyleList, productList);
pl.Show();
}
Notice that I set the e.CanExecute of the ListProductList button to false to ensure that the event handler doesn't run and therefore doesn't open more windows.
Now, how can I detect that the ProductSelection window has closed in order to set the _productListCanExecute back to true? It's not a modal window, because I want to allow them to do other things.
Probably the easiest solution here is to make the pl variable a global.
Whenever the button is clicked, just "show" that existing window.