ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3107|ตอบกลับ: 2

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

[คัดลอกลิงก์]

2

กระทู้

4

โพสต์

62

เครดิต

Member

Rank: 2

เครดิต
62

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

7878.PNG

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6052
โพสต์ 2018-12-27 13:10:57 | ดูโพสต์ทั้งหมด



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

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

  22. ' ===========================================================
  23. ' โปรแกรมย่อยที่ใช้ในการเพิ่ม CommandButton ตามจำนวนที่กำหนด แบบ Run Time
  24. ' ===========================================================
  25. Private Sub AddControl(NumOfControl As Integer)
  26.     Dim Count As Integer
  27.     For Count = 1 To NumOfControl
  28.         
  29.         ' คำสั่ง Load คือ การโหลด Form หรือ Control เข้าสู่หน่วยความจำ
  30.         ' การใช้คำสั่งนี้ ก็จะเหมือนกับการคัดลอก และ วาง Control ลงบนฟอร์มนั่นเอง
  31.         ' และเพิ่มจำนวนค่าของ Index ขึ้นอีก1 ... เพราะมันเป็น Array ไงล่ะครับ
  32.         ' เทคนิคง่ายๆ คำสั่งง่ายๆ ...
  33.         CtrlIndex = CtrlIndex + 1
  34.         Load CtrlCommand(CtrlIndex)
  35.    
  36.         With CtrlCommand(CtrlIndex)
  37.             ' จัดตำแหน่งของ Control ...
  38.             Select Case (Count Mod 2)
  39.                 ' เลขจำนวนเต็มใดๆ หารเอาเศษ (MOD) ด้วย 2 หากผลลัพธ์เป็น 0 เลขจำนวนเต็มนั้นๆจะเป็นเลขคู่
  40.                 Case 0
  41.                     .Left = CtrlCommand(0).Left
  42.                     .Top = CtrlCommand(CtrlIndex - 1).Top + _
  43.                                 CtrlCommand(CtrlIndex - 1).Height + 60
  44.                
  45.                 ' เลขจำนวนเต็มใดๆ หารเอาเศษ (MOD) ด้วย 2 หากผลลัพธ์เป็น 1 เลขจำนวนเต็มนั้นๆจะเป็นเลขคี่
  46.                 Case 1
  47.                     .Left = CtrlCommand(CtrlIndex - 1).Left + _
  48.                                 CtrlCommand(CtrlIndex - 1).Width + 60
  49.                     .Top = CtrlCommand(CtrlIndex - 1).Top
  50.                
  51.             End Select
  52.             
  53.             .Visible = True
  54.             ' ป้ายบอกชื่อ Control
  55.             'CtrlCommand(Count).Caption = CtrlCommand(Count).Name & _
  56.                                                                             " index " & CtrlCommand(Count).Index
  57.             
  58.             CtrlCommand(Count).Caption = "โต๊ะหมายเลข " & CtrlCommand(Count).Index + 1
  59.         End With
  60.     Next
  61. End Sub

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


AddButtonRuntime.zip

10.44 KB, ดาวน์โหลดแล้ว: 3505

สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

2

กระทู้

4

โพสต์

62

เครดิต

Member

Rank: 2

เครดิต
62
 เจ้าของ| โพสต์ 2018-12-27 21:58:04 | ดูโพสต์ทั้งหมด

ขอบคุณมากครับอาจารย์
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-27 00:26 , Processed in 0.280794 second(s), 5 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้