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).

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
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

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

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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);
        }
    }
}

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é!

Được phân loại:

Được gắn thẻ: