Share c# using

mongdiepcoochie

New member
C #để tạo một máy tính đơn giản ## C #, #calculator, #Programming, #tutorial, #Code

** Cách tạo một máy tính đơn giản trong C#**

Máy tính là một công cụ phổ biến mà chúng ta sử dụng trong cuộc sống hàng ngày.Chúng có thể được sử dụng để thực hiện một loạt các hoạt động toán học, từ việc bổ sung đơn giản và trừ đến các tính toán phức tạp hơn như lượng giác và tính toán.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo một máy tính đơn giản trong C#.

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

Để tạo một dự án C# mới, hãy mở Visual Studio và chọn ** Tệp> MỚI> Dự án **.Trong hộp thoại ** mới **, chọn ** Visual C#> Windows> Ứng dụng bảng điều khiển **.Đặt tên cho dự án ** Máy tính ** và nhấp vào ** OK **.

### 2. Thêm `system.windows.forms` không gian tên

Không gian tên `system.windows.forms` chứa các lớp mà chúng ta cần tạo giao diện người dùng đồ họa (GUI) cho máy tính của chúng tôi.Để thêm không gian tên này vào dự án của bạn, nhấp chuột phải vào thư mục ** Tài liệu tham khảo ** trong trình thám hiểm giải pháp và chọn ** Thêm tham chiếu **.Trong hộp thoại ** Thêm tham chiếu **, chọn **. Net Framework ** và sau đó chọn ** system.windows.forms ** không gian tên.Nhấp vào ** OK ** để thêm không gian tên vào dự án của bạn.

### 3. Tạo giao diện người dùng

Giao diện người dùng cho máy tính của chúng tôi sẽ bao gồm một hộp văn bản, một nút cho mỗi thao tác toán học và một nút để xóa hộp văn bản.Để tạo hộp văn bản, hãy kéo điều khiển `textbox` từ hộp công cụ ** ** vào biểu mẫu.Để tạo các nút, hãy kéo điều khiển `nút` vào biểu mẫu cho mỗi thao tác toán học.Cuối cùng, kéo điều khiển `nút` vào biểu mẫu cho nút rõ ràng.

### 4. Viết mã

Mã sau đây hiển thị mã mà bạn cần ghi để tạo máy tính:

`` `C#
// Tạo hộp văn bản.
TextBox TextBox = new TextBox ();
TextBox.location = New Point (10, 20);
TextBox.width = 200;
TextBox.Height = 20;
TextBox.Textalign = Horizontalalign.right;
this.controls.add (hộp văn bản);

// Tạo các nút.
Nút nút = nút new ();
nútadd.Text = "+";
BUTTERADD.Location = New Point (10, 50);
Butteradd.width = 50;
Butteradd.Height = 20;
this.controls.add (nútDD);

Nút nútSubTract = new Butter ();
nút
NútSubTract.location = New Point (70, 50);
nút
nút nút.height = 20;
this.controls.add (nútSubTract);

Nút nútMultiply = new Butter ();
nútMultiply.Text = "*";
nútMultiply.location = New Point (130, 50);
nútMultiply.width = 50;
NútMultiply.Height = 20;
this.controls.add (nútMultiply);

Nút Buttondivide = Nút mới ();
Buttondivide.Text = "/";
Buttondivide.location = điểm mới (190, 50);
Buttondivide.width = 50;
Buttondivide.height = 20;
this.controls.add (Buttondivide);

Nút nút = nút new ();
NútClear.Text = "Xóa";
nútclear.location = điểm mới (10, 80);
NútClear.width = 200;
NútClear.Height = 20;
this.controls.add (nútclear);

// Xử lý các sự kiện nhấp chuột cho các nút.
Buttadd.Click += Đại biểu {
// Nhận văn bản từ hộp văn bản.
Chuỗi văn bản = TextBox.Text;

// Thêm số hiện tại vào hộp văn bản.
TextBox.Text = text + " +";
};

nút
// Nhận văn bản từ văn bản
=======================================
C# to create a simple calculator ##C#, #calculator, #Programming, #tutorial, #Code

**How to Create a Simple Calculator in C#**

Calculators are a common tool that we use in our everyday lives. They can be used to perform a variety of mathematical operations, from simple addition and subtraction to more complex calculations such as trigonometry and calculus. In this tutorial, we will show you how to create a simple calculator in C#.

### 1. Create a new C# project

To create a new C# project, open Visual Studio and select **File > New > Project**. In the **New Project** dialog box, select **Visual C# > Windows > Console Application**. Name the project **Calculator** and click **OK**.

### 2. Add the `System.Windows.Forms` namespace

The `System.Windows.Forms` namespace contains the classes that we need to create a graphical user interface (GUI) for our calculator. To add this namespace to your project, right-click on the **References** folder in the Solution Explorer and select **Add Reference**. In the **Add Reference** dialog box, select **.NET Framework** and then select the **System.Windows.Forms** namespace. Click **OK** to add the namespace to your project.

### 3. Create the user interface

The user interface for our calculator will consist of a text box, a button for each mathematical operation, and a button to clear the text box. To create the text box, drag a `TextBox` control from the **Toolbox** onto the form. To create the buttons, drag a `Button` control onto the form for each mathematical operation. Finally, drag a `Button` control onto the form for the clear button.

### 4. Write the code

The following code shows the code that you need to write to create the calculator:

```c#
// Create the text box.
TextBox textBox = new TextBox();
textBox.Location = new Point(10, 20);
textBox.Width = 200;
textBox.Height = 20;
textBox.TextAlign = HorizontalAlignment.Right;
this.Controls.Add(textBox);

// Create the buttons.
Button buttonAdd = new Button();
buttonAdd.Text = "+";
buttonAdd.Location = new Point(10, 50);
buttonAdd.Width = 50;
buttonAdd.Height = 20;
this.Controls.Add(buttonAdd);

Button buttonSubtract = new Button();
buttonSubtract.Text = "-";
buttonSubtract.Location = new Point(70, 50);
buttonSubtract.Width = 50;
buttonSubtract.Height = 20;
this.Controls.Add(buttonSubtract);

Button buttonMultiply = new Button();
buttonMultiply.Text = "*";
buttonMultiply.Location = new Point(130, 50);
buttonMultiply.Width = 50;
buttonMultiply.Height = 20;
this.Controls.Add(buttonMultiply);

Button buttonDivide = new Button();
buttonDivide.Text = "/";
buttonDivide.Location = new Point(190, 50);
buttonDivide.Width = 50;
buttonDivide.Height = 20;
this.Controls.Add(buttonDivide);

Button buttonClear = new Button();
buttonClear.Text = "Clear";
buttonClear.Location = new Point(10, 80);
buttonClear.Width = 200;
buttonClear.Height = 20;
this.Controls.Add(buttonClear);

// Handle the click events for the buttons.
buttonAdd.Click += delegate {
// Get the text from the text box.
string text = textBox.Text;

// Add the current number to the text box.
textBox.Text = text + " + ";
};

buttonSubtract.Click += delegate {
// Get the text from the text
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top