简体   繁体   中英

In C#, how can I print something to the console after clicking a button from Windows Form?

I'm using Visual Studio 2019 and Windows Form (.NET Framework), I have a windows form that has a button. I want to print something to the console after clicking this button named 'btnPrint'. I don't know what codes to put in it.

I tried Console.WriteLine("Hello World!") but no console was shown. I wait for a few minutes hoping that something shows up, but it takes a long time, so I terminate the program.

This is my code:

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

  namespace Windows
  {
      public partial class Form1 : Form
      {
            public Form1()
            {
                 InitializeComponent();
            }

            private void btnPrint_Click(object sender, EventArgs e)
            {
                 Console.WriteLine("Hello World!");
            }
      }
   }

It looks like you're running a Windows Forms application. By default, the Console you're trying to access with Console.WriteLine() is not available. This command only works for Console Applications (See the description in the official documentation ).

If you're running your application inside of Visual Studio you should see the Hello World! message in Visual Studio's output window.

Some ways to add output to your code:

AllocConsole

If you would really want a Console to be open for your Forms application. You can have a look at this answer . This will open the Console so you can use it. However, if you close the console, your whole application will close.

This is the code, copied from the answer linked above:

using System.Runtime.InteropServices;

private void Form1_Load(object sender, EventArgs e)
{
    // This will open up the console when the form is loaded.
    AllocConsole();
}

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

RichTextBox

Another way to get sort of a Console for your Form application is to add an output to the form itself, like a RichTextBox . And write your message to the RichTextBox like so:

// Print the text
MyRichTextBox.AppendText("Hello World!\n" + output);
// Scroll the RichTextBox down
MyRichTextBox.ScrollToCaret();

Debug log

Instead of Console.WriteLine() you can also use Debug.WriteLine from System.Diagnostics if you attach a debugger like Visual Studio to the Form application. The output will be shown in the output window of Visual Studio.

This will work even after you've build the application.

Console Application

You could also create a Console application so the console will always be up. In the Console application you can then create a new Form to do all your From interactions with.
It's a bit of a workaround, but it should work.

You should make a label like this,

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

namespace Windows
{
    public partial class Form1 : Form
    {
        public Form1()
        {
                InitializeComponent();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
                Label Show_Text = new Label();
                Show_Text.Text = "Hello World!";
                Form1.Controls.Add(Show_Text);
        }
    }
}

Other preferences on c# Labels go to more about c# labels...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM