C#/WPF: Closing the current window without closing the whole project [duplicate] - c#

This question already has answers here:
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application
(3 answers)
Closed 6 years ago.
I have a project which acts as a boardgame. In this board game, I have multiple mini-games that the user can play (memory game, tic-tac-toe, minesweeper.) My problem is how to not close down the whole project when asking the user if he wants to start a new game so that he can play the other games.
I have the board game window as the base window, inside that window I have the grids for the games.
For example, when I win the tic-tac-toe a message box pops up asking if I want to start a new game with options yes and no. However, If I choose no, it will close the whole project instead of just stopping to play the tic-tac-toe.
in the tic-tac-toe.cs I have this method for closing the tic-tac-toe game:
internal void newGame(string message)
{
if(MessageBox.Show("Play Again?", message, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Clear(); // calls the method to clear buttons etc.
}
else
{
//Application.Current.MainWindow.Close();
//Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}
}
As you can see both lines in Else are commented as they do close down the whole project instead of just the tic-tac-toe game.

You can override the main window's window closing event:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//You can ask users to play new games, if they click close window (etc.)
e.Cancel = true; //This will cancel the closing of main window.
//You can now write logic to close your local windows (dispose objects of games) and show the main window.
}

Related

DisplayPromptAsync always displaying on main window

I'm studying to become a developer; as a formative project, I'm working on a desktop app to help me organize local amateur chess tournaments with MAUI. Basically, I want to be able to open multiple windows during the same execution, so that I can run various parallel tournaments on the same machine.
On the MainPage I placed a button that creates a new window that displays the NewTournamentPage
private void NewTournamentButton_Clicked(object sender, EventArgs e)
{
Application.Current.OpenWindow(new Window()
{
Page = new NewTournamentPage()
}) ;
}
in the NewTournamentPage I placed a button to add a new player to the tournament, and I want the window to freeze until the user inserts the player name, but without blocking the execution of the other windows. DisplayPromptAsync seems to be exactly what I'm looking for, so I did this:
public async void AddNewPlayer_Clicked(object sender, EventArgs e)
{
string newPlayerName = await this.DisplayPromptAsync("Add new Player", "Name:");
//code to add player to tournament
...
}
When I execute and click the NewTournamentButton multiple times, the windows are created with no problems, and they all work independently from each other, but when I press the AddNewPlayer button, the popup pops on the MainPage window, not on the one calling the method, and it takes and saves the input in newPlayerName; then, the same thing happens on all secondary windows, in order of creation, without updating newPlayerName. What is happening here? What am I missing?
Yes, it is just the case as you said. And I have created a new issue about this problem on github.
You can follow it up here: https://github.com/dotnet/maui/issues/7650.
Thanks for your feedback and support for maui very much.

Problem when running multiple instances of the same form

Here's the situation. I've been developing a serial communication application and I needed multiple instances of my main form to show up when I click on a ListView item. Everything was running fine before I added this code:
private void ListView_DeviceList_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = ListView_DeviceList.HitTest(e.Location);
if (hit.Item != null)
{
Form m = new Form1();
m.Show();
}
}
After I tried to run it and double clicked in my ListView new instance of my form opened up as expected. The problem was they were glued together and I couldn't see the first one. After I closed it and tried to run it again it didn't show up. I could see the application running in the task manager and there was the icon in the windows taskbar but no UI opened up... Sorry this might be really unclear but I don't know how to describe it differently.

