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

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

[VB.NET] การสร้างเมนู (ContextMenuStrip) บนปุ่มคำสั่ง (Button) แบบ Run Time

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

311

กระทู้

502

โพสต์

6074

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6074


ปกติปุ่มคำสั่ง (Button) บน Visual Basic 2010 จะไม่มีเมนูในการเลือกเพิ่มเติมติดมาให้ แต่เราสามารถสร้างมันขึ้นมาใหม่ ด้วยการเขียนโค้ดคำสั่งเข้าไปในขณะที่โปรแกรมทำงาน หรือที่เรียกว่า Run Time (หรือบางคนอาจจะเรียกว่าไดนามิค)


มาดูโค้ดในการสร้าง ...
  1. Public Class frmContextMenuButton
  2.     '// Create MenuStrip @Run Time
  3.     Dim _contextmenu As New ContextMenuStrip

  4.     Private Sub frmContextMenuButton_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5.         '// Add new contextmenu
  6.         _contextmenu.Items.Add("Image Files")
  7.         _contextmenu.Items.Add("Document Files")
  8.         _contextmenu.Font = New Font("Tahoma", 11, FontStyle.Regular)
  9.         AddHandler _contextmenu.ItemClicked, AddressOf Contextmenu_Click
  10.     End Sub

  11.     ' / --------------------------------------------------------------------------
  12.     Private Sub Contextmenu_Click(ByVal sender As System.Object, ByVal e As ToolStripItemClickedEventArgs)
  13.         '// Choose item from contextmenu.
  14.         Select Case e.ClickedItem.Text
  15.             '// Browse Images
  16.             Case "Image Files"
  17.                 _contextmenu.Visible = False
  18.                 '// DO SOMETHING
  19.                 MessageBox.Show("You choose Images Files")
  20.                 '// Browse Documents
  21.             Case "Document Files"
  22.                 _contextmenu.Visible = False
  23.                 '// DO SOMETHING
  24.                 MessageBox.Show("You choose Documents Files")
  25.         End Select
  26.     End Sub

  27.     ' / --------------------------------------------------------------------------
  28.     Private Sub btnAttach_Click(sender As System.Object, e As System.EventArgs) Handles btnAttach.Click
  29.         '// Show contextmenustrip on button @run Time.
  30.         _contextmenu.Show(btnAttach, 0, btnAttach.Height)
  31.     End Sub

  32. End Class
คัดลอกไปที่คลิปบอร์ด




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

0

กระทู้

2

โพสต์

51

เครดิต

Member

Rank: 2

เครดิต
51
โพสต์ 2019-3-24 15:33:57 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-5-6 03:31 , Processed in 0.132091 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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