thongkorn โพสต์ 2018-9-5 18:52:31

[VB.NET] การสร้างปุ่มคำสั่ง (Button Control) ในลักษณะแบบ Run Time หรือการสั่งด้วยโค้ด

http://www.g2gnet.com/webboard/images/vbnet/CreateButtonRuntime.png


การสร้าง Control ต่างๆบน Visual Basic เรามักจะใช้การ Design Time หรือ จับลาก Control มาวางแล้วปรับคุณสมบัติต่างๆ และมันก็จะแสดงผลให้เราเห็นได้ทันทีทันใด แต่ในบางกรณีเราไม่สามารถจะล่วงรู้ล่วงหน้าได้ว่าจำนวน Control ที่ต้องใช้งานมีกี่ตัวกันแน่ ดังนั้นการใช้วิธีการสร้าง Control แบบ Run Time หรือสั่งด้วยโค้ด จนกว่าคำสั่งจะทำงานไปถึง มันจึงจะแสดงผลให้เห็นได้ วิธีการนี้จะมีประสิทธิภาพและยืดหยุ่นสูงมาก เหล่าบรรดามือใหม่ทั้งหลาย ควรจะต้องศึกษาเรียนรู้กันไว้ด้วยน่ะขอรับกระผม


มาดูโค้ดกันเถอะ ... เป็นการสร้าง Button Control ให้อยู่ภายใต้กรอบของ Panel Control อีกที
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Create Button @Runtime with VB.NET
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------

Public Class frmCreateButtonRunTime
    Private pn As New Panel()
    Private Buttons As New Dictionary(Of String, Button)

    Private Sub frmCreateButtonRunTime_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      '// Create Panel
      With pn
            .Location = New System.Drawing.Point(1, 1)
            .Size = New System.Drawing.Size(405, Me.Height - 45)
            .BackColor = Color.Moccasin
            .AutoScroll = True
      End With
      '// ใส่ Panel ลงบนฟอร์ม
      Me.Controls.Add(pn)
      '// สร้างปุ่ม (Button) ลงใน Panel (จำนวนหลัก, จำนวนปุ่ม)
      Call AddButtons(4, 19)
    End Sub

    '// เพิ่มปุ่มคำสั่ง (Button Control)
    Private Sub AddButtons(ByVal ColCount As Byte, ByVal MaxBtn As Byte)
      For i As Integer = 0 To MaxBtn - 1
            Dim B As New Button
            pn.Controls.Add(B)
            With B
                .Height = 80
                .Width = 100
                .Left = (i Mod ColCount) * B.Width
                .Top = (i \ ColCount) * B.Height
                .Text = Chr((i \ ColCount) + Asc("A")) & i Mod ColCount + 1
                Buttons.Add(B.Text, B)
                .Tag = i
                .Cursor = Cursors.Hand
                .ForeColor = Color.Black
                .BackColor = Color.DeepSkyBlue
            End With
            '// Force events handler.
            AddHandler B.Click, AddressOf ClickButton
      Next
    End Sub

    '// Click Button event, get the text of button
    Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs)
      Dim btn As Button = sender
      MessageBox.Show("คุณคลิ๊ก [" + btn.Text + "]" & vbCrLf & "Tag=" & btn.Tag & " จะเป็นค่า Primary Key")
      btn.ForeColor = Color.White
      btn.BackColor = Color.Maroon
    End Sub

End Class

ดาวน์โหลดโค้ดต้นฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...

g2gsoftuser โพสต์ 2022-10-25 19:20:09

ขอบคุณครับ
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] การสร้างปุ่มคำสั่ง (Button Control) ในลักษณะแบบ Run Time หรือการสั่งด้วยโค้ด