Developing C# Visual Studio .exes similar to Java [duplicate] - c#

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?

Related

How do I open an executable as a MDI child?

I have a parent form that starts another application. When the application starts it should set the application as an mdichild of parent.
I have managed to get this to work with notepad using the code below.
Process proc = new Process();
proc.StartInfo.FileName = "notepad.exe";
proc.Start();
proc.WaitForInputIdle();
SetParent(proc.MainWindowHandle, this.panel.Handle);
The problem is that it doesn't work with all applications. Specifically it doesn't work with other win forms applications developed in c#, which is what I need it for.
Any help is appreciated, Thanks.
Edit: I have access to the code for the c# app i want to open as a mdi child.
OP: I have access to the code for the c# app i want to open as a mdi
child.
To open a form of other .Net windows application, you don't need to use SetParent and you can simply add a reference of that .Net Application (dll, exe or project) to your current project and then create an instance of the main form of that application and show it like other forms of your application.
If you have access to project of that application, you can add it to your current solution and use add a reference to your current project.
If you don't have access to project of the project of that application, you can add a reference to its exe/dll to your current project.
To learn more about add reference, read How to: Add or Remove References By Using the Add Reference Dialog Box
add .exe(for example another.exe) file as reference
add another.exe references // may be differ from your mdi form
add below code
another.Form1 fr = new another.Form1(); //another.exe has Form1 if not select form.
fr.MdiParent = this;
fr.Show(); // Display the new form.

Visual Studio - End debugger only when all forms have been closed

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
{
}

Get Application that runs a Windows Form Control

I'm making an Autocad Plugin which runs fine using Windows Forms
And I've created a user control (winforms) to replicate in any form I like in the future.
The question is
From the control's code, how do I get the instance of the application running that control?
(Probably a pure winforms problem)
Coditions:
Within the plug-in I can get the Autocad Application instance with no
problem.
This user control is meant to be in a separate assembly (dll) to be referenced in the plug-in application, so it has no direct access to the application instance.
A little explanation about the scenario:
There's a Main Assembly being run by Autocad as a plug-in.
That assembly has the Autocad application instantiated.
Now I have some useful form controls to work with Autocad, and they are in a separate assembly. (That's because I want to use them in as many different plug-ins I like).
So, Autocad runs main assembly, and main assembly runs controls of the separate assembly.
In order to work properly, those controls need to have access to the Autocad application wich is running the main assembly.
Today I use the application as a property in the controls, wich I must set before working with them. (If I forget to set that, exceptions are raised). Since I cannot have a control with a creator taking parameters.
I want the controls to detect their running application so I avoid that workaround.
Please see the following code
public class MyCommands {
[CommandMethod("NS", "TEST", "TEST", CommandFlags.Modal)]
public void TestCommand() // This method can have any name
{
Form fromAutoCADAPI = new TestForm();
Form independent1 = new TestForm();
Form independent2 = new TestForm();
//Using AutoCAD application
Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(fromAutoCADAPI);
independent1.Show();
independent2.Show();
//Using Windows Forms Application
var count = System.Windows.Forms.Application.OpenForms.Count; //should be 3
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(count.ToString());
}
}
If this is what you already know, then may be you should paste some sample code that will help understand where specifically you are stuck in your code. This is the way I have used AutoCAD application and Windows Forms application. In case you wan't to grab WPF application then you can use
var application = System.Windows.Application.Current;

Develop a program that runs in the background in .NET?

I have made a small program in C# that I want to run in the background and it should only appear when a certain key combination is pressed. How can I do this?
There are at least three ways to do this:
Classic Windows Service application. "Creating a Basic Windows Service in C#" article from CodeProject will help you. In that case you use System.ServiceProcess namespace. BTW, in that case you should read "System.ServiceProcess Namespace" article from MSDN. Here is a short quote from it:
The System.ServiceProcess namespace provides classes that allow you to implement, install, and control Windows service applications. Services are long-running executables that run without a user interface.
Memory-Resident Program. But this is almost impossible to do with C#. Use C++ or better C for this purpose, if you want. If you want to search by yourself, just use keyword TSR.
Last one is a dirty one. Just create a formless C# application and try to hide it from Task Manager.
To allow the program to be completely invisible is, in my opinion, a bad idea. Because the user cannot interact with the program.
I would recommend placing it in the SysTray (an icon by the clock in Windows)
trayIcon = new NotifyIcon();
trayIcon.Text = "My application";
trayIcon.Icon = TheIcon
// Add menu to the tray icon and show it.
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
To monitor keyboard you can use LowLevel Keyboard hook ( see example ) or attach a hootkey (See example)
Create a windows form application, and delete Form1
Modify program.cs Application.Run(new Form1()); to Application.Run();
You can create a Windows Service Application. It runs as a background process. No user interface. This can also start automatically when the computer boots. You can see the rest of the background processes in Task Manager or you can type in services.msc in Command Prompt.
This might help. http://msdn.microsoft.com/en-us/library/9k985bc9%28v=vs.80%29.aspx
A quick and dirty solution (I think the Window Service Application template is unavailable in Visual Studio Express and Standard):
Start a new Windows Forms Application.
Add a new class to the solution and write the code you want inside it.
Go back to the form designer, set the WindowState property to Minimized, and add a Load event to the form. In the event handler hide the form and call your class:
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
MyNewClass mynewclass=new MyNewClass();
}
The application doesn't appear in the taskbar and you don't see it when you hit Alt+Tab. You can add a systray icon to it if you want, just like magol wrote:
NotifyIcon trayIcon = new NotifyIcon();
trayIcon.Icon=new Icon(#"C:\iconfilename.ico");
trayIcon.Visible = true;
If you really want to create a program that really run in background, try to create a Windows service.
Its there if when you create a new project

Windows aplication that lives only in the taskbar

I'm new to windows programming (any win 32 API).
I want to create a windows application that listens to the clipborad all the time and reacts to keyboard shortcuts (for example you copy text from the ClipBoard and press Ctrl-F and something is done on the text in the clipboard).
I know how to make a window app in win32 and in C# (.NET) but it's a windowed application which has a window and appears in the window panel.
I want an pplication that will only be visible in the taskbar right part so you can close it (like most antivirus do) and keeps on running from start will it's closed.
Anyone got some code template, or can reffer me to a tutorial ? i don't mind if it's C\C+ or C#.
10x.
It can be another windows application in which the form's ShowInTaskbar property is set to false and you add a NotifyIcon component to put it into tray. Here is an article from CodeProject. You can find many more on codeproject or on other programming related sites.
You may find this useful, although it's in C.
Or this in C#.

Categories