- Back to Home »
- Visual Basic.Net , មេរៀនឆ្នាំទី៣ »
- Change Font
Posted by : Unknown
Sunday, December 9, 2012
ការសរសេរកូដ
Public Class Form1
Dim FontName As String, FontStyle As Single, FontSize As Integer
Dim Style As New System.Drawing.FontStyle
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load
lstFontName.Items.Add("Arial")
lstFontName.Items.Add("Arial Black")
lstFontName.Items.Add("Arial Narrow")
lstFontName.Items.Add("Limon F1")
lstFontName.Items.Add("Limon F2")
lstFontName.Items.Add("Limon F3")
lstFontName.Items.Add("Limon R1")
lstFontName.Items.Add("Limon R2")
lstFontName.Items.Add("Limon R3")
lstFontName.Items.Add("Limon R4")
lstFontName.Items.Add("Limon S1")
lstFontName.Items.Add("Limon S1")
lstFontName.Items.Add("Limon S3")
lstFontName.Items.Add("Limon S4")
lstFontName.Items.Add("Limon S5")
lstFontName.Items.Add("Script MT Bold")
lstFontName.Items.Add("Khmer OS Bokor")
lstFontName.Items.Add("Microsoft Sans Serif")
lstFontName.Items.Add("Time New Roman")
lstFontStyle.Items.Add("Normal")
lstFontStyle.Items.Add("Bold")
lstFontStyle.Items.Add("Italic")
cmbSize.Items.Add("8")
cmbSize.Items.Add("9")
cmbSize.Items.Add("10")
cmbSize.Items.Add("11")
cmbSize.Items.Add("12")
cmbSize.Items.Add("14")
cmbSize.Items.Add("16")
cmbSize.Items.Add("18")
cmbSize.Items.Add("20")
cmbSize.Items.Add("22")
cmbSize.Items.Add("24")
cmbSize.Items.Add("26")
cmbSize.Items.Add("28")
cmbSize.Items.Add("36")
cmbSize.Items.Add("48")
cmbSize.Items.Add("72")
lstFontStyle.SelectedIndex = 0
lstFontName.SelectedIndex = 0
FontName = lstFontName.SelectedItem
FontSize = cmbSize.Text
Style = lstFontStyle.SelectedIndex
txtLetter.Font = New Drawing.Font(FontName, FontSize, Style)
End Sub
Private Sub lstFontName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles lstFontName.SelectedIndexChanged
FontName = lstFontName.SelectedItem
FontSize = cmbSize.Text
On Error Resume Next
txtLetter.Font = New Drawing.Font(FontName, FontSize, Style)
End Sub
Private Sub cmbSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmbSize.SelectedIndexChanged
FontSize = cmbSize.Text
End Sub
Private Sub lstFontStyle_SelectedIndexChanged(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles lstFontStyle.SelectedIndexChanged
Style = lstFontStyle.SelectedIndex
FontSize = cmbSize.Text
On Error Resume Next
txtLetter.Font = New Drawing.Font(FontName, FontSize, Style)
End Sub
End Class
ការព្យល់កូដ
+ នៅផ្នែកខាងលើបំផុតយើងបានប្រកាសអញ្ញាត្ត :
- FontName សម្រាប់រក្សាឈ្មោះនៃ Font អក្សរ។
- FontSize សម្រាប់រក្សាទុកនូវទំហំរបស់អក្សរ។
- Style សម្រាប់រក្សាទុកនូវ Style នៃអក្សរដូចជា Bold, Italic, Normal។
+ នៅក្នុង event Form_Load:
- កូដ lstFontName.Items.Add("...") សម្រាប់បញ្ចូលតម្លៃនៃ Font អក្សរទៅក្នុង lstFontName ដែលជា ListBox។
- កូដ lstFontStyle.Items.Add("...") សម្រាប់បញ្ចូលតម្លៃនៃ Style អក្សរទៅក្នុង lstFontStyle ដែលជា ListBox។
- កូដ cmbSize.Items.Add("...") សម្រាប់បញ្ចូលតម្លៃនៃ ទំហំអក្សរទៅក្នុង lstFontStyle ដែលជា ListBox។
- កូដ lstFontStyle.SelectedIndex = 0 សម្រាប់ select ធាតុដំបូងនៃ lstFontStyle ដែលមាន index ស្មើនឹង ០។
- កូដ lstFontName.SelectedIndex = 0 សម្រាប់ select ធាតុដំបូងនៃ lstFontName ដែលមាន index ស្មើនឹង ០។
- កូដ FontName = lstFontName.SelectedItem សម្រាប់ផ្ទេរតម្លៃនៅក្នុង lstFontName ដែលមាន index 0 ទៅអោយអញ្ញាត្ត FontName។
- កូដ FontSize = cmbSize.Text ផ្ទេរតម្លៃនៅក្នុង cmdSize ដែលបានជ្រើសរើសរួច ទៅអោយអញ្ញាត្ត FontSize ។
- កូដ Style = lstFontStyle.SelectedIndex ផ្ទេរតម្លៃដែលជា index របស់ lstFontStyle ទៅអោយ អញ្ញាត្ត Style។
+ កូដ txtLetter.Font = New Drawing.Font(FontName, FontSize, Style) សម្រាប់កំណត់ Font ទៅអោយ TextBox txtLetter តាមអញ្ញាត្ត FontName, FontSize និង Style។
+ ចំពោះកូដផ្សេងៗទៀតមានលក្ខណៈដូចនិងកូដខាងលើនេះដែរ។