Mã nguồn system window form c

If your project includes Windows Forms, you can use JetBrains Rider's visual Windows Forms designer and also create new Windows Forms projects.

Create Windows Forms projects

You can create a new project in a new solution using or add a new project to the existing solution by right-clicking the solution or solution folder node in the Solution Explorer, and choosing .

Choose Desktop Application project template and then Desktop Application as the project type.

Mã nguồn system window form c

Create and edit Windows Forms

You can add Windows Forms items in any project which have a reference to System.Windows.Forms. To add a new Windows Form item, right-click the project in the Solution Explorer and choose Add. You will be then able to select one of the Windows Forms templates — Windows Form, User Control, or Component. Windows Forms items are displayed with the corresponding icons in the Solution Explorer tree:

Mã nguồn system window form c

When you double-click a Windows Form item, it opens in a new editor tab and additionally opens the Designer Toolbox window.

Switch between the designer and the source code

  • Press Shift+F7 to switch to the designer and F7 to view the source code.

  • Use the Designer and Code tabs at the bottom of the editor.

When working in the designer view, you can select the desired component in the Designer Toolbox and then without dragging it, draw a rectangle area on the canvas where the component should be added.

If you decide not to add the component that you clicked, click the Pointer item in the components view.

Edit components on the canvas

  • Select components with a single click.

  • Use its adorners to move and resize the selected components.

  • Use the property grid and the event list to manipulate available properties like Text, Items, and Fonts, and subscribe to available events.

  • Double-click the component to add the default event handler. For example, it will add the Click event handler for a button.

  • Use Ctrl+X, Ctrl+C, paste Ctrl+V shortcuts to cut, copy, and paste components within the canvas and also between multiple Windows Forms items.

Mã nguồn system window form c

By default, the Designer Toolbox window lists standard Windows Forms components. To add add custom components to the toolbox, click Manage Components and then select the desired components there. All components from Global Assembly Cache and installed NuGet packages will be available for selection automatically. To load component assemblies from disk, click Add

Mã nguồn system window form c
in the left part of the dialog and pick the desired assembly.

Mã nguồn system window form c

Last modified: 17 March 2022

Mã nguồn system window form c

1. Mở đầu

Để thực hiện bài viết hướng dẫn, mình sẽ tạo ra một ứng dụng dạng Windows Form App (.NET Framework) đơn giản trên Visual Studio.

2. Các bước thực hiện

+ Đầu tiên, mình đã tạo ra 1 form có tên là MainForm và trong form sẽ có 1 button (btnOpenURL).

Mã nguồn system window form c
Tạo một form đơn giản.

+ Tiếp theo, mình sẽ tạo ra một sự kiện cho button đã tạo (btnOpenURL) như sau:

private void btnOpenURL_Click(object sender, EventArgs e)
{
  string url = "https://www.phanxuanchanh.com"; // URL cần được mở trên trình duyệt.
  Process.Start(url); // Thực hiện mở URL trên trình duyệt.
}

Sau khi thực hiện xong các bước trên, khi bạn chạy chương trình và nhấn vào button Mở URL (btnOpenURL) thì trình duyệt sẽ được mở và truy cập vào URL mà bạn đã chỉ định.

Lưu ý: Việc mở URL trên trình duyệt nào sẽ phụ thuộc vào thiết lập của bạn, cụ thể là trình duyệt nào đang được thiết lập làm trình duyệt mặc định.

* Toàn bộ code của form đã tạo (MainForm)

+ Phần code giao diện:

namespace OpenURL
{
    partial class MainForm
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.btnOpenURL = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // btnOpenURL
            // 
            this.btnOpenURL.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.btnOpenURL.Location = new System.Drawing.Point(95, 35);
            this.btnOpenURL.Name = "btnOpenURL";
            this.btnOpenURL.Size = new System.Drawing.Size(152, 33);
            this.btnOpenURL.TabIndex = 0;
            this.btnOpenURL.Text = "Mở URL";
            this.btnOpenURL.UseVisualStyleBackColor = false;
            this.btnOpenURL.Click += new System.EventHandler(this.btnOpenURL_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(337, 117);
            this.Controls.Add(this.btnOpenURL);
            this.Name = "MainForm";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button btnOpenURL;
    }
}

+ Phần code xử lý:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace OpenURL
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnOpenURL_Click(object sender, EventArgs e)
        {
            string url = "https://www.phanxuanchanh.com";
            Process.Start(url);
        }
    }
}

3. Các bài viết liên quan

Nếu các bạn quan tâm đến các bài viết về ngôn ngữ lập trình C# thì có thể đọc các bài viết sau đây:

  • Lập trình C# – Dịch ngược mã nguồn với ILSpy.
  • Lập trình C# – Xây dựng trò chơi Kéo Búa Bao không dùng tới câu lệnh if, switch case, biểu thức tam phân.
  • Lập trình C# – Kết nối cơ sở dữ liệu SQL Server với ADO.NET phần 1.
  • Lập trình C# – Xây dựng hàm băm PBDKF2.
  • Lập trình C# – Gửi Email đơn giản bằng giao thức SMTP.

4. Lời kết

Thông qua bài viết này, mình đã thực hiện một chương trình siêu đơn giản, để từ đó, hướng dẫn bạn cách để thực hiện mở một URL trên trình duyệt trong Windows Form App (C#). Tóm lại, để mở URL trên trình duyệt thì chỉ cần sử dụng Process.Start(url). Quá đơn giản phải không nào?

Cuối cùng, cảm ơn các bạn đã đọc bài viết, nếu các bạn có vấn đề cần giải đáp, hãy để lại bình luận cho mình nhé!

Hits: 123