I'm using WPF. I have a window named NewWindow.xaml, and I want it to open when the user presses a button in a separate Window. This is what I have:
private void Button(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
NewWindow.Show();
}
Although, it gives me an error and says I can simplify it. Then, it doesn't do anything. What can I do?
Try this instead of your code in that method:
NewWindow yourInstanceOfTheNewWindow = new NewWindow();
yourInstanceOfTheNewWindow.Show();
Click here and here for more Information.
Related
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().
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");
}
My WPF application is a media player that has a button that launches another window to a certain location and is my webbrowser. Problem is, once i click my button to launch the browser, my 'hide' button does not hide the browser window :/, from what i researched i need to inherit button click events, take a look at the code:
private void button4_ClickWeb(object sender, RoutedEventArgs e)
{
//opens a web browser if 'Web' is clicked on the WPF Media PLayer
Window1 f1 = new Window1();
f1.Show();
}
the ideas i thought would work is create a 'toggle button' like such:
private void button4_ClickWeb(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if ((bool)tb.IsChecked)
{
f1.Show();
//or
f1.WindowState = WindowState.Normal;
}
else
{
f1.Hide();
f1.WindowState = WindowState.Minimized;
}
}
this approach failed, and yes i tried variations and moving things around with NO success, i was told that i need to make a class of f1 to inherit button click events but i have no idea how to do that :/ any ideas?
here is how the app looks like:
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.
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();
}
}