c#图形,如何在文本框中输入内容后,点击确定按钮,将文本输入的内容打印出来,在控制台打印就行

2024-11-20 16:49:16
推荐回答(1个)
回答1:

namespace WinFormWithConsole
{
    using System;
    using System.Drawing;       // 由于该程序本身是控制台程序,所以需要添加System.Drawing组件引用
    using System.Windows.Forms; // 需要添加System.Windows.Forms组件引用,
    class Program
    {
         private static Form frmInput=null;
         private static System.Windows.Forms.TextBox textBox1 = new TextBox();
         private static System.Windows.Forms.Button button1 = new Button();
        static void Main(string[] args)
        {
            frmInput= new Form();
            textBox1.Location = new System.Drawing.Point(125, 43);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(120, 21);
            textBox1.TabIndex = 0;
            button1.Location = new System.Drawing.Point(136, 84);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(98, 31);
            button1.TabIndex = 1;
            button1.Text = "button1";
            button1.UseVisualStyleBackColor = true;
            button1.Click += (x, y) =>
            {
                if (!String.IsNullOrWhiteSpace(textBox1.Text)) {
                    Console.WriteLine(textBox1.Text.Trim());
                }
            };

            frmInput.Controls.Add(textBox1);
            frmInput.Controls.Add(button1);

            Application.Run(frmInput);
            Console.WriteLine();
        }
    }
}