araban โพสต์ 2018-12-26 14:29:37

เรียนสอบถามเรื่อง การ ดึงข้อมูลจาก teble มาแสดง Button ใน tabcontrol

แก้ไขครั้งสุดท้ายโดย araban เมื่อ 2018-12-26 14:30

https://www.google.com/search?q=%E0%B8%A3%E0%B8%B0%E0%B8%9A%E0%B8%9A%E0%B8%AB%E0%B8%AD%E0%B8%9E%E0%B8%B1%E0%B8%81&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjG69HA_LzfAhXLMI8KHbvICF0Q_AUIDigB&biw=1440&bih=795#imgrc=Q-rhnmV5DXPLfM:

thongkorn โพสต์ 2018-12-27 13:10:57

http://www.g2gnet.com/webboard/images/vb6/createbuttonruntime.png

โค้ดนี้เป็นการสร้างปุ่มแบบ Run Time โดยมีแค่ฟอร์มเปล่าๆและ CommandButton 1 ตัวเท่านั้นครับ โดยให้สร้างปุ่มตามจำนวนที่เรากำหนด และจัดเรียงแบบ 2 หลัก ด้วยการหาระยะการวางตำแหน่งหากเข้าใจวิธีการคิด ก็น่าจะนำไปประยุกต์ใช้งานได้ (ทำตามขั้นตอนที่ผมบอกโดยเริ่มจาก Form_Load) ...
Option Explicit
' เก็บค่า Index ของ Control Array
Private CtrlIndex As Integer

Private Sub Form_Load()
      ' ปกติวิธีการสร้าง CommandButton Control Array ลงบนฟอร์ม
    ' 1. สร้าง CommandButton ต้นแบบออกมาไว้ก่อน 1 ตัว และตั้งค่าคุณสมบัติ
    '   - Name = CtrlCommand
    '   - Index = 0
    ' 2. คัดลอก หรือ Copy Control แล้ววาง (Paste) ลงบนฟอร์ม
   
    ' เราก็จะใช้วิธีการเดียวกันนั่นแหละครับ เพียงแต่ใช้ Code สั่งงานในขณะ Run Time แทน
    ' การโหลด Form หรือ Control เข้าสู่หน่วยความจำ ... เราใช้คำสั่ง LOAD
    ' กำหนด Index ของ CommandButton Control ตัวแรกเท่ากับ 0
    CtrlIndex = 0
    ' ป้ายบอกชื่อ Control
    'CtrlCommand(0).Caption = CtrlCommand(0).Name & " index " & CtrlCommand(0).Index
    CtrlCommand(0).Caption = "โต๊ะหมายเลข " & CtrlCommand(0).Index + 1
   
    ' เพิ่ม CommandButton อีก 8 ตัว
    Call AddControl(8)
End Sub

' ===========================================================
' โปรแกรมย่อยที่ใช้ในการเพิ่ม CommandButton ตามจำนวนที่กำหนด แบบ Run Time
' ===========================================================
Private Sub AddControl(NumOfControl As Integer)
    Dim Count As Integer
    For Count = 1 To NumOfControl
      
      ' คำสั่ง Load คือ การโหลด Form หรือ Control เข้าสู่หน่วยความจำ
      ' การใช้คำสั่งนี้ ก็จะเหมือนกับการคัดลอก และ วาง Control ลงบนฟอร์มนั่นเอง
      ' และเพิ่มจำนวนค่าของ Index ขึ้นอีก1 ... เพราะมันเป็น Array ไงล่ะครับ
      ' เทคนิคง่ายๆ คำสั่งง่ายๆ ...
      CtrlIndex = CtrlIndex + 1
      Load CtrlCommand(CtrlIndex)
   
      With CtrlCommand(CtrlIndex)
            ' จัดตำแหน่งของ Control ...
            Select Case (Count Mod 2)
                ' เลขจำนวนเต็มใดๆ หารเอาเศษ (MOD) ด้วย 2 หากผลลัพธ์เป็น 0 เลขจำนวนเต็มนั้นๆจะเป็นเลขคู่
                Case 0
                  .Left = CtrlCommand(0).Left
                  .Top = CtrlCommand(CtrlIndex - 1).Top + _
                              CtrlCommand(CtrlIndex - 1).Height + 60
               
                ' เลขจำนวนเต็มใดๆ หารเอาเศษ (MOD) ด้วย 2 หากผลลัพธ์เป็น 1 เลขจำนวนเต็มนั้นๆจะเป็นเลขคี่
                Case 1
                  .Left = CtrlCommand(CtrlIndex - 1).Left + _
                              CtrlCommand(CtrlIndex - 1).Width + 60
                  .Top = CtrlCommand(CtrlIndex - 1).Top
               
            End Select
            
            .Visible = True
            ' ป้ายบอกชื่อ Control
            'CtrlCommand(Count).Caption = CtrlCommand(Count).Name & _
                                                                            " index " & CtrlCommand(Count).Index
            
            CtrlCommand(Count).Caption = "โต๊ะหมายเลข " & CtrlCommand(Count).Index + 1
      End With
    Next
End Sub

Private Sub ctrlCommand_Click(Index As Integer)
    ' นำไปประยุกต์กันต่อเองล่ะกันครับ ... พี่น้อง
    'MsgBox "CtrlCommand " & Index & " ถูกเลือก."
    MsgBox "คุณจิ้มกดเลือกโต๊ะหมายเลข " & Index + 1, vbOKOnly + vbInformation, "เลือกโต๊ะ:"
End Sub

araban โพสต์ 2018-12-27 21:58:04

ขอบคุณมากครับอาจารย์
หน้า: [1]
ดูในรูปแบบกติ: เรียนสอบถามเรื่อง การ ดึงข้อมูลจาก teble มาแสดง Button ใน tabcontrol