<< Chapter < Page Chapter >> Page >

Bước 9: Thêm đoạn mã sau vào sự kiện Text1_KeyPress để ngăn chặn các giá trị không phải là số.

' Loai bo ky tu khong can thiet

If KeyAscii = vbKeyBack Then Exit Sub

If KeyAscii<vbKey0 Or KeyAscii>vbKey9 Then

KeyAscii = 0

End If

Bước 10: Lưu dự án lại và chạy chương trình.

Bài tập 1i-3

THAO TÁC VỚI DRIVELISTBOX, DIRLISTBOX, FILELISTBOX

Trong ví dụ này ta phải tạo 5 đối tượng, trong đó có 4 điều khiển:

  • Một Form.
  • Một điều khiển DriveListBox
  • Một điều khiển DirListBox
  • Một điều khiển FileListBox
  • Một điều khiển ImageBox

Bước 1: Tạo giao diện người dùng. Ta chỉ cần nhấp và vẽ đúng vị trí từng điều khiển trên Form.

1243

Hình I.3: Giao diện lựa chọn tập tin hình ảnh để hiển thị

1: DriveListBox

Name: drvSource

2:DirListBox

Name: dirSource

3:FileListBox

Name: filSource

Pattern: *.bmp;*.wmf;*.ico;*.jpg

4:ImageBox

Name: ImgSource

Stretch: TRUE

Bước 2: Viết mã trao đổi thông tin giữa các đối tượng:

Trong cửa sổ thiết kế Form, nhấp đúp vào DriveListBox, cửa sổ Code hiện ra, xử lý sự kiện sau:

Private Sub drvSource_Change()

dirSource.Path = drvSource.Drive

End Sub

Tương tự cho DirListBox&FileListBox

Private Sub dirSource_Change()

filSource.Path = dirSource.Path

End Sub

Private Sub filSource_Click()

imgSource.Picture = LoadPicture(filSource.Path&"\"&filSource.FileName)

End Sub

Bước 3: Lưu dự án lại vào thư mục Basic\Bt1-3. Chạy chương trình nhờ phím F5.

Hình I.4: Kết quả thực thi

Bài tập 1i-4

ĐIỀU KHIỂN OLE

Bước 1: Tạo dự án mới, trong đó ta có sử dụng OLE.

Hộp thoại Insert Object hiện ra để ta lựa chọn, ở đây chọn kích hoạt Create New, Object Type là Bitmap Image; đánh dấu chọn Display as Icon.

Hình I.5: Sử dụng OLE Control

Bước 2: Nhấp OK, VB sẽ gọi trình ứng dụng Paint&ta vẽ hình trên cửa sổ Paint. Sau đó chọn Exit&Return trong cửa sổ Form, ta được:

Hình I.6: Kết quả thực thi ứng dụng

Bước 3: Lưu dự án vào thư mục Basic\Bt1I-4 và chạy chương trình; nhấp đúp vào biểu tượng Bitmap Image,VB sẽ khởi động Paint để ta hiệu chỉnh hình vẽ đầu.

Bài tập tự làm

  1. Thiết kế chương trình như sau:

Hình I.7 Các phép tính cơ bản

Nhập vào 2 giá trị A, B; sau đó chọn một phép toán (+,-,*,/). Nhấp chọn nút nhấn Thực hiện, kết quả sẽ hiển thị trong điều khiển nhãn Kết quả.

  1. Thiết kế chương trình để nhập vào tọa độ của hai điểm (x1,y1); (x2,y2) và cho phép:

a) Tính hệ số góc của đường thẳng đi qua hai điểm đó theo công thức:

Hệ số góc = (y2 - y1) /(x2 - x1)

b) Tính khoảng cách giữa hai điểm theo công thức:

khoảng cách = ( x2 - x1 ) 2 + ( y2 - y1 ) 2 size 12{ sqrt { \( "x2 - x1" \) rSup { size 8{2} } + \( "y2 - y1" \) rSup { size 8{2} } } } {}

Giao diện chương trình có thể như sau:

Hình I.8: Tọa độ các điểm

  1. Viết chương trình cho phép nhập vào bán kính r của một hình tròn. Tính chu vi và diện tích của hình tròn theo công thức :

Chu vi CV = 2*Pi*r

Diện tích Dt = Pi*r*r

Hiển thị các kết quả lên màn hình.

4) Thiết kế chương trình có giao diện như hình dưới và thực hiện các chức năng sau:

Hình I.9: Lựa chọn tên

  • Mỗi khi người sử dụng chương trình nhập thông tin vào 2 ô TextBox, sau đó nhấp chọn nút Thêm, giá trị của ô Mã số được đưa vào ComboBox, còn giá trị của ô Họ và tên được đưa vào ListBox.
  • Mỗi khi họ chọn một mã số nào đó trong ComboBox, giá trị họ và tên tương ứng cũng sẽ được chọn trong ListBox; đồng thời chúng sẽ được hiển thị lên trên các điều khiển TextBox tương ứng (như hình). (Xử lý sự kiện Combo1_Click&List1_Click)
  • Đối với mã số và họ tên của một người, ta có thể sửa đổi giá trị của chúng trong các ô nhập TextBox, sau đó chọn nút Sửa, giá trị của chúng trong ComboBox&ListBox cũng sửa đổi theo.
  • Khi người dùng chọn một mã số (hay họ tên) trên ComboBox (hoặc ListBox), sau đó họ chọn Xóa, các thông tin này được xóa ra khỏi ComboBox&ListBox.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask