溫馨提示×

c# winform如何創建窗口

c#
小樊
156
2024-09-02 04:32:00
欄目: 編程語言

在C#中,使用WinForms創建窗口需要以下幾個步驟:

  1. 創建一個新的Windows Forms應用程序項目。

  2. 在解決方案資源管理器中,雙擊“Form1.cs”以打開設計器。

  3. 若要向窗口添加控件,請打開工具箱并將所需的控件(例如按鈕、文本框等)拖放到窗口上。

  4. 為控件添加事件處理程序,例如雙擊按鈕以生成按鈕單擊事件的代碼。

  5. 在事件處理程序中編寫代碼以實現所需的功能。

以下是一個簡單的示例,該示例創建一個包含一個按鈕和一個文本框的窗口。當單擊按鈕時,文本框中顯示“Hello, World!”。

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Button button1;
        private TextBox textBox1;

        public Form1()
        {
            InitializeComponent();

            // 創建一個按鈕
            button1 = new Button();
            button1.Location = new System.Drawing.Point(50, 50);
            button1.Text = "點擊我";
            button1.Click += new EventHandler(button1_Click);
            this.Controls.Add(button1);

            // 創建一個文本框
            textBox1 = new TextBox();
            textBox1.Location = new System.Drawing.Point(50, 100);
            this.Controls.Add(textBox1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Hello, World!";
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

這個示例展示了如何在C# WinForms中創建一個簡單的窗口。你可以根據自己的需求修改代碼,添加更多的控件和功能。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女