<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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