• <tfoot id='kaSaf'></tfoot>
    <legend id='kaSaf'><style id='kaSaf'><dir id='kaSaf'><q id='kaSaf'></q></dir></style></legend>
      <i id='kaSaf'><tr id='kaSaf'><dt id='kaSaf'><q id='kaSaf'><span id='kaSaf'><b id='kaSaf'><form id='kaSaf'><ins id='kaSaf'></ins><ul id='kaSaf'></ul><sub id='kaSaf'></sub></form><legend id='kaSaf'></legend><bdo id='kaSaf'><pre id='kaSaf'><center id='kaSaf'></center></pre></bdo></b><th id='kaSaf'></th></span></q></dt></tr></i><div id='kaSaf'><tfoot id='kaSaf'></tfoot><dl id='kaSaf'><fieldset id='kaSaf'></fieldset></dl></div>

      • <bdo id='kaSaf'></bdo><ul id='kaSaf'></ul>

        <small id='kaSaf'></small><noframes id='kaSaf'>

      1. 如何将上下文菜单添加到 ListBoxItem?

        How can I add a context menu to a ListBoxItem?(如何将上下文菜单添加到 ListBoxItem?)
        • <bdo id='FVN6n'></bdo><ul id='FVN6n'></ul>

            <small id='FVN6n'></small><noframes id='FVN6n'>

            <tfoot id='FVN6n'></tfoot>
          1. <i id='FVN6n'><tr id='FVN6n'><dt id='FVN6n'><q id='FVN6n'><span id='FVN6n'><b id='FVN6n'><form id='FVN6n'><ins id='FVN6n'></ins><ul id='FVN6n'></ul><sub id='FVN6n'></sub></form><legend id='FVN6n'></legend><bdo id='FVN6n'><pre id='FVN6n'><center id='FVN6n'></center></pre></bdo></b><th id='FVN6n'></th></span></q></dt></tr></i><div id='FVN6n'><tfoot id='FVN6n'></tfoot><dl id='FVN6n'><fieldset id='FVN6n'></fieldset></dl></div>

                <legend id='FVN6n'><style id='FVN6n'><dir id='FVN6n'><q id='FVN6n'></q></dir></style></legend>
                    <tbody id='FVN6n'></tbody>
                • 本文介绍了如何将上下文菜单添加到 ListBoxItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 ListBox,我想为列表中的每个项目添加一个上下文菜单.我已经看到了解决方案"让右键单击选择一个项目并在空白处抑制上下文菜单,但是这个解决方案感觉很脏.

                  I have a ListBox and I want to add a context menu to each item in the list. I've seen the "solution" to have the right click select an item and suppress the context menu if on white space, but this solution feels dirty.

                  有人知道更好的方法吗?

                  Does anyone know a better way?

                  推荐答案

                  这样鼠标旁边会弹出菜单

                  This way the menu will pop up next to the mouse

                  private string _selectedMenuItem;
                  private readonly ContextMenuStrip collectionRoundMenuStrip;
                  
                  public Form1()
                  { 
                      var toolStripMenuItem1 = new ToolStripMenuItem {Text = "Copy CR Name"};
                      toolStripMenuItem1.Click += toolStripMenuItem1_Click;
                      var toolStripMenuItem2 = new ToolStripMenuItem {Text = "Get information on CR"};
                      toolStripMenuItem2.Click += toolStripMenuItem2_Click;
                      collectionRoundMenuStrip = new ContextMenuStrip();
                      collectionRoundMenuStrip.Items.AddRange(new ToolStripItem[] {toolStripMenuItem1, toolStripMenuItem2 });
                      listBoxCollectionRounds.MouseDown += listBoxCollectionRounds_MouseDown;
                  }
                  
                  private void toolStripMenuItem2_Click(object sender, EventArgs e)
                  {
                      var info = GetInfoByName(_selectedMenuItem);
                     MessageBox.Show(info.Name + Environment.NewLine + info.Date);
                  }
                  
                  private void toolStripMenuItem1_Click(object sender, EventArgs e)
                  {
                      Clipboard.SetText(_selectedMenuItem);
                  }
                  
                  private void myListBox_MouseDown(object sender, MouseEventArgs e)
                  {
                      if (e.Button != MouseButtons.Right) return;
                      var index = myListBox.IndexFromPoint(e.Location);
                      if (index != ListBox.NoMatches)
                      {
                          _selectedMenuItem = listBoxCollectionRounds.Items[index].ToString();
                          collectionRoundMenuStrip.Show(Cursor.Position);
                          collectionRoundMenuStrip.Visible = true;
                      }
                      else
                      {
                          collectionRoundMenuStrip.Visible = false;
                      }
                  }
                  

                  这篇关于如何将上下文菜单添加到 ListBoxItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                    <bdo id='r2T7G'></bdo><ul id='r2T7G'></ul>
                      <legend id='r2T7G'><style id='r2T7G'><dir id='r2T7G'><q id='r2T7G'></q></dir></style></legend>
                      <tfoot id='r2T7G'></tfoot>

                    • <small id='r2T7G'></small><noframes id='r2T7G'>

                          <tbody id='r2T7G'></tbody>

                        <i id='r2T7G'><tr id='r2T7G'><dt id='r2T7G'><q id='r2T7G'><span id='r2T7G'><b id='r2T7G'><form id='r2T7G'><ins id='r2T7G'></ins><ul id='r2T7G'></ul><sub id='r2T7G'></sub></form><legend id='r2T7G'></legend><bdo id='r2T7G'><pre id='r2T7G'><center id='r2T7G'></center></pre></bdo></b><th id='r2T7G'></th></span></q></dt></tr></i><div id='r2T7G'><tfoot id='r2T7G'></tfoot><dl id='r2T7G'><fieldset id='r2T7G'></fieldset></dl></div>