0

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!

oowekyala
  • 7,518
  • 1
  • 15
  • 20
DevelopedLogic
  • 165
  • 2
  • 9

3 Answers3

1

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.

Rob
  • 181
  • 5
1

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.

Oswald
  • 1,252
  • 20
  • 32
  • this appears to work almost perfectly, however the only issue is that when doing `window.Show()` and the code after it continues, the window that was opened locks up and you cannot interact with it. – DevelopedLogic Oct 26 '17 at 06:23
  • Have you tried starting the window in a new thread? – Oswald Oct 26 '17 at 06:25
  • I see how this would work and i'm certain it would work even without me having tested it as long as I use ShowDialog(); however what is the point of Show(); if it locks up due to continued code execution, in a thread or not? – DevelopedLogic Oct 26 '17 at 06:51
0

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?

Emond
  • 50,210
  • 11
  • 84
  • 115