<< Chapter < Page Chapter >> Page >

Chọn Modul1 trong cửa sổ soạn thảo chương trình, sau đó nhấp chọn Tools\Add Procedure. Định nghĩa một hàm public tên Doihoa() như sau:

Public Function Doihoa(s As String) As String

Dim s1 As String

Dim s2 As String

Do While InStr(s, " ")<>0

s1 = Left(s, InStr(s, " "))

s = Right(s, Len(s) - InStr(s, " "))

' Doi chu hoa

s1 = UCase(Left(s1, 1))&Right(s1, Len(s1) - 1)

s2 = s2&s1

Loop

' Tra ket qua

Doihoa = s2&" "&s

End Function

' Ham nay khong viet hoa tu cuoi cung.

Bước 7: Hàm Doihoa có nhiệm vụ nhận vào một chuỗi và đổi ký tự đầu tiên của các từ trong chuỗi thành chữ hoa. Bây giờ ta kiểm tra hàm này như sau: Thêm một TextBox và một nút nhấn (Button) lên Form1. Nhấp vào Button, ta thêm đoạn mã sau vào hàm xử lý sự kiện Command1_Click:

Form1.Caption = Doihoa(Text1.Text)

Chạy ứng dụng, nhập một chuỗi vào Text1, nhấp Command1. Chuỗi chữ hoa sẽ xuất hiện trên tiêu đề của Form1.

Bước 8: Sửa lại sao cho có thể viết hoa ký tự đầu tiên của tất các từ.

Bài tập 1ii-3

Lựa chọn với listbox

Bước 1: Tạo thư mục Basic\Bt1II-3. Tạo dự án mới VB Standard EXE trong thư mục trên, sau đó tạo Form có dạng sau:

Hình I.11: Lựa chọn với ListBox

Ta có 2 ListBox và các nút nhấn (Button); trong đó:

Nút>chuyển một phần tử từ trái sang phải

Nút<chuyển một phần tử từ phải sang trái.

Nút>>chuyển tất cả các phần tử từ trái sang phải.

Nút<<chuyển tất cả các phần tử từ phải sang trái.

Thêm 2 ListBox và 4 Button vào Form1. Trong hàm xử lý sự kiện Form_Load thêm vào đoạn mã:

List1.AddItem "Thing 1"

List1.AddItem "Thing 2"

List1.AddItem "Thing 3"

List1.AddItem "Thing 4"

List1.AddItem "Thing 5"

List1.AddItem "Thing 6"

Chạy chương trình.

Bước 2: Thêm hàm xử lý sự kiện Click cho nút nhấn 1 (>) Command1_Click:

' Kiem tra co chon hay khong?

If Form1.List1.ListIndex = -1 Then Exit Sub

' Chep tu trai sang phai

Form1.List2.AddItem Form1.List1.List(Form1.List1.ListIndex)

' Xoa ben trai

Form1.List1.RemoveItem Form1.List1.ListIndex

Bước 3: Chạy chương trình, chọn phần tử trong List1 và nhấp nút>, phần tử đó chuyển sang List2. Bây giờ ta làm ngược lại: chuyển phần tử được chọn từ List2 sang List1. Trở về cửa sổ soạn thảo; chọn đoạn mã vừa nhập trong List1, chọn Edit\Copy trong menu của VB. Nhấp lên Button<, chọn Edit\Paste. Bây giờ ta sửa lại đoạn mã sau trong hàm xử lý sự kiện Command2_Click:

' Kiem tra co chon hay khong?

If Form1.List2.ListIndex = -1 Then Exit Sub

' Chep tu phai sang trai

Form1.List1.AddItem Form1.List2.List(Form1.List2.ListIndex)

' Xoa ben phai

Form1.List2.RemoveItem Form1.List2.ListIndex

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

Ta nhận thấy 2 đoạn mã lệnh trên (cho Button<và>) là như nhau (chỉ đổi chỗ List1 cho List2 và ngược lại). Do đó ta sẽ viết một chương trình con để chuyển dữ liệu từ ListBox này sang ListBox kia, và trong hàm xử lý sự kiện của 2 Button ta chỉ cần gọi chương trình con này để chuyển dữ liệu.

Thêm một Modul mới vào dự án tên Modul1, chọn Tool\Add Procedure để thêm một chương trình con vào tên Chuyendulieu()

Vào Modul1, sửa đổi lại thủ tục chuyển dữ liệu như sau:

Public Sub Chuyendulieu(L1 As ListBox, L2 As 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