-1

I am new to programming (c#). I have seen in VBScript we can show the message box (standalone). I would like to do the same in C#, just for learning, but the code when executed shows a Console or Form in the background along with a messagebox. Is it possible to only show the messagebox when the program is executed?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageBox.Show("My message");
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
msvs.vb.net
  • 1
  • 1
  • 2
  • 3
    Possible duplicate of [Hide Console Window in C# Console Application](https://stackoverflow.com/questions/3853629/hide-console-window-in-c-sharp-console-application) – Ray May 22 '19 at 04:24
  • 1
    I just created a new Windows Forms application and replaced the entire code in `Program.cs` and it works as intended - no form window is ever shown, just the messagebox. I even deleted the auto-created `Form1.cs` and the program still builds and runs as expected. Your namespace suggests you created a Console Application - create a new Windows application instead. – Marc.2377 May 22 '19 at 04:27
  • it works, thank you. – msvs.vb.net May 22 '19 at 04:33

1 Answers1

1

For Console projects, you can hide the console as follows:

  • Right click the project in the Solution Explorer
  • Select Properties
  • Go the the Application tab in the project properties.
  • Change the Output Type combo box from "Console Application" to "Windows Application".
Ray
  • 7,940
  • 7
  • 58
  • 90
  • thank you, it did work in Console App. Is the same possible in Windows Forms App? – msvs.vb.net May 22 '19 at 04:26
  • 1
    The difference to a Windows Forms app are only some added assembly references and the call to `Application.Run(new FormXyz())` in the `Program.cs` file - you'd have to remove that call to not create a main window (but then you can just go with a Console application anyway), or make the Form hide itself after having been created in various ways. – Ray May 22 '19 at 04:29