visual studio open windows form from console application c# [duplicate] - c#

This question already has answers here:
how to run a winform from console application?
(10 answers)
Closed 8 years ago.
The title is a bit vague, but my problem is this. In Microsoft visual studio im making a console application in c#, and in the code i wanted to be able to launch a windows form inside the same project. In visual basic this can be done with Application.run(formName), however if done in c# using a refrence, the windows form oppens but then immediately stops responding. What is the correct way to open a form from a console app?
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number");
int x = int.Parse(Console.ReadLine());
if (x == 3)
{
menu menuInstance = new menu(); //menu would be the name of the windows form
menuInstance.Show();
}
Console.ReadKey();
}
}
}

Use the same code a WindowsForms project does, Application.Run is still the way to go:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Hard to say what you did wrong with your original attempt at that, as you didn't post that code.

Related

How to make windows form app resize correctly [duplicate]

This question already has answers here:
Creating a DPI-Aware Application
(9 answers)
Closed 2 years ago.
I'm developing a windows form GUI, and came up with this tricky problem: When windows resolution is set to 100%, everything is normal, Good Sample:
whereas when windows resolution is set to 150%, the graphical displays as well as the texts failed to autosize correctly, blocks messed up with each other, Bad sample:
Any advice would be helpful! Thanks.
Thanks guys and shout out to Ken White I've found my solution here: Creating a DPI-Aware Application
In short I added following and it worked:
namespace myApplication
{
static class Program
{
[STAThread]
static void Main()
{
// ***this line is added***
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
// ***also dllimport of that function***
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
}
}

c# Visual Studio not showing debug string [duplicate]

This question already has answers here:
How can I execute code after my form starts?
(5 answers)
Closed 5 years ago.
I am fairly new to C# but I feel like this should output "hi":
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace PathMet_Controller
{
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Debug.WriteLine("hi");
}
}
}
My output window gives me this:
In playing around, it did output "hi" once, but it would only output after I stop running the file.
Because Application.Run will start special loop which will end when you exit your win forms application.
If you want to print something at start, use events provided by Form class.

C# - How to change the Startup form Programmatically? [duplicate]

This question already has an answer here:
Changing startup form in C#
(1 answer)
Closed 6 years ago.
Is it possible to change the startup form programmatically, because when my application starts it asks for the serial key and I want it to only show up ONCE and when the user enters the serial number and presses the continue button, it will check if the serial number is correct and take him to the main form.
But how is it possible to change the startup Form after the user enters serial number?
For example i want it to lead them to Form1 instead of TrialCheck.
I'm using Visual Studio 2015 C#.
Your project has a Program.cs file which is the entry point to your program.
In its Main() method you should perform your validation and then decide which form to load.
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool registered = FunctionThatChecksSerialNumber();
if (registered)
{
Application.Run(new Form1());
}
else
{
Application.Run(new TrialCheck());
}
}
}

C# Monogame - passing arguments on startup

So I want to have a winforms GUI for my game... But I can't seem to find a GUI lib that is tested to work with Monogame. That's why I have another plan:
Create a WinForms Project to act as a startup for my game.
Then, when the user clicks "Start Game" in that project's main form, the winforms project should look for my Monogame project executable and start it, then the form should close.
However I want to pass an argument from the WinForms exe to the Monogame exe. This has the purpose of somewhat preventing users from starting the game through the Monogame exe and use the Winforms "Starter" instead. Which leads to step #4.
If no argument is passed to the Monogame project it should close.
So should I do it like in console applications, pass a string array to the main void of the Monogame app like this:
using System;
namespace myGame
{
#if WINDOWS || LINUX
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
using (var game = new Game1())
game.Run();
if (args[0] != "startFromWinForms")
{
// Find a way to close or
// on the other hand I think
// it will throw an exception??
}
}
}
#endif
}
How would you do that? What approach do you recommend? Also code examples/ideas explained would be great!
Let's take a step back and look at your original requirement.
I want to have a winforms GUI for my game ... when the user clicks "Start Game" [the game will start]
I just tried a simple solution that I think solves your problem without needing to create 2 projects. All I had to do was:
Create a new MonoGame Windows project.
Add a WinForms form (Form1) to it.
Write the following code..
In the Main() method:
[STAThread]
static void Main()
{
var form = new Form1();
if (form.ShowDialog() == DialogResult.OK)
{
using (var game = new Game1())
game.Run();
}
}
And in the Form1 button click event:
private void StartGameButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
And that's it. You don't need to pass anything on the command line. You'll only have 1 exe and it meets all of the requirements. MonoGame works exactly as it does now and the WinForms code stays isolated from the game.
Like this?
[STAThread]
static void Main(string[] args)
{
if (args[0] == "startFromWinForms")
{
using (var game = new Game1())
game.Run();
}
}
If you have an argiment, you will start your game, otherwise nothing happens

How to get c# to run windows form functions alongside console app

I am using c# and have a console application. Now, to integrate GUI, I added a windows form application. Thing is, the windows form and the console application need to communicate but the functions built inside the console app don't work till the windows form is closed.
Its like the windows form is overlapping it.
From what you write I think I understand that you write your console application code after Application.Run(new Form1());
and so your code probably looks like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
//Some more things to do here
}
}
This is not the way to go. a project can be either a console app. or a winforms project.
If you need them both to run together , use two projects.
If you need them to communicate , use a communication object, such as WCF framework server/client.
You can control the lunch behaviour by simply going to Solution property page, Common properties,Start up project. Over there you can specify which program to run first in the action section.
Create a new console project and add a reference to System.Windows.Forms. Add a new form to your project. As an example, I added one button to the form and set it's DialogResult to Ok.
In your program's main method, create an instance of your form and open it using Show or ShowDialog. Here's an example:
static void Main(string[] args)
{
Console.WriteLine("Opening window...");
var result = new TestForm().ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
Console.WriteLine("Form closed by button.");
else
Console.WriteLine("Form closed otherwise.");
Console.ReadLine();
}
From your form any location within your program you can use the static Console class to access the console. Here's an example that prints some status info from the form's constructor:
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
Console.WriteLine("Form initialized.");
}
}
If you already have created your project as class library or windows forms project, right-click it, navigate to properties and switch the "Output type" to "Console application".

Categories