Share standard calculator using vb.net - step,

nguyenhuu.minh

New member
#calculator #vb.net #VisualBasic #Programming #development ## Hướng dẫn từng bước để tạo máy tính tiêu chuẩn bằng VB.NET

Hướng dẫn từng bước này sẽ chỉ cho bạn cách tạo một máy tính đơn giản bằng VB.NET.Chúng tôi sẽ bao gồm những điều cơ bản về việc tạo giao diện người dùng, xử lý đầu vào của người dùng và thực hiện các tính toán.

## 1. Tạo một dự án mới

Để bắt đầu, mở Visual Studio và tạo một dự án mới.Chọn ** Tệp **> ** Mới **> ** Dự án **.Trong hộp thoại ** New Project **, chọn ** Visual Basic **> ** Ứng dụng Windows Forms **.Đặt tên cho dự án "Máy tính" và nhấp vào ** OK **.

## 2. Thêm điều khiển người dùng

Chúng tôi sẽ tạo một điều khiển người dùng để giữ giao diện người dùng của máy tính.Nhấp chuột phải vào thư mục ** Dự án ** trong Giải pháp Explorer và chọn ** Thêm **> ** Mục mới **.Trong hộp thoại ** Thêm mục mới **, chọn ** Điều khiển người dùng **.Đặt tên cho điều khiển "Máy tính điều khiển" và nhấp vào ** Thêm **.

Máy tính sẽ chứa các điều khiển sau:

* Một điều khiển nhãn để hiển thị tính toán hiện tại
* Một điều khiển hộp văn bản để nhập số
* Điều khiển nút để xóa hộp văn bản
* Điều khiển nút để thực hiện tính toán

## 3. Thêm trình xử lý sự kiện

Chúng tôi cần thêm trình xử lý sự kiện cho các điều khiển nút để máy tính có thể thực hiện các tính toán và xóa hộp văn bản.

Bấm đúp vào nút ** Xóa ** để mở ** C# Designer **.Trong cửa sổ ** Thuộc tính **, xác định vị trí sự kiện ** nhấp ** và nhấp đúp vào nó.Điều này sẽ tạo ra một phương thức xử lý sự kiện trống.

Trong phương thức xử lý sự kiện, thêm mã sau:

`` `
Me.textbox1.text = ""
`` `

Mã này xóa hộp văn bản.

Bấm đúp vào nút ** Tính ** để mở Trình thiết kế ** C# **.Trong cửa sổ ** Thuộc tính **, xác định vị trí sự kiện ** nhấp ** và nhấp đúp vào nó.Điều này sẽ tạo ra một phương thức xử lý sự kiện trống.

Trong phương thức xử lý sự kiện, thêm mã sau:

`` `
Dim số1 dưới dạng đôi
Dim số 2 dưới dạng gấp đôi
Toán tử mờ dưới dạng chuỗi

'Nhận các số và toán tử từ giao diện người dùng.
Number1 = double.parse (me.textbox1.text)
toán tử = me.combobox1.text
number2 = double.parse (me.textbox2.text)

'Thực hiện tính toán.
Nếu toán tử = "+" thì
result = number1 + number2
Otherif Toán tử = "-" Sau đó
result = number1 - number2
Otherif toán tử = "*" sau đó
result = number1 * number2
Otherif toán tử = "/" sau đó
result = number1 / number2
Kết thúc nếu

'Hiển thị kết quả trong giao diện người dùng.
Me.textbox3.text = result.toString ()
`` `

Mã này nhận được các số và toán tử từ giao diện người dùng, thực hiện tính toán và hiển thị kết quả trong hộp văn bản.

## 4. Chạy dự án

Để chạy dự án, nhấn ** F5 **.Máy tính sẽ được hiển thị trong trình gỡ lỗi Visual Studio.

Bạn có thể nhập số vào hộp văn bản và chọn toán tử từ ComboBox.Máy tính sẽ thực hiện tính toán và hiển thị kết quả trong hộp văn bản.

## 5. Tóm tắt

Trong hướng dẫn này, bạn đã học cách tạo một máy tính đơn giản bằng VB.NET.Bạn đã học cách tạo giao diện người dùng, xử lý đầu vào của người dùng và thực hiện các tính toán.

## Tài nguyên bổ sung

* [Hướng dẫn vb.net] (https://www.tutorialspoint.com/vbnet/index.htm)
* [Lập trình cơ bản trực quan] (https://www.w3schools.com/vb/default.asp)
* [Điều khiển máy tính] (XML sub-tree processing in .NET Framework 1.1)
=======================================
#calculator #vb.net #VisualBasic #Programming #development ##Step-by-Step Guide to Create a Standard Calculator Using VB.NET

This step-by-step guide will show you how to create a simple calculator using VB.NET. We'll cover the basics of creating a user interface, handling user input, and performing calculations.

## 1. Create a New Project

To get started, open Visual Studio and create a new project. Select **File** > **New** > **Project**. In the **New Project** dialog box, select **Visual Basic** > **Windows Forms Application**. Name the project "Calculator" and click **OK**.

## 2. Add a User Control

We'll create a user control to hold the calculator's user interface. Right-click the **Project** folder in Solution Explorer and select **Add** > **New Item**. In the **Add New Item** dialog box, select **User Control**. Name the control "CalculatorControl" and click **Add**.

The CalculatorControl will contain the following controls:

* A Label control to display the current calculation
* A TextBox control to enter numbers
* A Button control to clear the TextBox
* A Button control to perform the calculation

## 3. Add Event Handlers

We need to add event handlers for the Button controls so that the calculator can perform calculations and clear the TextBox.

Double-click the **Clear** button to open the **C# Designer**. In the **Properties** window, locate the **Click** event and double-click it. This will create an empty event handler method.

In the event handler method, add the following code:

```
Me.Textbox1.Text = ""
```

This code clears the TextBox.

Double-click the **Calculate** button to open the **C# Designer**. In the **Properties** window, locate the **Click** event and double-click it. This will create an empty event handler method.

In the event handler method, add the following code:

```
Dim number1 As Double
Dim number2 As Double
Dim operator As String

' Get the numbers and operator from the user interface.
number1 = Double.Parse(Me.Textbox1.Text)
operator = Me.ComboBox1.Text
number2 = Double.Parse(Me.Textbox2.Text)

' Perform the calculation.
If operator = "+" Then
result = number1 + number2
ElseIf operator = "-" Then
result = number1 - number2
ElseIf operator = "*" Then
result = number1 * number2
ElseIf operator = "/" Then
result = number1 / number2
End If

' Display the result in the user interface.
Me.Textbox3.Text = result.ToString()
```

This code gets the numbers and operator from the user interface, performs the calculation, and displays the result in the TextBox.

## 4. Run the Project

To run the project, press **F5**. The calculator will be displayed in the Visual Studio debugger.

You can enter numbers in the TextBox and select an operator from the ComboBox. The calculator will perform the calculation and display the result in the TextBox.

## 5. Summary

In this tutorial, you learned how to create a simple calculator using VB.NET. You learned how to create a user interface, handle user input, and perform calculations.

## Additional Resources

* [VB.NET Tutorial](https://www.tutorialspoint.com/vbnet/index.htm)
* [Visual Basic Programming](https://www.w3schools.com/vb/default.asp)
* [Calculator Controls](https://www.codeproject.com/Articles/12824/Calculator-Control-for-Windows-Forms)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top