User Tools

Site Tools


blog:2025-04-15-001

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

blog:2025-04-15-001 [2025/04/15 13:45] (current)
jethro created
Line 1: Line 1:
 +~~NOTOC~~
 +====== 2025-04-15 c# contextmenustrip 關聯之listbox ======
 +  * 在 C# 中,可以使用 ContextMenuStrip 來實現根據 ListBox 的選擇狀態顯示不同的選單項目。以下是一個簡單的範例,說明如何根據 ListBox 的選擇狀態來顯示 "​Add"​ 和 "​Delete"​ 菜單項目。
 +===== 步驟 =====
 +  * 建立一個 Windows Forms 應用程式。
 +  * 添加 ListBox 和 ContextMenuStrip 控制項。
 +  * 設置 ContextMenuStrip 的事件。
 +===== 範例程式碼 =====
 +  * <sxh c#>using System;
 +using System.Windows.Forms;​
 +
 +public class MyForm : Form
 +{
 +    private ListBox listBox;
 +    private ContextMenuStrip contextMenuStrip;​
 +
 +    public MyForm()
 +    {
 +        listBox = new ListBox();
 +        contextMenuStrip = new ContextMenuStrip();​
 +
 +        // 添加菜單項目
 +        ToolStripMenuItem addItem = new ToolStripMenuItem("​Add"​);​
 +        ToolStripMenuItem deleteItem = new ToolStripMenuItem("​Delete"​);​
 +
 +        // 將菜單項目添加到 ContextMenuStrip
 +        contextMenuStrip.Items.Add(addItem);​
 +        contextMenuStrip.Items.Add(deleteItem);​
 +
 +        // 將 ContextMenuStrip 關聯到 ListBox
 +        listBox.ContextMenuStrip = contextMenuStrip;​
 +
 +        // 設置事件處理
 +        listBox.MouseDown += ListBox_MouseDown;​
 +        ​
 +        // 添加 ListBox 到窗體
 +        listBox.Dock = DockStyle.Fill;​
 +        this.Controls.Add(listBox);​
 +        ​
 +        // 添加一些示例項目
 +        listBox.Items.Add("​Item 1");
 +        listBox.Items.Add("​Item 2");
 +        listBox.Items.Add("​Item 3");
 +    }
 +
 +    private void ListBox_MouseDown(object sender, MouseEventArgs e)
 +    {
 +        if (e.Button == MouseButtons.Right)
 +        {
 +            // 獲取選中的項目
 +            int index = listBox.IndexFromPoint(e.Location);​
 +            if (index != -1) // 有選中項目
 +            {
 +                contextMenuStrip.Items[1].Visible = true; // 顯示 Delete
 +            }
 +            else // 沒有選中項目
 +            {
 +                contextMenuStrip.Items[1].Visible = false; // 隱藏 Delete
 +            }
 +        }
 +    }
 +
 +    [STAThread]
 +    static void Main()
 +    {
 +        Application.EnableVisualStyles();​
 +        Application.Run(new MyForm());
 +    }
 +}</​sxh>​
 +===== 說明 =====
 +  * ListBox: 用於顯示項目。
 +  * ContextMenuStrip:​ 包含 "​Add"​ 和 "​Delete"​ 菜單項目。
 +  * MouseDown 事件: 當右鍵單擊 ListBox 時,檢查是否有選中的項目,並根據選擇狀態顯示或隱藏 "​Delete"​ 菜單項目。
 +===== 注意事項 =====
 +  * 確保將 ContextMenuStrip 的 Visible 屬性設置為 true 或 false,以控制菜單項目的顯示。
 +  * 可以根據需求添加 "​Add"​ 菜單項目的功能。
 +  * 這樣就可以根據 ListBox 的選擇狀態來顯示不同的上下文菜單了!
 +===== TAGS =====
 +  * 
 +====== ======
 +  * {{counter}} person(s) visited this page until now.
 +  * [[:​memo:​index|Back]]
 +====== ======
 +<​html><​!-- ​
 +PDF for A4-Portrait:​ {{pdfjs 50%,450px > xxx.pdf?​page-fit}}
 +PDF for A4-Landscape:​ {{pdfjs 500px,700px > xxx.pdf?​page-fit}}
 +PDF for iPad Note: {{pdfjs 700px,500px > xxx.pdf?​page-fit}}
 +
 +Youtube: {{youtube>​large:​XXXXX}}
 +Code Highlight: <sxh php; first-line: 70; highlight: [89,92]; title: New title attribute in action>
 +
 +--></​html>​
  
blog/2025-04-15-001.txt · Last modified: 2025/04/15 13:45 by jethro