0

OK, let's start with I do not need: Show console window in forms application like in this video https://www.youtube.com/watch?v=YHo_W2PvVWs is not what I am looking for.

I have created WinForms application, that has got several small application inside like: temperature converter, calculator and other. Now I want to have a button, that will call console and in there there already will be a logic for console application like here: https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-console?view=vs-2019.

The main point is to start console application (with already imbedded code) in WinForms?

MikeRyz
  • 209
  • 1
  • 3
  • 18
  • Are you asking how to start an .exe application or how to embed another (console) application within your WinForms? If the latter, what are you trying to achieve? That console window will not display the console output from your main application (by default at least). – Xerillio Dec 31 '20 at 10:16
  • Some possibilities: [This one](https://stackoverflow.com/questions/27707433/run-or-embed-vb-net-console-application-within-a-vb-net-or-c-sharp-forms-applica) or [this one](https://stackoverflow.com/questions/5836176/docking-window-inside-another-window). – Idle_Mind Dec 31 '20 at 17:03

2 Answers2

0

if i get well, You can use method like this , also can post command for cmd and you can change the type WindowStyle = ProcessWindowStyle.Hidden, for example i didnt try but you can, give some cmd code in string like this link below

https://www.wikihow.com/Make-a-Command-Prompt-Calculator

  public int cmdCommandWork (string command, int timeout)
    {
       ProcessStartInfo test = new ProcessStartInfo("cmd.exe", "/C " + command)
       {
         WindowStyle = ProcessWindowStyle.Hidden,
         CreateNoWindow = true,
         UseShellExecute = false,
         WorkingDirectory = @"C:\\"
        };
        Process process = Process.Start(test);
        process.WaitForExit(timeout);
        int exitCode = process.ExitCode;
        process.Close();
        return exitCode;
    }
  • Thanks for the answer. And I want my WinForm to has its own Console app popup when the some button is hit. – MikeRyz Jan 04 '21 at 10:38
0

I did find the answer here: https://zombievdk.clan.su/news/urok_7_console_v_proekte_windows_forms/2016-07-07-33

I added the new Console method to my WinForm App:

public partial class NativeMethods

    {
        public static Int32 STD_OUTPUT_HANDLE = -11;

        [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "GetStdHandle")]
        public static extern System.IntPtr GetStdHandle(Int32 nStdHandle);

        [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "AllocConsole")]

        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]

        public static extern bool AllocConsole();
    }

And put the body of the code to another method public void consondeb() that calls the first one like that:

public void consondeb() {

        if (NativeMethods.AllocConsole())
        {

            IntPtr stdHandle = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);

//Body of the code

}

}

For more information please see the github source code( remember to Start without debugging or just hit Ctrl + F5: https://github.com/mikepowertech79/Csharp-console-Car-Store-App

MikeRyz
  • 209
  • 1
  • 3
  • 18