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
Related
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.
I know this can be a duplicate, but I already searched the web and tried literally ALL and EVERY solution I found, but to no avail...
I have a custom form in my project called ParentForm it is the parent of most of my other forms in the project, it has some standard elements on it, like panels and timers.
I have no problem with showing this form, editing its design or code.
but whenever I open a form that is inheriting from it, it opens for a moment (not responding thou) and then visual studio crashes immediately, I think it was only the designer first, but now, even when I open the form's code, the same happens!
I tweaked some of the parent form's code and now it stopped crashing.. but now, whenever I open a child form of it, I get this:
ContaierPanel and panel2 are two panels I have on the parent form, and as I see no problem with them, they are used as containers in the child forms, their access modifiers are public too.
The problems seems to be related to the parent form itself, but I already removed every single line of code in it, that didn't help at all, now I'm stuck with the error you see in the image above.
I also uploaded the complete project to Github in case someone wanted to see the code, here's the link.
I'm using the Material Skin in the project.
Your help is so appreciated, I have no much time before the deadline and there is still much to do... Thanks
Edit: After missing around the code for a few extra hours, I finally discovered the code part that is causing the problem:
As I said before I have a timer on the parent form called tmrCheckConnection, here's the code of the timer:
private void tmrCheckConnection_Tick(object sender, EventArgs e)
{
if (!Program.dbConnection.IsConnect())
{
if (!controlsAreDisabled)
{
disabledControls.Clear();
foreach (Control c in this.Controls)
{
if (c.Enabled)
{
disabledControls.Add(c);
c.Enabled = false;
}
}
controlsAreDisabled = true;
}
lblNoDBConnection.Enabled = true;
lblNoDBConnection.Visible = true;
this.Controls.SetChildIndex(lblNoDBConnection, 0);
} else {
if (controlsAreDisabled)
{
foreach (Control c in disabledControls)
c.Enabled = true;
lblNoDBConnection.Visible = false;
}
}
}
Obviously, the error happens here >> Program.dbConnection.IsConnect() << because that is not an instantiated class yet, and the IsConnect() functions uses some of its un-instatiated members.
So the solution is simply to prevent the timer code from running at design time, so I tried putting this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
return;
at the beginning of the timer's code, it didn't change anything.
Next, I tried to remove the event handler from the timer, and reassign it in the Form_Load like this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Runtime)
tmrCheckConnection.Tick += tmrCheckConnection_Tick;
And again, it didn't help.
Any ideas?
Oh, and I removed the github repository.
I have just switched from VB.Net to C# and am writing a windows application.
In VB.net you could easily change the Shutdown Mode by selecting the properties of the
project and moving to a dropdown where you could choose between "When startup form closes"
and "When last form closes". Please help me to find the equivalent in C#.
By the way, I'm using VS 2010.
In c# the easiest trick to achieve this is to change the entry point of the application in the "program.cs" file.
This entry form should be hidden on startup,but will call the main form. Then call the Application.Exit(); function in the close procedure in any other form/class.
Sample pseudo code below
program.cs
//edit this line
Applcation.Run(startupForm);
StratupForm.cs
//startup method
StartupForm_load (object e)
{
this.Hide();
MainForm mainForm = new MainForm();
mainForm.show();
}
MainForm.cs
//application exit method
MainFormExit_close (object e)
{
Application.Exit();
}
You should probably implement a neater way to manage and keep track of open forms later in your program
It's not the first thing you might look for, but if you look at the docs for Application.Run(ApplicationContext), you'll find sample code to do exactly what you're asking for: exit the application when the last form is closed.
I created a user control using C# for windows form application. This user control has some properties. In runtime, if the user does not enter values for this properties I want to show a message box and exit the application.
The problem is when I write the checking code in the Load event of User Control. When I drag & drop it on the form the message box will appear.
private void UserControl1_Load(Object sender, EventArgs e)
{
if (_getFirstPageArgument==null || _getFirstPageArgument.Length==0)
{
throw new Exception("Some Message");
}
}
How do I distinguish between load on the form and load on run time?
I fear there is a larger problem here. But to solve your immediate problem (if I understand correctly...) There is a form attribute called DesignMode. When you are in the visual studio design mode, this will be true. At runtime, this will be false.
For beginners, #Nimas case can be a good study point to understand that Visual Studio actually runs and executes parts of our code even when we are in design time, which is why the constructor is invoked. Even "DesignMode" property is not 100% reliable. You can find an interesting note here related to that http://weblogs.asp.net/fmarguerie/archive/2005/03/23/395658.aspx
If you only want to know when the type itself has been loaded into the runtime (not a specific instance), you can put code into the static constructor for that class.
If I'm misinterpreting your question, please clarify using a timeline when you want specific events to happen.
I have a C# Winforms app that uses the HelpProvider class.
Whenever i press F1 to bring up help, the help window will always be on top of my application, I cannot bring my application UI to the foreground. I can still interact with my UI, but the help window will remain on top.
Is this by design of HelpProvider? Or am I missing something?
There is a solution to this issue, a bit dirty, but it works.
The thing is, the help window opened by HelpProvider is always on top of its parent window control, which is determined by Control instance in first parameter of Help.ShowHelp. Even if you specify null there, the main application form is still used as parent window.
To avoid this, one can create a dummy form, which will be used as a help parent form. This form will be never shown, but still, help window will be “on top” of it, effectively being NOT on top of all other application windows.
public static class AppHelp
{
private static Form mFrmDummyHost = new Form();
public static void ShowChm()
{
Help.ShowHelp(mFrmDummyHost, "my_help.chm");
}
}
Of course, all other Help.ShowHelp overloads can be called this way as well.
Hope this helps people like me, searching for answers to never-getting-old questions ;)
It is indeed by design, and its something that i did not realise. I have just recompiled my final year project and confirmed it. I have read up about it and basically the help file is set to TopMost=True every time the form is clicked. This means even if you code your form to be TopMost, as soon as you click the help file it will go back on top again.
I do believe if you use start process, it should get around the issue at the loss of some customisability the help provider gives.
private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode ==Keys.F1)
{
System.Diagnostics.Process.Start(#"C:\WINDOWS\Help\mspaint.chm");
}
}
Hope it helps