Share Làm việc với Console Application và chờ người dùng nhấn phím trong VB.NET

tranduy.khang

New member
## Cách chờ đầu vào của người dùng trong Ứng dụng bảng điều khiển VB.NET

Trong hướng dẫn này, bạn sẽ tìm hiểu cách chờ đầu vào của người dùng trong ứng dụng bảng điều khiển VB.NET.Bạn sẽ tìm hiểu cách sử dụng phương thức `Console.ReadKey ()` để lấy ký tự từ bàn phím và cách sử dụng phương thức `Console.Readline ()` để lấy một dòng văn bản từ bàn phím.

### 1. Nhận một ký tự từ bàn phím

Để có được một ký tự từ bàn phím, bạn có thể sử dụng phương thức `Console.ReadKey ()`.Phương thức này lấy tham số `boolean` chỉ định có nên đợi người dùng nhấn phím hay không.Nếu tham số là `true`, phương thức sẽ chặn cho đến khi người dùng nhấn phím.Nếu tham số là `false`, phương thức sẽ trả về ngay lập tức, ngay cả khi không nhấn phím.

Mã sau đây cho thấy cách sử dụng phương thức `Console.ReadKey ()` để lấy ký tự từ bàn phím:

`` `VBNet
Dim ch như char

ch = console.ReadKey (true)

'Làm gì đó với nhân vật
`` `

### 2. Nhận một dòng văn bản từ bàn phím

Để nhận một dòng văn bản từ bàn phím, bạn có thể sử dụng phương thức `Console.Readline ()`.Phương thức này sẽ chặn cho đến khi người dùng nhấn phím Enter.

Mã sau đây cho thấy cách sử dụng phương thức `Console.Readline ()` để lấy một dòng văn bản từ bàn phím:

`` `VBNet
Dim str như chuỗi

str = console.Readline ()

'Làm điều gì đó với chuỗi
`` `

### 3. Ví dụ

Mã sau đây hiển thị một ví dụ hoàn chỉnh về ứng dụng bảng điều khiển VB.NET chờ đợi đầu vào của người dùng.Ứng dụng in một thông báo vào bảng điều khiển và sau đó chờ người dùng nhấn phím.Khi người dùng nhấn phím, ứng dụng sẽ in ký tự được nhấn vào bảng điều khiển.

`` `VBNet
Mô -đun mô -đun1

Sub main ()
'In tin nhắn vào bảng điều khiển
Console.WriteLine ("Nhấn phím để tiếp tục.")

'Chờ người dùng nhấn phím
Dim ch as char = console.ReadKey (true)

'In ký tự được nhấn vào bảng điều khiển
Console.WriteLine ("Bạn đã nhấn ký tự {0}.", Ch)
Kết thúc phụ

Mô -đun kết thúc
`` `

### 4. Kết luận

Trong hướng dẫn này, bạn đã học cách chờ đợi đầu vào của người dùng trong ứng dụng bảng điều khiển VB.NET.Bạn đã học cách sử dụng phương thức `Console.ReadKey ()` để lấy ký tự từ bàn phím và cách sử dụng phương thức `Console.Readline ()` để lấy một dòng văn bản từ bàn phím.

### 5. Hashtags

* #vb.net
* #Ứng dụng giao diện điều khiển
* #UserInput
* #Keyboard
* #Đọc khóa
=======================================
## How to Wait for User Input in VB.NET Console Application

In this tutorial, you will learn how to wait for user input in a VB.NET console application. You will learn how to use the `Console.ReadKey()` method to get a character from the keyboard, and how to use the `Console.ReadLine()` method to get a line of text from the keyboard.

### 1. Getting a Character from the Keyboard

To get a character from the keyboard, you can use the `Console.ReadKey()` method. This method takes a `Boolean` parameter that specifies whether to wait for the user to press a key. If the parameter is `True`, the method will block until the user presses a key. If the parameter is `False`, the method will return immediately, even if no key is pressed.

The following code shows how to use the `Console.ReadKey()` method to get a character from the keyboard:

```vbnet
Dim ch As Char

ch = Console.ReadKey(True)

' Do something with the character
```

### 2. Getting a Line of Text from the Keyboard

To get a line of text from the keyboard, you can use the `Console.ReadLine()` method. This method will block until the user presses the Enter key.

The following code shows how to use the `Console.ReadLine()` method to get a line of text from the keyboard:

```vbnet
Dim str As String

str = Console.ReadLine()

' Do something with the string
```

### 3. Example

The following code shows a complete example of a VB.NET console application that waits for user input. The application prints a message to the console, and then waits for the user to press a key. When the user presses a key, the application prints the character that was pressed to the console.

```vbnet
Module Module1

Sub Main()
' Print a message to the console
Console.WriteLine("Press a key to continue.")

' Wait for the user to press a key
Dim ch As Char = Console.ReadKey(True)

' Print the character that was pressed to the console
Console.WriteLine("You pressed the character {0}.", ch)
End Sub

End Module
```

### 4. Conclusion

In this tutorial, you learned how to wait for user input in a VB.NET console application. You learned how to use the `Console.ReadKey()` method to get a character from the keyboard, and how to use the `Console.ReadLine()` method to get a line of text from the keyboard.

### 5. Hashtags

* #vb.net
* #ConsoleApplication
* #UserInput
* #Keyboard
* #ReadKey
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top