Share vb.net openfiledialog

hoanvu27

New member
### vb.net OpenFileDialog

### Cách sử dụng OpenFileDialog trong vb.net

Điều khiển OpenFileDialog cho phép bạn mở tệp từ máy tính của người dùng.Đây là một hộp thoại phương thức hiển thị danh sách các tệp và thư mục và cho phép người dùng chọn một cái để mở.

Để sử dụng điều khiển OpenFileDialog, trước tiên bạn cần thêm nó vào biểu mẫu của mình.Bạn có thể làm điều này bằng cách kéo điều khiển từ hộp công cụ vào biểu mẫu của bạn.

Khi điều khiển được thêm vào biểu mẫu của bạn, bạn cần đặt các thuộc tính của nó.Thuộc tính quan trọng nhất là thuộc tính ** Ban đầu **.Thuộc tính này chỉ định thư mục rằng điều khiển OpenFileDialog sẽ hiển thị khi nó mở đầu tiên.

Bạn cũng có thể đặt thuộc tính bộ lọc ** ** để chỉ định các loại tệp mà người dùng có thể chọn.Ví dụ: mã sau đặt thuộc tính bộ lọc để chỉ hiển thị các tệp văn bản:

`` `VBNet
OpenFileDialog1.filter = "Text Files (*.txt) |*.txt"
`` `

Khi bạn đã đặt các thuộc tính của điều khiển OpenFileDialog, bạn có thể hiển thị nó bằng cách gọi phương thức ** showDialog **.Phương pháp này sẽ trả về một giá trị hộp thoại.Nếu người dùng nhấp vào ** OK **, giá trị hộp thoại sẽ là DIEMOGRESULT.OK.Nếu người dùng nhấp vào ** Hủy **, giá trị hộp thoại sẽ là DIEMOGRESULT.CANCEL.

Sau đó, bạn có thể sử dụng thuộc tính ** FileName ** của điều khiển OpenFileDialog để lấy đường dẫn của tệp mà người dùng đã chọn.

Mã sau đây cho thấy cách sử dụng điều khiển OpenFileDialog để mở tệp:

`` `VBNet
Dim FilePath dưới dạng chuỗi

'Đặt các thuộc tính của điều khiển OpenFileDialog.
OpenFileDialog1.initialdirectory = "C: \ Users \ myname \ Documents"
OpenFileDialog1.filter = "Text Files (*.txt) |*.txt"

'Hiển thị điều khiển OpenFileDialog.
Lấy động vật = OpenFileDialog1.ShowDialog ()

'Nếu người dùng nhấp vào OK, hãy lấy đường dẫn của tệp đã được chọn.
Nếu DIALOGRESULT = DIALOGRESULT.OK thì
filePath = OpenFileDialog1.filename
Kết thúc nếu

'Sử dụng đường dẫn của tệp để mở tệp.
File.open (FilePath, Filemode.open)
`` `

### Bài viết tham khảo

* [Cách sử dụng điều khiển OpenFileDialog trong vb.net] (https://www.tutorialspoint.com/vbnet/vbnet_openfiledialog_control.htm
* [Điều khiển OpenFileDialog trong vb.net] (OpenFileDialog Class (System.Windows.Forms))

### hashtags

* #vb.net
* #Openfiledialog
* #FileDialog
* #Windows Forms
* #Programming
=======================================
### VB.NET OpenFileDialog

### How to use OpenFileDialog in VB.NET

The OpenFileDialog control allows you to open a file from the user's computer. It is a modal dialog box that displays a list of files and folders, and allows the user to select one to open.

To use the OpenFileDialog control, you first need to add it to your form. You can do this by dragging the control from the Toolbox onto your form.

Once the control is added to your form, you need to set its properties. The most important property is the **InitialDirectory** property. This property specifies the folder that the OpenFileDialog control will display when it first opens.

You can also set the **Filter** property to specify the types of files that the user can select. For example, the following code sets the Filter property to show only text files:

```vbnet
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"
```

Once you have set the properties of the OpenFileDialog control, you can display it by calling the **ShowDialog** method. This method will return a DialogResult value. If the user clicks **OK**, the DialogResult value will be DialogResult.OK. If the user clicks **Cancel**, the DialogResult value will be DialogResult.Cancel.

You can then use the **FileName** property of the OpenFileDialog control to get the path of the file that the user selected.

The following code shows how to use the OpenFileDialog control to open a file:

```vbnet
Dim filePath As String

' Set the properties of the OpenFileDialog control.
OpenFileDialog1.InitialDirectory = "C:\Users\MyName\Documents"
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"

' Display the OpenFileDialog control.
DialogResult = OpenFileDialog1.ShowDialog()

' If the user clicked OK, get the path of the file that was selected.
If DialogResult = DialogResult.OK Then
filePath = OpenFileDialog1.FileName
End If

' Use the path of the file to open the file.
File.Open(filePath, FileMode.Open)
```

### Reference articles

* [How to use the OpenFileDialog control in VB.NET](https://www.tutorialspoint.com/vbnet/vbnet_openfiledialog_control.htm)
* [The OpenFileDialog control in VB.NET](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog?view=net-6.0)

### Hashtags

* #vb.net
* #Openfiledialog
* #FileDialog
* #Windows Forms
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top