thực hành visual basic 6 0

26 3.1K 0
thực hành visual basic 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 1 / 26 Bài 1: Điều khiển Animation ( Hình động) Điều khiển Animation cho phép hiển thị các đoạn phim .AVI Để sử dụng trớc hết ta thêm điều khiển Animation vào Toolbox thông qua hộp thoại Components \ Microsoft Windows Common Control 2.60 Bổ sung điều khiển Anim ation vào form, tiếp đó bổ sung lên form hai nút bấm lên form. Điều khiển Thuộc tính Điều khiển Thuộc tính Animation Name: Animation1 Name: frmMain Name: CmdPlay Form Caption: Animation Command BorderStyle: Fixed Dialog Name: CmdStop Command Trong sự kiện cmdPlay _Click() và cmdStop_Click() nhập code nh sau: Dim str As String Private Sub cmdPlay_Click() str = "C:\Program Files\Microsoft Visual Studio \Common\Graphics\Videos\filecopy.avi" Animation1.Open str Animation1.Play End Sub Private Sub cmdStop_Click() Animation1.Stop End Sub Tiến hành chạy chơng trình bằng cách nhấn phím F5 Bài 2: Điều khiển Progress bar (Thanh tiến trình) Để sử dụng trớc hết ta thêm điều khiển Progress Bar vào Toolbox thông qua hộp thoại Components \ Microsoft Windows Common Controls 6.0 Bổ sung điều khiển Progress Bar vào form, tiếp đó bổ sung lên form hai điều khiển là Label và Timer Điều khiển Thuộc tính ProgressBar Name: ProgressBar1 Max: 100 Min: 0 Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 2 / 26 Timer Name: Timer1 Interval: 300 Label Name: Label1 Caption: Chơng trình đang khởi động Trong sự kiện Form_Load() và Timer1_Timer() nhập code nh sau: Private Sub Form_Load() ProgressBar1.Value = ProgressBar1.Min End Sub Private Sub Timer1_Timer() ProgressBar1.Value = ProgressBar1. Value + 5 If ProgressBar1.Value >= ProgressBar1.Max Then Timer1.Enabled = False Unload Form1 End If End Sub Tiến hành chạy chơng trình Bài 3: Điều khiển Progress bar (Thanh tiến trình) dùng PictureBox Để sử dụng trớc hết ta thêm điều khiển Progress Bar vào Toolbox thông qua hộp thoại Components \ Microsoft Windows Common Controls 6.0 Bổ sung điều khiển Progress Bar vào form, tiếp đó bổ sung lên form hai điều khiển là Label và Timer Điều khiển Thuộc tính Điều khiển Thuộc tính Picture1 Name: Picture1 Backcolor: chọn màu trắng Label Name: Label1 Caption: Program is loading Picture2 Name: Picture2 Backcolor: chọn màu đỏ Form Name: form1 Caption: Progressbar Timer Name: Timer1 Interval: 30 Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 3 / 26 Trong sự kiện Timer1_Timer() nhập code nh sau: Private Sub Timer1_Timer() Picture2.Width = Picture2.Width + 40 If Picture2.Width = Picture1.Width Then Picture1.Visible = False Timer1.Interval = 0 Unload frmlogin End If End Sub Tiến hành chạy chơng trình Bài 4: Sử dụng điề u khiển textbox và nút bấm Command để tạo hộp thoại Login Trớc hết tạo một form. Sau đó bổ sung thêm 2 điều khiển textbox, 2 nhãn label và 2 nút bấm Command lên form. Điều khiển Thuộc tính Điều khiển Thuộc tính Form Name: frmlogin Caption: Login textbox Name: txtName text: Label Name: Label1 Caption: Ngời sử dụng textbox Name: txtName text: Label Name: Label2 Caption: Mật khẩu Command Name: cmdOk Caption: &Chấp nhận Command Name: cmdexit Caption: &Hủy bỏ Ta mở trang soạn thảo lệnh chơng trình và viết mã lệnh cho các sự kiện Private Sub cmdexit_Click() Unload frmlogin End Sub 'doan code de kiem tra tai khoan nguoi dung Private Sub cmdok_Click() If txtname.Text = "" And txtpassword.Text = "" Then MsgBox "Ban chua nhap thong tin nguoi dung!" txtname.SetFocus Else Bµi tËp thùc hµnh m«n: ng«n ng÷ visual basic Bé m«n : c«ng nghÖ phÇn mÒm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 4 / 26 If txtname.Text = "" Or txtpassword.Text = "" Then MsgBox "Ban phai nhap du thong tin !" If txtname.Text = "" Then txtname.SetFocus If txtpassword.Text = "" Then txtpassword.SetFocus Else If UCase(Trim(txtname.Text)) = "ADMIN" And UCase(Trim(txtpassword.Text)) = "123456" Then MsgBox "Chuc mung ban da dang nhap thanh cong !" Unload frmlogin Else MsgBox "Ban nhap sai thong tin, hay nhap lai !" txtname.Text = "" txtpassword.Text = "" txtname.SetFocus End If End If End If End Sub 'doan code sau lam doi mau doi tuong kho con tro hoi tu va mat hoi tu Private Sub txtname_GotFocus() txtname.BackColor = QBColor(11) End Sub Private Sub txtname_LostFocus() txtname.BackColor = QBColor(15) End Sub Private Sub txtpassword_GotFocus() txtpassword.BackColor = QBColor(11) End Sub Private Sub txtpassword_LostFocus() txtpassword.BackColor = QBColor(15) End Sub 'doan code sau xu ly su kien nguoi su dung nhan phim ENTER Private Sub txtname_KeyPress(KeyAscii As Integer) Select Case Key Ascii Case 13 txtpassword.SetFocus End Select End Sub Private Sub txtpassword_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 13 cmdok.SetFocus End Select End Sub  Thùc hiÖn ch¬ng tr×nh. Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 5 / 26 Bài 5: Thiết kế chơng trình tính diện tích tam giác. Form nh sau: Bảng thuộc tính nh sau: Điều khiển Thuộc tính Điều khiển Thuộc tính Điều khiển Thuộc tính Name: lblA Name: cmdHuy Name: txtA Label Caption: Cạnh A Command Caption: &Hủy bỏ Textbox Text: Name: lblB Name: cmdThoat Name: txtB Label Caption: Cạnh B Command Caption: &Thoát Textbox Text: Name: lblC Name: Frame1 Name: txtC Label Caption: Cạnh C Frame Caption: &Chọnthao tác Textbox Text: Name: lblDT Name: cmdTinh Name: txtDT Label Caption:Diện tích Command Caption: &Tính Textbox Text: Mở cửa sổ soạn thảo mã lệnh và nhập code cho các sự kiện nh sau: Option Explicit Dim a, b, c, p, dt As Double Private Sub cmdHuy_Click() txtA.Text = "" txtB.Text = "" txtC.Text = "" txtdt.Text = "" txtA.SetFocus End Sub Private Sub cmdThoat_Click() Unload Me End Sub Private Sub cmdTinh_Click() If txtA.Text = "" Or txtB.Text = "" Or txtC.Text = "" Then MsgBox "Ban nhap thieu du lieu", vbOKOnly + vbQuestion txtA.Text = 4 txtB.Text = 5 txtC.Text = 6 Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 6 / 26 End If a = CDbl(txtA.Text) 'ham CDbl chuyen doi gia tri trong txtA.text thanh kieu so Double b = CDbl(txtB.Text) c = CDbl(txtC.Text) p = (a + b + c) / 2 If (a + b > c And b + c > a And a + c > b) Then dt = Sqr(p * (p - a) * (p - b) * (p - c)) txtdt.Text = FormatNumber(dt, 2) 'ham formatnumber cho phep lay gia tri cua dt voi hai so le Else MsgBox "Khong the tinh!" cmdHuy_Click End If End Sub Private Sub txtA_GotFocus() txtA.BackColor = QBColor (14) End Sub Private Sub txtb_GotFocus() txtB.BackColor = QBColor(14) End Sub Private Sub txtc_GotFocus() txtC.BackColor = QBColor(14) End Sub Private Sub txtA_LostFocus() txtA.BackColor = QBColor(15) End Sub Private Sub txtb_LostFocus() txtB.BackColor = QBColor(15) End Sub Private Sub txtc_LostFocus() txtC.BackColor = QBColor(15) End Sub Private Sub txta_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 13 txtB.SetFocus End Select End Sub Private Sub txtB_K eyPress(KeyAscii As Integer) Select Case KeyAscii Case 13 txtC.SetFocus End Select End Sub Private Sub txtc_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 13 cmdTinh.SetFocus End Select End Sub Thực hiện chơng trình. Trong trờng hợp ta không muốn cho ngời sử dụng nhập ký tự vào các textbox thì cần viết thêm đoạn code sau vào trong chơng trình. Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 7 / 26 Private Sub txtA_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Then KeyAscii = 0 End If End Sub Private Sub txtB_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Then KeyAscii = 0 End If End Sub Private Sub txtC_KeyPress(KeyAscii As Integer) If (KeyAscii < Asc(" 0") Or KeyAscii > Asc("9")) Then KeyAscii = 0 End If End Sub Bài 6: Thiết kế chơng trình tính khoảng cách hai điểm có giao diện nh sau: Bảng thuộc tính nh sau: Điều khiển Thuộc tính Điều khiển Thuộc tính Điều khiển Thuộc tính Name: Label1 Name: Frame1 Name: txthesogoc Label Caption: X1 Frame Caption: Điểm thứ 1 Textbox Text: Name: Label2 Name: Frame2 Name: txtKC Label Caption: Y1 Frame Caption: Điểm thứ 2 Textbox Text: Name: Label3 Name: txtX1 Name: cmdTinhtoan Label Caption: X2 Textbox Text: Command Caption: Tính toán Name: Label4 Name: txtY1 Name: cmdthoat Label Caption: Y2 Textbox Text: Command Caption: Thoát Name: Label5 Name: txtX2 Label Caption: Hệ số góc Textbox Text: Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 8 / 26 Name: Label6 Name: txtY2 Label Caption: Khoảng cách Textbox Text: Viết mã lệnh cho các sự kiện chọn nút Tính toán và Thoát nh sau: Private Sub cmdTinhtoan_Click() Dim x1, y1, x2, y2, d, hsg As Double If txtX1.Text = "" Or txtY1.Text = "" Or txtX2.Tex t = "" Or txtY2.Text = "" Then MsgBox "Ban can nhap du du lieu !" If txtX1.Text = "" Then txtX1.SetFocus If txtY1.Text = "" Then txtY1.SetFocus If txtX2.Text = "" Then txtX2.SetFocus If txtY2.Text = "" Then txtY2. SetFocus Else x1 = Val(txtX1.Text) y1 = Val(txtY1.Text) x2 = Val(txtX2.Text) y2 = Val(txtY2.Text) d = Sqr((x2 - 1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) hsg = (y2 - y1) / (x2 - x1) txthesogoc.Text = FormatNumber(hsg, 2) txtKC.Text = FormatNumber(d, 2) End If End Sub Private Sub cmdthoat_Click() Unload Me End Sub Bài 7: Thiết kế chơng trình giải phơng trình bậc nhất ax +b=0 có giao diện nh sau: Bảng thuộc tính nh sau: Điều khiển Thuộc tính Điều khiển Thuộc tính Điều khiển Thuộc tính Name: Label1 Name: Frame1 Name: txtKQ Label Caption: Hệ số A Frame Caption: Chọn nút bấm Textbox Text: Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 9 / 26 Name: Label2 Name: txtA Name: cmdTinh Label Caption: Hệ số B Textbox Text: Command Caption: &Tính Name: Label3 Name: txtB Name: cmdThoat Label Caption: Kết quả Textbox Text: Command Caption: &Thoát Viết mã lệnh cho các sự kiện chọn nút Tính và Thoát nh sau: Private Sub cmdTinh_Click() Dim a, b, x As Double If txtA.Text = "" Or txtB.Text = "" Then MsgBox "Ban chua nhap du du lieu !" If txtA.Text = "" Then txtA.SetFocus If txtB.Text = "" Then txtB.SetFocus Else a = Val(txtA.Text) b = Val(txtB.Text) If a = 0 Then If b = 0 Then txtKQ.Text = "Phuong trinh vo so nghiem !" Else txtKQ.Text = "Phuong trinh vo nghiem!" End If Else x = -b / a txtKQ.Text = "Phuong trinh co nghiem x=" txtKQ.Text = txtKQ.Text & FormatNumber(x, 2) End If End If End Sub Private Sub cmdthoat_Click() Unload Me End Sub Bài 8: Thiết kế chơng trình giải phơng trình bậc hai ax 2 +bx+c=0 có giao diện nh sau: Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 10 / 26 Bảng thuộc tính các đi ều khiển nh sau: Điều khiển Thuộc tính Điều khiển Thuộc tính Điều khiển Thuộc tính Name: Label1 Name: Frame1 Name: Command1 Label Caption: Hệ số A Frame Caption: Chọn chức năng Command Caption: &Giải phơng trình Name: Label2 Name: textA Name: Command2 Label Caption: Hệ số B Textbox Text: Command Caption: &Xóa Name: Label3 Name: textB Name: Command3 Label Caption: Hệ số C Textbox Text: Command Caption: &Thoát Name: Label4 Name: textC Name: Timer1 Label Caption: Kết quả Textbox Text: Timer Interval: 150 Name: Label5 Name: Textketqua Name: frmPTB2 Lbl1 Caption: Chuong trinh duoc viet boi ABC Textbox Text: Form Caption: CT giai phuong trinh bac hai Viết mã lệnh cho các sự kiện chọn nút Giải phơng trình, Xóa và Thoát nh sau: Private Sub Command1_Click() Dim a, b, c As Double Dim delta, x, x1, x2 As Double If TextA.Text = "" Or TextB.Text = "" Or TextC.Text = "" Then MsgBox "Ban nhap thieu du lieu", vbOKOnly + vbQuestion TextA.Text = 4 TextB.Text = -6 TextC.Text = 2 End If a = Val(TextA.Text) b = Val(TextB.Text) c = Val(TextC.Text) delta = b * b - 4 * a * c If (delta < 0) Then Textketqua.Text = " Phơng trình vô nghiệm" End If If (delta = 0) Then x = -b / (2 * a) Textketqua.Text = " Phơng trình có nghiệm kép x= " & FormatNumber(x, 2) End If If (delta > 0) Then x1 = (-b + Sqr(delta)) / (2 * a) x2 = (-b - Sqr(delta)) / (2 * a) Textketqua.Text = " Phơng trình có hai nghiệm x1= " & FormatNumber(x1, 2) & " và x2= " & FormatNumber(x2, 2) End If End Sub Private Sub Command2_Click() TextA.Text = "" TextB.Text = "" TextC.Text = "" Textketqua.Text = "" [...]... thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm Private Sub Form_Load() solanchon = 0 'khoi tao so lan nguoi su dung chon la 0 Dim i As Integer For i = 0 To 6 Chksothich(i).Value = 0 chkxe(i).Value = 0 chkcongviec(i).Value = 0 'huy danh dau cac hop kiem a(i) = 0: b(i) = 0: c(i) = 0 'ba lenh duoc viet tren mot dong 'khoi tao gia tri cho cac phan tu cua 3 mang bang 0 Next i End Sub Private... vdecimal = 0 ce = 0 txtresult.Text = "0. " End Sub Private Sub cmdCancel_Click() txtresult.Text = "0. " status = 0 vdecimal = 0 oper = "" ce = 0 so = 0 Bộ môn : công nghệ phần mềm Bài tập thực hành môn: ngôn ngữ visual basic End Sub Private Sub cmdCE_Click() ce = 0 vdecimal = 0 txtresult.Text = "0. " so = 0 End Sub Private Sub cmdnumber_Click(Index As Integer) If ce = 0 Then txtresult.Text = Index ce = 1... = a (0) For i = 0 To 6 If maxa < a(i) Then maxa = a(i) Next i 'tim max cua mang b ung voi loai xe duoc chon nhieu nhat maxb = b (0) For i = 0 To 6 If maxb < b(i) Then maxb = b(i) Next i 'tim max cua mang c ung voi cong viec duoc chon nhieu nhat maxc = c (0) For i = 0 To 6 If maxc < c(i) Then maxc = c(i) Next i 'Tim ten sothich, xe may, cong viec duoc chon nhieu nhat va luu vao cac bien For i = 0 To 6 If... visual basic Private Sub CmdTinhvaIn_Click() Cls If optBang.Value Then tienngoaite = "Bang Anh" tygia = 2 100 0 End If If OptMac.Value Then tienngoaite = "Mac Duc" tygia = 1 200 0 End If If OptRup.Value Then tienngoaite = "Rup Nga" tygia = 800 End If If optDola.Value Then tienngoaite = "Dola My" tygia = 17 900 End If tienngoaite = tienviet / tygia Print Print "Khach hang: "; ten Print "So tien Viet la: "; tienviet... Label1.Caption = Format(Time, "hh:mm:ss:nn") If n = 0 Then MsgBox "Da het 60 giay" Label1.Caption = "" Label2.Caption = "" Timer1.Enabled = False End If End Sub Private Sub Command1_Click() Unload Me End Sub Điều khiển Command Timer Thuộc tính Name: Command1 Caption: &Close Name: Timer1 Interval: 100 0 Bộ môn : công nghệ phần mềm Bài tập thực hành môn: ngôn ngữ visual basic Điều khiển Option Option Option Option... khach hang", " Le Van Hai") tienviet = 100 0 * Val(InputBox("So tien can doi= (00 0)")) End Sub Private Sub CmdXoa_Click() Cls End Sub Private Sub CmdKetthuc_Click() Beep End End Sub Thuộc tính Name: cmdNhap Caption: &Nhập dữ liệu Name: cmdTinhvaIn Caption:&Tính và In Name: cmdXoa Caption: &Xóa Name: cmdKetthuc Caption: &Kết thúc Bài tập thực hành môn: ngôn ngữ visual basic Private Sub CmdTinhvaIn_Click()... End Sub Private Sub Operator_Click(Index As Integer) oper = Operator(Index).Caption If txtresult.Text = "0. " Then so = 0 Else so = Val(txtresult.Text) End If status = 1 txtresult.Text = "0. " vdecimal = 0 ce = 0 End Sub Private Sub cmdpoint_Click() If vdecimal = 0 Then If ce = 0 Then txtresult.Text = "0. " ce = 1 Else txtresult.Text = txtresult.Text & "." End If vdecimal = 1 End If End Sub Private Sub CmdEqual_Click()... Sub cmdsave_Click() solanchon = solanchon + 1 For i = 0 To 6 If Chksothich(i).Value = 1 Then a(i) = a(i) + 1 If chkxe(i).Value = 1 Then b(i) = b(i) + 1 If chkcongviec(i).Value = 1 Then c(i) = c(i) + 1 'neu nguoi su dung chon hop kiem nao thi cong them mot gia tri Next i For i = 0 To 6 Chksothich(i).Value = 0 chkxe(i).Value = 0 chkcongviec(i).Value = 0 Next i End Sub 'huy danh dau cac hop kiem de khoi... ngôn ngữ visual basic Private Sub cmdexit_Click() Unload frmThongke End Sub Private Sub Timer1_Timer() Dim x, y As String x = Left(lbl1.Caption, 1) y = Right(lbl1.Caption, Len(lbl1.Caption) - 1) lbl1.Caption = y + x End Sub Private Sub Command1_Click() Unload frmBC frmThongke.Show End Sub Dim status, vdecimal, oper, ce As Byte Dim so As Double Private Sub Form_Load() status = 0 vdecimal = 0 ce = 0 txtresult.Text... Form2 Form1.Show End Sub Bộ môn : công nghệ phần mềm Bài tập thực hành môn: ngôn ngữ visual basic Điều khiển Thuộc tính DriverListBox Name: Driver1 DirListBox Name: Dir1 FileListBox Label Điều khiển Timer Name: File1 Patern: *.jpg; *.bmp; *.gif Name: Label1 Caption: PictureViewer Thuộc tính Name: Timer1 Interval: 100 Điều khiển Image Name: Label2 Label Caption: Chương trình được viết bởi Vũ Bảo Tạo . ", "Nhap ten khach hang", " Le Van Hai") tienviet = 100 0 * Val(InputBox("So tien can doi= (00 0)")) End Sub Private Sub CmdXoa_Click() Cls End Sub Private Sub CmdKetthuc_Click() . hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 2 / 26 Timer Name: Timer1 Interval: 300 Label Name: Label1 Caption:. txtB.Text = 5 txtC.Text = 6 Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 6 / 26 End If a = CDbl(txtA.Text)

Ngày đăng: 19/06/2014, 12:11

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan