Hộp thoại hiện thị OpenFileDialog C#

Hướng dẫn cách sử dụng Hộp thoại hiện thị OpenFileDialog C#

  1. OpenFileDialog là gì ?
  2. Cách sử dụng cơ bản OpenFileDialog
  3. Các thuộc tính quan trọng OpenFileDialog
  4. Một số ví dụ sử dụng OpenFileDialog trong Csharp
  5. Những lưu ý khi sử dụng OpenFileDialog
  6. Tài liệu chính thức OpenFileDialog

1. OpenFileDialog là gì ?

OpenFileDialog là một lớp trong namespace System.Windows.Forms, dùng để hiển thị hộp thoại chọn tệp chuẩn của Windows, cho phép người dùng chọn tệp để mở trong ứng dụng WinForms.

2. Cách sử dụng cơ bản OpenFileDialog

OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    string filePath = openFileDialog.FileName;
    // Xử lý filePath (ví dụ đọc nội dung tệp)
}

3. Các thuộc tính quan trọng OpenFileDialog

Thuộc tính Kiểu dữ liệu Chức năng
FileName string Lấy đường dẫn đầy đủ của tệp đã chọn.
Filter string Giới hạn kiểu tệp hiển thị, ví dụ: `"Text files (*.txt)
Title string Tiêu đề của hộp thoại.
Multiselect bool Cho phép chọn nhiều tệp cùng lúc.
InitialDirectory string Thư mục hiển thị ban đầu.
DefaultExt string Phần mở rộng mặc định.
CheckFileExists bool Mặc định true. Nếu false, cho phép nhập tên tệp không tồn tại.

 

4. Một số ví dụ sử dụng OpenFileDialog trong Csharp

Ví dụ 1: Mở một tệp .txt và hiển thị nội dung với OpenFileDialog

private void btnChonFile_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "Chọn tệp văn bản";
    ofd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string path = ofd.FileName;
        string content = File.ReadAllText(path);
        MessageBox.Show(content, "Nội dung tệp");
    }
}

Ví dụ 2: Chọn nhiều tệp cùng lúc với OpenFileDialog

private void btnChonNhieuFile_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    ofd.Filter = "All files (*.*)|*.*";

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string[] selectedFiles = ofd.FileNames;
        foreach (string file in selectedFiles)
        {
            MessageBox.Show("Tệp được chọn: " + file);
        }
    }
}

Ví dụ 3: Mở ảnh và hiển thị trong PictureBox với OpenFileDialog

private void btnChonAnh_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Image Files (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png";

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.Image = Image.FromFile(ofd.FileName);
    }
}

5. Những lưu ý khi sử dụng OpenFileDialog

    ShowDialog() là phương thức chặn (blocking), nó sẽ dừng chương trình cho đến khi người dùng đóng hộp thoại.
    Luôn kiểm tra DialogResult == DialogResult.OK trước khi xử lý tệp.
    Có thể thêm kiểm tra tồn tại tệp với File.Exists(path) nếu cần chắc chắn.

6. Tài liệu chính thức OpenFileDialog

Tham khảo [Khóa học Winform cơ bản]

 

 

Tìm kiếm:

Hướng dẫn cách sử dụng Hộp thoại hiện thị OpenFileDialog C#