0
点赞
收藏
分享

微信扫一扫

winfrom ListBox根据鼠标位置选中项

卿卿如梦 2022-04-19 阅读 26
c#windows
 private void PopUpListBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (SelectItem != null)
            {
                GetItemAt(this, e.X,e.Y);
               // MessageBox.Show("MouseDown" + base.SelectedIndex.ToString());
                SelectItem(Keys.Enter);
            }
        }
        private object GetItemAt(ListBox listBox, int X, int Y)
        {
            try
            {
                object res = null;
                for (int i = 0; i < listBox.Items.Count; i++)
                {
                    System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
                    if (r.Contains(new Point(X, Y)))
                    {
                        res = listBox.Items[i];
                        base.SelectedItem = listBox.Items[i];
                        break;
                    }
                }
                return res;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
举报

相关推荐

0 条评论