I would like to spawn a Windows form from the console using C#. Roughly like display
does in Linux, and modify its contents, etc. Is that possible?
You need to create a Windows application. Then right click the project and go to properties and set the Output Type for the application to Console Application. This will then not only show your form but also the Console Window. So now you can use the Console class to read in or display messages to that console Window.
Windows Forms (and WPF) are both not supported on Linux with . NET Core and probably never will be. Winforms runs on mono, but that is based on . NET Framework, not .
Forms. Integration namespace is defined in 'WindowsFormsIntegration. dll' which currently ships in the WinFX SDK, not in the standard redist. Therefore, the file will be found in '\Program Files\Reference Assemblies\Microsoft\Framework\v3.
You should be able to add a reference for System.Windows.Forms and then be good to go. You may also have to apply the STAThreadAttribute to the entry point of your application.
using System.Windows.Forms;
class Program
{
[STAThread]
static void Main(string[] args)
{
MessageBox.Show("hello");
}
}
... more complex ...
using System.Windows.Forms;
class Program
{
[STAThread]
static void Main(string[] args)
{
var frm = new Form();
frm.Name = "Hello";
var lb = new Label();
lb.Text = "Hello World!!!";
frm.Controls.Add(lb);
frm.ShowDialog();
}
}
Yes, you can initialize a form in the Console. Add a reference to System.Windows.Forms and use the following sample code:
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ShowDialog();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With