C# Multiple GUIs With One Window in Window Form [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My friend and I are trying to work on a text game using Visual Studios Community. As of now, we have started our project in windows form. One thing we are stuck on is being able to design multiple screens but using only one window. As of right now, the way we have it designed is after you click "Start Game" on the first window, it pops open a second window to the character select screen. Once you select a character, it opens a third window.
What we would rather do is be able to design the GUI to display a basic opening splash screen and clicking on "Start Game" would bring up a new "screen" but in the same window. The new screen should have it's own unique GUI from the initial splash screen. Also part of the game, we are going to want to put a pause menu with options. When the user clicks on the pause button, that should bring up a new "screen", again with it's own unique GUI from the main screen you would see during the game.
Is it possible to create multiple GUIs but only using one window in window form? If not, how could we make something like that happen?
Thanks in advance!
You have to use UserControl in this case. A UserControl can be set up as a whole form, then you simply swap the UserControls that you have created.
In visual studio create a UserControl item, put your user interface in them, basically very similar to designing a normal Form you just put buttons, labels and other stuff on it and wire up events and logics and you are ready to go.
You propably need to implement a global logic or business model to handle or pass the events of each usercontrol you are creating to have a unified model accross your application.
Here is a good tutorial on using UserControl
You can also apply transition animations while swapping between different controls, anyway if you google these stuff up you will find plenty of useful data.
You can create a wizard like control which can have a custom TabControl. Each tab page can then have different controls you would like to place. You can also create user controls and add them to tab pages which would make it a bit easy to maintain.
Customizing a tab page:
public class CustomWizard : TabControl
{
protected override void WndProc(ref Message m)
{
// Second condition is to keep tab pages visible in design mode
if (m.Msg == 0x1328 && !DesignMode)
{
m.Result = (IntPtr)1;
}
else
{
base.WndProc(ref m);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Tab)
return;
base.OnKeyDown(e);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
You can then use this tab in your form and have next and back buttons. Handle the click event of these buttons to move back and forth.

Window management - opening, closing & re-opening non-modal forms?

In pseudocode, this is what I am trying to do from a main window, with many non-modal sub-windows that can be opened and closed independently of the main window.
(Think "preferences" or "find")
On pressing "OPEN WINDOW"
STEP 1: If window does not exist, create it.
STEP 2: Window now exists, so bring it to the front & make it visible.
(Step 2 is NB in case OPEN WINDOW is pressed while window is already open - I don't want multiple instances of it, just bring it to the front.)
On pressing "CLOSE WINDOW"
STEP 3: Close the window
ALT STEP 3: Hide the window
This is the code I have tried. I got as far as being able to open the window, and bring it to the front if OPEN WINDOW is pressed again while the window is open. However, once I close the window, I CANNOT get it to open a second time. I get an error stating that Window.Show() cannot be used once the window is closed.
public static void OpenWindowOnce(Window windowToOpen)
{
foreach (Window n in Application.Current.Windows)
{
//Checks if the window is already open, and brings it to the front if it is
if (n.GetType() == windowToOpen.GetType())
{}
else
{ windowToOpen.Show(); }
}
windowToOpen.Activate();
}
Where am I going wrong in my code/logic? Thank you, I am pretty new to coding and have spent weeks trying to get this right.
You cannot use a Window that has been closed because its resources are disposed at that time. The solution is to simply create a new Window each time that you want to display it.
I can see that you are passing in a Window object and then trying to find the particular type of Window... in this case, you can use reflection to instantiate your Windows from their Type. In particular, please see the Activator.CreateInstance Method page on MSDN. You could use it something like this:
public static void OpenWindowOnce(Window windowToOpen)
{
foreach (Window n in Application.Current.Windows)
{
//Checks if the window is already open, and brings it to the front if it is
if (n.GetType() == windowToOpen.GetType()) { ... }
else
{
windowToOpen = Activator.CreateInstance(typeof(YourWindow)) as YourWindow;
windowToOpen.Show();
}
}
windowToOpen.Activate();
}

Quit Application when all forms is closed

I want to have a lot of forms in my Gtk# application. I want to quit application when user close all form. I try to use next code:
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
/*Application.Quit ();
a.RetVal = true;*/
if(System.Windows.Forms.Application.OpenForms.Count==0)
{
Gtk.Application.Quit ();
a.RetVal = true;
}
}
But System.Windows.Forms.Application.OpenForms.Count allways return "0" regardless of the number of open forms (OS Ubuntu 12.04). How can I solve this problem and get actual quantity of open forms?
Thanks in advance
also tried to find an answer to that issue.
My current implementation is based on some concept I have seen in MS Visual Studio documentation:
in the project's main class maintain a static list of open windows.
do not use the delete event but the destroy event of the GTK# window (with the OnDelete event handler something did not work, if I remember correctly the windows delete cannot be directly called by a member function).
In the OnDestoy event handler: when the window gets destroyed then remove it from the static list. Then check the list for being empty, then quit the application.
Not sure if this is really ideal for GTK# windows but in my application this concept works.
regards
Harald

Categories

Resources