Skip to main content

How to Make Button Dynamic With VB.NET

នៅក្នុងកម្មវិធីមួយចំនួនតំរូវឱ្យអ្នកធ្វើការបង្កើត Button តាមតម្រូវការរបស់អ្នកប្រើប្រាស់ ឧទាហរណ៍នៅក្នុង​ ប្រពន្ធ័គ្រប់គ្រងការកម្មង់អាហារនៅក្នុង​ភោជនិយ ដ្ឋាន​(Food Order Management System) អ្នកប្រើប្រាស់អាច ធ្វើការបន្ថែមតុ (តំណាងដោយ Button) តាមតម្រូវការ។
ខាងក្រោមនេះនាងខ្ញុំនឹងបង្ហាញអ្នកអំពីរបៀបបង្កើត Button Dynamic ដែលទាញចេញពី​Database ជាមួយ VB.NET។

 


ជាដំបូងសូមធ្វើការ Design Form ដូចខាងក្រោម 

1.       TextBox: txtBtnName
2.       Button: btnAdd
3.       FlowLayoutPanel: FlowLayoutPanelButton
ចំណាំ៖ ចំពោះ Panel ដែលប្រើសម្រាប់ផ្ទុក Button អ្នកគួរជ្រើសយក FlowLayoutPanel ព្រោះវាជួយរៀបចំឱ្យ Button មានលក្ខណៈជាជួរ ដោយមិនចាំបាច់ឱ្យយើងធ្វើការ​ Set Location ទៅលើButtonដោយខ្លួនឯងទេ។
បន្ទាប់មកអ្នកត្រូវមាន​Database សម្រាប់ផ្ទុកនូវ អក្សរសម្រាប់បង្ហាញនៅក្នុង​ Button និមួយៗ ជាមុនសិន​។ ហើយនៅពេលដែលអ្នកបើ  Form លើកក្រោយចំនួន Button ។
 

ការសរសេរកូដ

- កូដសម្រាប់ Save ឈ្មោះ Button 
1Sub InsertButton(ByVal btnName As String)
2        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=BTN.accdb")
3        Dim comm As New OleDbCommand("Insert into Buttons Values ('" + btnName + "')", con)
4        con.Open()
5        comm.ExecuteNonQuery()
6        con.Close()
7End Sub
- កូដសម្រាប់​បង្កើត​Button ដែលត្រូវបានទាញចេញពី Database
1Sub CallButton()
2        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=BTN.accdb")
3        Dim comm As New OleDbCommand("SELECT * FROM Buttons", con)
4        con.Open()
5        Dim dr As OleDbDataReader = comm.ExecuteReader
6        While dr.Read
7            Dim btn As New Button
8            btn.Text = dr(0).ToString
9            FlowLayoutPanelButton.Controls.Add(btn)
10
11        End While
12        con.Close()
13End Sub
**ដើម្បីឱ្យបើក Form មកចេញ​Button ដែលមានឈ្មោះនៅក្នុង​Database សូមហៅ Function CallButton ខាងលើនៅក្នុង​Event Form_Load ដូចខាងក្រោម​​៖
1Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
2        Call CallButton()
3    End Sub
** សូមហៅ​Function InserButton() នៅក្នុង​Event click របស់ btnBtnName ដូចខាក្រោម៖
1Private Sub btnBtnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
2        FlowLayoutPanelButton.Controls.Clear()
3        Call InsertButton(txtBtnName.Text)
4        Call CallButton()
5End Sub

Comments

Popular posts from this blog

Windows 7 Boot Updater

ជាទូទៅអ្នកតែងតែឃើញនូវរូប Logo របស់ Windows 7 រៀងរាល់ពេលដែល អ្នកបើកដំនើរការ កុំព្យូទ័រ ដែលគេហៅថា BootScreen។ ដូច្នេះនៅក្នុងអត្ថបទនេះ អាយធីកូនខ្មែរ នឹងបង្ហាញពីរ របៀបផ្លាស់ប្តូរ BootScreen របស់ Windows 7។ ជំហានទី ១៖ អ្នកត្រូវធ្វើការទាញយកកម្មវិធី Windows 7 Boot Updater  ចុចត្រង់នេះ ជំហានទី ២៖ បើកដំនើរការកម្មវិធីបន្ទាប់មកនៅក្នុងប្រអប់ Animation ជ្រើសរើសយកជំរើស Animation

Real Football Manager 2013 APK (Armv6 & 7)

Game Features : An intuitive interface with a simple game system (enhanced for RF Manager 2013). Actions with the press, fans and  players  that affect the performance of your  computer . An impressive array of  options , from technical to training schedules. Find the stars of tomorrow with scouting reports you send promising young. Download  Link: DataFileHost: Real Football Manager APK 2Shared: Real Football Manager APK Install  APK And Play. It's a java  port  of RFM13.

Blood & Glory: Legend Galaxy S3 APK and SD Data

Blood & Glory: Legend Galaxy S3 APK and SD Data the highly anticipated sequel to the AAA blockbuster smash-hit Blood & Glory! Return to the arena and bask in the blood-hungry cheers of the crowd as you fight to become a legend! Download Blood & Glory: Legend apk and sd data for your Samsung Galaxy S3.