Bài tập lập trình windows có lời giải

ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm THỰC HÀNH C# [Có hướng dẫn chi tiết ] I. Nội dung 1. Tiếp cận môi trường lập trình C# - IDE: Microsoft Visual Studio 2015 2. Lập trình với ngôn ngữ C# ở mức cơ bản: - Đọc hiểu chương trình C# ở mức cơ bản - Nắm các kiểu dữ liệu trong C# Kiểu dữ liệu nguyên thủy: int, long, double, char…. Kiểu dữ liệu tham chiếu: Array, List, Class … - Nắm được các cấu trúc điều khiển trong lập trình Cấu trúc điều khiển tuần tự Cấu trúc điều khiển rẽ nhánh if Cấu trúc điều khiển rẽ nhánh if… else Cấu trúc điều khiển vòng lặp - Nắm được các kỹ thuật xử lý trên mảng 1 chiều Trang 1 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm II. Ví dụ 1. Chương trình xuất chữ “Hello World” 2. namespace HelloWorld 3. { 4. class Program 5. { 6. static void Main[string[] args] 7. { 8. Console.WriteLine["Hello World"]; 9. } 10. } 11. } 2. Viết chương trình nhập 2 số nguyên và tính tổng 2 số đó [Sử dụng Windows Form Application] Class Form 1 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1[] { InitializeComponent[]; } private void button1_Click[object sender, EventArgs e] { string s1Text = s1.Text; string s2Text = s2.Text; int s1Int = int.Parse[s1Text]; int s2Int = int.Parse[s2Text]; long sumLong = 0; sumLong = s1Int + s2Int; sum.Text = sumLong.ToString[]; } } } Form1.Designer.cs namespace WindowsFormsApplication1 { partial class Form1 Trang 2 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm { private void InitializeComponent[] { this.s1 = new System.Windows.Forms.TextBox[]; this.s2 = new System.Windows.Forms.TextBox[]; this.label1 = new System.Windows.Forms.Label[]; this.label2 = new System.Windows.Forms.Label[]; this.sumBtn = new System.Windows.Forms.Button[]; this.sum = new System.Windows.Forms.TextBox[]; this.label3 = new System.Windows.Forms.Label[]; this.label4 = new System.Windows.Forms.Label[]; this.SuspendLayout[]; // // s1 // this.s1.Location = new System.Drawing.Point[176, 102]; this.s1.Multiline = true; this.s1.Name = "s1"; this.s1.Size = new System.Drawing.Size[163, 33]; this.s1.TabIndex = 0; // // s2 // this.s2.Location = new System.Drawing.Point[176, 174]; this.s2.Multiline = true; this.s2.Name = "s2"; this.s2.Size = new System.Drawing.Size[163, 35]; this.s2.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font["Microsoft Sans Serif", 13F]; this.label1.Location = new System.Drawing.Point[44, 113]; this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size[102, 22]; this.label1.TabIndex = 2; this.label1.Text = "Số thứ nhất"; this.label1.Click += new System.EventHandler[this.label1_Click]; // // label2 // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font["Microsoft Sans Serif", 13F]; this.label2.Location = new System.Drawing.Point[44, 187]; this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size[91, 22]; this.label2.TabIndex = 3; this.label2.Text = "Số thứ hai"; // // sumBtn // this.sumBtn.Location = new System.Drawing.Point[365, 129]; this.sumBtn.Name = "sumBtn"; this.sumBtn.Size = new System.Drawing.Size[83, 47]; this.sumBtn.TabIndex = 4; this.sumBtn.Text = "Tính"; this.sumBtn.UseVisualStyleBackColor = true; this.sumBtn.Click += new System.EventHandler[this.button1_Click]; // // sum // Trang 3 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm this.sum.Location = new System.Drawing.Point[176, 251]; this.sum.Multiline = true; this.sum.Name = "sum"; this.sum.Size = new System.Drawing.Size[163, 35]; this.sum.TabIndex = 5; // // label3 // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font["Microsoft Sans Serif", 13F]; this.label3.Location = new System.Drawing.Point[44, 264]; this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size[82, 22]; this.label3.TabIndex = 6; this.label3.Text = "Kết quả: "; // // label4 // this.label4.AutoSize = true; this.label4.Font = new System.Drawing.Font["Microsoft Sans Serif", 16F]; this.label4.Location = new System.Drawing.Point[86, 34]; this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size[313, 26]; this.label4.TabIndex = 7; this.label4.Text = "TÍNH TỔNG HAI SỐ NGUYÊN"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF[6F, 13F]; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size[500, 329]; this.Controls.Add[this.label4]; this.Controls.Add[this.label3]; this.Controls.Add[this.sum]; this.Controls.Add[this.sumBtn]; this.Controls.Add[this.label2]; this.Controls.Add[this.label1]; this.Controls.Add[this.s2]; this.Controls.Add[this.s1]; this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout[false]; this.PerformLayout[]; }

