Sabtu, 18 Desember 2010

Mengubah Font Saat Runtime

Cara ini dkhususkan untuk para pengguna VB .NET, mengingat dalam mengubah properti font saat runtime (dijalankan) dari suatu kontrol tidak sesederhana dalam VB 6.0. Agar mempermudah prosesnya, maka Anda perlu menambahkan sebuah fungsi dibawah ini ke dalam Module baru.
Public Function nFont(ByVal FontName As String, ByVal Size As Single, Optional ByVal Bold As Boolean = False, Optional ByVal Italic As Boolean = False, Optional ByVal Underline As Boolean = False, Optional ByVal Strikeout As Boolean = False) As Font
Dim fs As FontStyle = FontStyle.Regular
If Bold = True Then fs += FontStyle.Bold
If Italic = True Then fs += FontStyle.Italic
If Underline = True Then fs += FontStyle.Underline
If Strikeout = True Then fs += FontStyle.Strikeout

Try
Return New Font(FontName, Size, fs)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function

Sekarang untuk mengetesnya, buatlah sebuah kontrol Label. Lalu tuliskan kode berikut di 'Form_Load'.
Label1.Font = nFont("Arial", 16)

Anda juga bisa menerapkannya pada kontrol ListView. Buat sebuah kontrol ListView(View=List), lalu tuliskan kode berikut di 'Form_Load'.
ListView1.Items.Add("Pertama").Font = nFont("Arial", 9, True)
ListView1.Items.Add("Kedua").Font = nFont("Times New Roman", 10, , True)
ListView1.Items.Add("Ketiga").Font = nFont("Lucida Handwriting", 8, , , True)
ListView1.Items.Add("Keempat").Font = nFont("Courier New", 10, , , , True)

Label: , ,

0 Komentar:

Posting Komentar

Pengunjung yang baik selalu meninggalkan jejak berupa komentar. :)

Berlangganan Posting Komentar [Atom]

<< Beranda