Share simple calculator using vb.net - step,

smallfrog241

New member
#vb.net, #calculator, #Programming, #tutorial, #Simple ** Máy tính đơn giản bằng VB.NET - Hướng dẫn từng bước **

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu cách tạo một máy tính đơn giản bằng VB.NET.Chúng tôi sẽ đề cập đến các chủ đề sau:

* Tạo một dự án mới trong Visual Studio
* Thêm kiểm soát người dùng vào dự án
* Mã hóa điều khiển người dùng
* Thêm điều khiển người dùng vào biểu mẫu
* Chạy ứng dụng

## Tạo một dự án mới trong Visual Studio

Để tạo một dự án mới trong Visual Studio, hãy mở ** Visual Studio ** IDE và nhấp vào menu ** tệp **.Sau đó, chọn ** Mới **> ** Dự án **.

Trong hộp thoại ** New Project **, chọn tab ** Visual Basic ** và chọn ứng dụng ** Windows Forms ** Loại dự án.Đặt tên cho dự án ** Máy tính đơn giản ** và nhấp vào ** OK **.

## Thêm kiểm soát người dùng vào dự án

Kiểm soát người dùng là một loại điều khiển có thể được sử dụng để tạo giao diện người dùng tùy chỉnh.Để thêm điều khiển người dùng vào dự án, nhấp chuột phải vào thư mục ** Dự án ** trong ** Solution Explorer ** và chọn ** Thêm **> ** Mục mới **.

Trong hộp thoại ** Thêm mục mới **, chọn mẫu điều khiển người dùng ** ** và đặt tên cho điều khiển ** calculatorControl **.Nhấp vào ** Thêm ** để tạo điều khiển người dùng.

## Mã hóa điều khiển người dùng

Kiểm soát người dùng sẽ chứa mã cho máy tính.Để thêm mã, nhấp đúp vào tệp ** calculatorControl.vb ** trong ** Solution Explorer **.

Mã sau đây hiển thị mã hoàn chỉnh cho điều khiển người dùng:

`` `VBNet
Nhập khẩu System.Windows.Forms

Máy tính lớp công khai
Thực hiện icomponent

'Các điều khiển hộp văn bản sẽ hiển thị các số và kết quả.
Riêng txtnumber1 dưới dạng hộp văn bản
Private txtNumber2 dưới dạng hộp văn bản
TXTRESULT riêng làm hộp văn bản

'Các điều khiển nút sẽ thực hiện các tính toán.
Btnadd riêng tư như nút
BTNSubTract riêng tư như nút
Btnmultiply riêng tư như nút
Btndivide riêng như nút

'Trình xây dựng cho điều khiển người dùng.
Công khai Sub mới ()
Khởi tạo ()
Kết thúc phụ

'Phương thức khởi tạo được gọi tự động bởi Visual Studio để tạo giao diện người dùng của người dùng.
Sub InitizeComponent ()
'Tạo các điều khiển hộp văn bản.
txtNumber1 = new TextBox ()
txtNumber2 = new TextBox ()
txtresult = new TextBox ()

'Tạo các điều khiển nút.
btnadd = new Butter ()
BTNSubTract = new Butter ()
btnmultiply = new Butter ()
btndivide = new Butter ()

'Thêm các điều khiển vào điều khiển người dùng.
Control.add (txtnumber1)
Control.add (txtnumber2)
Điều khiển.ADD (TXTRESULT)
Control.ADD (BTNADD)
Control.ADD (BTNSubTract)
Điều khiển.add (btnmultiply)
Điều khiển.add (btndivide)

'Đặt các thuộc tính của các điều khiển.
txtnumber1.location = new Point (10, 10)
txtnumber1.size = new Kích thước (100, 20)
txtNumber2.Location = new Point (120, 10)
txtNumber2.size = new Kích thước (100, 20)
txtresult.location = new Point (230, 10)
txtresult.size = kích thước mới (100, 20)
btnadd.location = new Point (10, 40)
btnadd.size = kích thước mới (50, 20)
BTNSubTract.location = New Point (70, 40)
btnsubTract.size = new Kích thước (50, 20)
btnmultiply.location = new Point (130, 40)
btnmultiply.size = kích thước mới (50
=======================================
#vb.net, #calculator, #Programming, #tutorial, #Simple **Simple Calculator Using VB.NET - Step by Step Tutorial**

In this tutorial, we will learn how to create a simple calculator using VB.NET. We will cover the following topics:

* Creating a new project in Visual Studio
* Adding a user control to the project
* Coding the user control
* Adding the user control to the form
* Running the application

## Creating a New Project in Visual Studio

To create a new project in Visual Studio, open the **Visual Studio** IDE and click the **File** menu. Then, select **New** > **Project**.

In the **New Project** dialog box, select the **Visual Basic** tab and choose the **Windows Forms Application** project type. Name the project **Simple Calculator** and click **OK**.

## Adding a User Control to the Project

A user control is a type of control that can be used to create custom user interfaces. To add a user control to the project, right-click the **Project** folder in the **Solution Explorer** and select **Add** > **New Item**.

In the **Add New Item** dialog box, select the **User Control** template and name the control **CalculatorControl**. Click **Add** to create the user control.

## Coding the User Control

The user control will contain the code for the calculator. To add the code, double-click the **CalculatorControl.vb** file in the **Solution Explorer**.

The following code shows the complete code for the user control:

```vbnet
Imports System.Windows.Forms

Public Class CalculatorControl
Implements IComponent

' The TextBox controls that will display the numbers and the result.
Private txtNumber1 As TextBox
Private txtNumber2 As TextBox
Private txtResult As TextBox

' The Button controls that will perform the calculations.
Private btnAdd As Button
Private btnSubtract As Button
Private btnMultiply As Button
Private btnDivide As Button

' The constructor for the user control.
Public Sub New()
InitializeComponent()
End Sub

' The InitializeComponent method is called automatically by Visual Studio to create the user control's UI.
Private Sub InitializeComponent()
' Create the TextBox controls.
txtNumber1 = New TextBox()
txtNumber2 = New TextBox()
txtResult = New TextBox()

' Create the Button controls.
btnAdd = New Button()
btnSubtract = New Button()
btnMultiply = New Button()
btnDivide = New Button()

' Add the controls to the user control.
Controls.Add(txtNumber1)
Controls.Add(txtNumber2)
Controls.Add(txtResult)
Controls.Add(btnAdd)
Controls.Add(btnSubtract)
Controls.Add(btnMultiply)
Controls.Add(btnDivide)

' Set the properties of the controls.
txtNumber1.Location = New Point(10, 10)
txtNumber1.Size = New Size(100, 20)
txtNumber2.Location = New Point(120, 10)
txtNumber2.Size = New Size(100, 20)
txtResult.Location = New Point(230, 10)
txtResult.Size = New Size(100, 20)
btnAdd.Location = New Point(10, 40)
btnAdd.Size = New Size(50, 20)
btnSubtract.Location = New Point(70, 40)
btnSubtract.Size = New Size(50, 20)
btnMultiply.Location = New Point(130, 40)
btnMultiply.Size = New Size(50
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top