endregion private private private private private private private private } System.Windows.Forms.TextBox s1; System.Windows.Forms.TextBox s2; System.Windows.Forms.Label label1; System.Windows.Forms.Label label2; System.Windows.Forms.Button sumBtn; System.Windows.Forms.TextBox sum; System.Windows.Forms.Label label3; System.Windows.Forms.Label label4; } Trang 4 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm Bài tập có gợi ý: Bài 1: Viết chương trình nhập vào 2 số a và b, cho biết số lớn nhất và số nhỏ nhất trong 2 số a, b với giao diện như sau: Hướng dẫn gợi ý: Sử dụng cấu trúc If… else. using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace _2soab { public partial class Form1 : Form { public Form1[] { InitializeComponent[]; Trang 5 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm } private void textBox3_TextChanged[object sender, EventArgs e] { } private void textBox2_TextChanged[object sender, EventArgs e] { } private void label2_Click[object sender, EventArgs e] { } private void button1_Click[object sender, EventArgs e] { int a = Convert.ToInt32[textA.Text]; int b = Convert.ToInt32[textB.Text]; int max = 0; if [a > max] max = a; if [b > max] max = b; int min = a; if [b < min] min = b; textMax.Text = Convert.ToString[max]; textMin.Text = Convert.ToString[min]; } } } Bài 2: Viết chương trình nhập vào 3 số a , b và c cho biết số lớn nhất và số nhỏ nhất trong 3 số a, b, c với giao diện như sau: Trang 6 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN using using using using using using using using using Khoa Công Nghệ Phần Mềm System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace lonbe3abc { public partial class Form1 : Form { public Form1[] { InitializeComponent[]; } private void textBox4_TextChanged[object sender, EventArgs e] { } private void label5_Click[object sender, EventArgs e] { } private { int int int void button1_Click[object sender, EventArgs e] a = Convert.ToInt32[textA.Text]; b = Convert.ToInt32[textB.Text]; c = Convert.ToInt32[textC.Text]; int max if [a > max if [b > max if [c > max = 0; max] = a; max] = b; max] = c; int min if [b < min if [c < min = a; min] = b; min] = c; textMax.Text = Convert.ToString[max]; textMin.Text = Convert.ToString[min]; } } Trang 7 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm } Bài 3: Nhập vào một số nguyên từ 0 đến 9, hiển thị bằng chữ với các số trên. Ví dụ: - Nhập 1: “Một” - Nhập 2: “Hai” - Nhập 3: “Ba” ……………… - using using using using using using using using using Nhập 9: “Chín” System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace bai03 { public partial class Form1 : Form { public Form1[] { InitializeComponent[]; } private void label2_Click[object sender, EventArgs e] { Trang 8 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm } private void button1_Click[object sender, EventArgs e] { int a = Convert.ToInt32[textnhap.Text]; switch[a] { case 1: richTextdoc.Text="mot"; break; case 2: richTextdoc.Text="hai"; break; case 3: richTextdoc.Text="ba"; break; case 4: richTextdoc.Text="bon"; break; case 5: richTextdoc.Text="nam"; break; case 6: richTextdoc.Text="sau"; break; case 7: richTextdoc.Text="bay"; break; case 8: richTextdoc.Text="tam"; break; case 9: richTextdoc.Text="chim"; break; default : richTextdoc.Text="nhap sai"; break; } } } } Hướng dẫn gợi ý: Sử dụng cấu trúc switch…. case Bài 4: Viết chương trình gồm 1 form trong đó có một button và 1 label hiển thị số lầ khi nhấn button đó. Trang 9 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN using using using using using using using using using Khoa Công Nghệ Phần Mềm System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace bai04 { public partial class Form1 : Form { public Form1[] { InitializeComponent[]; } int dem = 0; private void button1_Click[object sender, EventArgs e] { dem++; doc.Text = "bạn đã click : " + dem; } } } Bài 5: Viết chương trình nhập vào giá trị nguyên dương N, tính tổng S = 1 + 2 + 3 + 4 + ……. + N Với giao diện như sau: using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; Trang 10 ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Khoa Công Nghệ Phần Mềm using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace bai05 { public partial class Form1 : Form { public Form1[] { InitializeComponent[]; } private { int int for { void button1_Click[object sender, EventArgs e] s = 0, i ; a = Convert.ToInt32[textnhap.Text]; [i = 0; i

Chủ Đề