<< 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 does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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