【问题标题】:C# How to select ListBox item with a RightClick?C#如何用右键单击选择列表框项目?
【发布时间】:2012-03-02 23:54:41
【问题描述】:

我为此尝试了很多方法并进行了数小时的研究,但它似乎对我不起作用。

这是我当前的代码,我不知道为什么它不应该工作。

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);
        if (e.Button == MouseButtons.Right)
        {
            contextMenuStrip1.Show();
        }
    }

另外我不关心可以删除的上下文菜单我只是在寻找一种方法让鼠标右键选择我单击的项目。

有什么想法吗?

【问题讨论】:

  • 如果你在方法中设置了一个断点,当你按下鼠标右键时你会点击它吗?还是左边?
  • 我好像根本没打到
  • 那么你需要调查为什么你根本没有击中它。该方法是否绑定到组合框上的事件? (通常这是设计者在 InitialiseComponent() 函数中添加的)
  • 我不知道如何将它绑定到事件

标签: c# winforms listbox selection


【解决方案1】:

您很接近,您只是忘记选择该项目。修复:

    private void listBox1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) {
            var item = listBox1.IndexFromPoint(e.Location);
            if (item >= 0) {
                listBox1.SelectedIndex = item;
                contextMenuStrip1.Show(listBox1, e.Location);
            }
        }
    }

【讨论】:

    【解决方案2】:
      private void lstFiles_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)   //(1)
            {
                int indexOfItemUnderMouseToDrag;
                indexOfItemUnderMouseToDrag = lstFiles.IndexFromPoint(e.X, e.Y); //(2)
                if (indexOfItemUnderMouseToDrag != ListBox.NoMatches)
                {
                    lstFiles.SelectedIndex = indexOfItemUnderMouseToDrag; //(3)
                }
            }
        }
    

    【讨论】:

    • 你能添加一些关于发生了什么的描述吗?
    【解决方案3】:

    每个控件都从 Control 类继承 ContextMenu 属性。将上下文菜单对象分配给列表框控件的 ContextMenu 属性,WinForms 会自动为您处理。

    【讨论】:

      【解决方案4】:
          private void listBox1_MouseUp(object sender, MouseEventArgs e)
          {
              if (e.Button== MouseButtons.Right)
              {
                  int nowIndex = e.Y / listBox1.ItemHeight;
                  if (nowIndex < listBox1.Items.Count)
                  {
                      listBox1.SelectedIndex = e.Y / listBox1.ItemHeight;
                  }
                  else
                  {
                      //Out of rang
                  }
              }
          }
      

      我对 C# 了解不多,但我试过了 :)

      【讨论】:

        【解决方案5】:

        我正在处理同样的问题。从 Hans Passant 的回复中,我对其进行了一些调整以获得以下代码。我还发现我根本不需要把contextMenuStrip1.Show(listBox1, e.Location); 放在那里。它是自动为我调用的。

        (我使用 Visual Studio 2010 Ultimate 并在 .NET 4 上编译。我还验证了以下代码适用于 MouseUp 和 MouseDown。)

            private void OnMouseDown(object sender, MouseEventArgs args)
            {
                if (args.Button == MouseButtons.Right)
                {
                    var item = this.IndexFromPoint(args.Location);
                    if (item >= 0 && this.SelectedIndices.Contains(item) == false)
                    {
                        this.SelectedItems.Clear();
                        this.SelectedIndex = item;
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 2012-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多