I'm working in Visual Studio 2013 Premium on a Windows forms project. When I'm debugging my windows forms project and I close my start up form, all other forms stop too. I googled this problem and saw that in project --> properties there is a drop down list where you're able to change shutdown mode, which would solve my problem.
The shutdown mode option however is not present at my application properties. I can't find it anywhere else and searching the web didn't resolve this problem for me either, so what could it be?
That only applies to Visual Basic projects.
Here's your options:
For Visual Basic projects, twiddle the option you found on Google
For C# WPF projects, you can set the Application.ShutdownMode property
For C# WinForms projects, you must either:
Accept that it will close when the main form closes
Prevent the main form from closing until all other forms have closed
The Application.OpenForms Property should be helpful to you:
Definition:
Gets a collection of open forms owned by the application.
Example:
In the closing event of the main application you can put following code that will check if there's more than 1 form opened.
var openForms = Application.OpenForms;
if(openForms.Count > 1)
{
//More than 1 form is opened: cancel the close-event
}
else
{
}
Related
This question already has answers here:
How do I create a C# app that decides itself whether to show as a console or windowed app?
(11 answers)
Closed 5 years ago.
I've come from developing in Java to developing in C# in Visual Studio. I'm very comfortable with the change apart from one thing: The way in which I can write hidden or GUI applications.
I used to use Swing for developing my GUI based applications in Java.
What i'd like to do is develop an application in the style of a Console Application with static void Main(string[] args){} however I want to be able to create windows and tell them to be visible/open using a method similar to jFrameObject.setVisible(bool). What I want is a console style application which I can use WPF windows and their components in, and i'd like to be able to make those windows in the window designer built in to Visual Studio 2017.
If at all possible, it would be nice if I had an option to show/hide the console too. I have tried a few different methods to do this, like this, however the console which is shown isn't linked to the C# Console object so doing Console.WriteLine(string); outputs to the debug console in Visual Studio 2017 and not the console which was opened in a window.
Thank you for any help given!
Theres no special thing if you want to open a WPF-window (even in a Console Project).
You can simply add a WPF Form using the Visual Studio Utilities (New/Window or something like this).
To open your window, you have two possibilities:
The first one is to open a dialog modal (the code doesn't continue until the form ist closed)
MyWindow window = new MyWindow();
window.ShowModal();
The second solution is open the window without waiting for the dialog to close(some kind like asynchronous)
MyWindow window = new MyWindow();
window.Show();
You can find further information on this article..
Enjoy programming in C# instead of Java.
Starting out, you can use Windows Forms which enables you to easily add GUI elements to an application, e.g. in your Console Application's Main():
var form = new System.Windows.Forms.Form();
form.Text = "Window Title";
var button = new System.Windows.Forms.Button();
button.Text = "OK";
form.Controls.Add(button);
form.ShowDialog();
This requires you to add a reference to the [System.Windows.Forms] assembly.
As for the console window [question]: Show/Hide the console window of a C# console application
If you want to do WPF, add a class representing some window:
public class SomeWindow : System.Windows.Window
{
public SomeWindow()
{
this.AddChild(new System.Windows.Controls.Button() { Content = "OK", Height = 30, Width = 100 });
}
}
Then use the application object to run the window:
new System.Windows.Application().Run(new SomeWindow());
The Main needs to be marked with the [STAThreadAttribute]. The assemblies PresentationCore, PresentationFramework and WindowsBase needs to be referenced.
You can start out with a Console application and add references to WinForms or WPF assemblies to open windows.
Alternatively you can start with a WinForms or WPF application and open a console using the Win32 API: How do I create a C# app that decides itself whether to show as a console or windowed app?
I have a rather large win forms project that includes a form that opens another form that includes static information for our employees while working on the other form. (I will call the form that is opened the edit form and the informational form the information form for purposes of this question) The problem that I am having is that if they happen to minimize the edit form, they must click on the minimized edit form several times to open it, each time with a ding being heard until it works. This problem is not reproducible in my development environment, but is in the deployed environments including our production and test environments. I have worked backwards commenting out stuff until it went to working, and found that when this information form is accessing the data model is when the problem happens, if I don't let the information form access the datamodel the forms will open up as expected.
Additional Information:
.Net 4.0
Information Form includes a user control
Visual Studio 2013
If anyone has any clue as to what may be causing this issue or something that may help me find this issue it would be much appreciated. I have attempted looking at process monitor to no avail.
Am I crazy or is this normal operation. Visual Studio 2010 SP1.
I have a .NET 3.5 (C#) base form with a few inherited classes. Whenever I open the inherited form in designer view it actually starts running my code... I'm not even executing or debugging.
Example:
I need my form to start a named pipe server which is created in the FormLoad event. Whenever I open the form in designer view, it actually starts up the pipe server.
Maybe I just don't fully understand how designer view works?
Edit: I know it's actually running because I'm trying to debug my pipe code and every time I open the designer window, it starts throwing pipe exceptions at me. I can also see through process explorer that devenv.exe is starting up my pipe instances when I'm not debugging.
Yes, the designer is actually "running" your form.
If you want to avoid this, just put your critical code inside an if block like so:
if(!this.DesignMode) //this is the Form
{
....
}
Cheers
I'm new to WPF and have a question which quite same with the article: "Walkthrough: Debug a WPF window or Windows Form without running your application".
The link: http://blogs.msdn.com/b/habibh/archive/2009/07/17/walkthrough-debug-a-wpf-window-or-windows-form-without-running-your-application-video.aspx
I will take the picture as an example:
http://blogs.msdn.com/blogfiles/habibh/WindowsLiveWriter/DebugaWPFwindoworWindowsFormwithoutrunni_FD38/image_3.png
From the picture, the project have 4 WPF windows such as:
App.xaml
Create...so on.xaml
MainWindows.xaml
ViewChart... so on.xaml
When every time in VS 2010, I click on the starting debugging (F5) or start without debugging(Ctrl + F5) will always run the MainWindows.xaml. What the reason coz this? Is this because of in the App.xaml that we declare StartupUri="MainWindow.xaml".
Can I run the particular WPF windows such number 4. ViewChart... so on.xaml instead of the whole application(*Such as the MainWindows.xaml)?
The article you linked is out of date and the video that demonstrates his solution is no longer hosted. However, the basic steps that were outlined in the video are still possible, though the process can be more trouble than it's worth.
The process is as follows:
Open the Immediate Debugger window (Debug -> Windows -> Immediate Window)
In the Immediate mode, create an instance of the window you are wishing to test.
call the ShowDialog() method on your Immediate instance.
The main trouble with this method of debugging is that if your window has a complex constructor or has dependencies on external objects, you may end up spending more time setting up the dependencies than just navigating to the page would have taken.
When i run my application with visual studio IDE, the application runs fine. When i run directly the executable it is not running fine.
My app creates two forms. First form losts focus when the second form is about to create. The first form UI frezzes when i run directly executable.
Please give any suggestions how to debug the problem.
Regards
Raju
Setting the First form property Topmost to true solved the prolem.