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

We Lost Him & Lost Forever King norodom sihanouk (ព្រះករុណាព្រះបាទ នរោត្តម សីហនុ)

1:20AM 15/Oct/2012  is our sad day to heard that our hero (norodom sihanouk) was died in Beijing China . He is the king that i respect he is the one that help Cambodia. From now he leave us he leave us forever and forever :'( how sad we are to live without him. Even He was dead but his name his thing alway on our mind . Good Bye Grand Father  wish you all the best. YOU ARE MY HERO EVER AND FOREVER.

Google’s 12 best Android apps for 2012

As 2012 draws to a close, ‘ol Googs has finally revealed their top picks for apps of the year. If you’re expecting Holo-themed diamonds in the rough, you may be left wanting. Google said their pics were based on compatibility with both tablets and smartphones, and even a few veteran apps who recently found themselves redesigned made the cut. While I don’t have any particular objections with Google’s picks for best of 2012, I was only a bit disappointed to find there weren’t really any surprises. See for yourself and let me know if you agree. In no particular order, Google’s best Android apps for 2012 are: Zappos Evernote Pinterest Grimm’s Snow White

Asphalt 7: Heat v1.0.1 APK

Hit the speed of heat in the newest, fastest, most visually stunning edition of the famed Asphalt series Asphalt 7: Heat v1.0.1   play.google.com.gameloft.GloftA7HM A FIRST-CLASS LINEUP Drive 60 different cars from the world’s most prestigious manufacturers, like Ferrari, Lamborghini and Aston Martin, including the legendary DeLorean. RACE ACROSS THE GLOBE Gear up to race on 15 tracks set in real cities around the world, including brand new tracks in Hawaii, Paris, London, Miami and Rio. CHALLENGE THE WORLD The completely revamped multiplayer lets you take on up to 5 of your friends locally or online. Keep track of who’s the best with the new Asphalt Tracker that lets you compare stats, show off achievements and challenge rivals. You can also find new online opponents with the matchmaking system. Practice haHit the speed of heat in the newest, fastest, most visually stunning edition of the famed Asphalt series.