C# gui 的鼠标移动事件不能select 一个列表元素?

C# gui the mouse move event cannot select a list element?

这个标题可能有点不准确,我真正遇到的是这个问题。我画了几行,每行都存储在一个列表中,作为行路径。然后我使用 IsOutlineVisible 来测量鼠标位置是否在其中任何一个上,如果鼠标在其中一个上,则将其绘制为不同的颜色或做一些事情。 但它只识别列表中的最后一行。

我只会附上代码的相关部分,但是我真的不太确定问题出在哪里,所以这是完整的代码

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{


    List<GraphicsPath> LineGroup = new List<GraphicsPath>();
    Point Latest{get;set;}
   List<Point> pointtemp = new List<Point>();
   bool startdrawline = true;
   bool selectlinestate = false;
   int selectedline;


    public Form1()
    {
        InitializeComponent();
    }



    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        // Save the mouse coordinates
        Latest = new Point(e.X, e.Y);

        // Force to invalidate the form client area and immediately redraw itself. 
        Refresh();

        if (startdrawline == false)
        {
            selectedline = Linesel(LineGroup);
        }

    }


    protected override void OnPaint(PaintEventArgs e)
    {
        this.DoubleBuffered = true;

        var g = e.Graphics;
        base.OnPaint(e);



        Pen penb = new Pen(Color.Navy,2);
        Pen peny=new Pen(Color.Yellow,2);


                    for (int i = 0; i < LineGroup.Count; i++)
        {
            if (i == selectedline)
            {
                g.DrawPath(peny, LineGroup[i]);
            }
            else
            {
                g.DrawPath(penb, LineGroup[i]);
            }
        }
        penb.Dispose();
        peny.Dispose();



        if (startdrawline == true)
        {
            GraphicsPath tracepath = new GraphicsPath();
            Pen penr = new Pen(Color.Red,2);

            if (pointtemp.Count == 1)
            {
                tracepath.AddLine(pointtemp[0], Latest);

            }
            else if (pointtemp.Count > 1)
            {
                tracepath.AddLine(pointtemp[1], Latest);
            }

            g.DrawPath(penr, tracepath);
            penr.Dispose();

        }
        Refresh();

    }







    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {



        if (e.Button == MouseButtons.Right)
        {
            startdrawline = false;
            pointtemp.Clear();
        }
        else if (e.Button == MouseButtons.Left )
        {
            startdrawline = true;


            Latest = new Point(e.X, e.Y);


            if (pointtemp.Count < 2)
            {
                pointtemp.Add(Latest);
            }
            else
            {
                pointtemp[0] = pointtemp[1];
                pointtemp[1] = Latest;


            }


            if (pointtemp.Count == 2)
            {
                LineP2P(pointtemp);
            }


            Refresh();
        }

    }




    private void LineP2P(List<Point> pointtemp){
        GraphicsPath path = new GraphicsPath();
        path.AddLine(pointtemp[0], pointtemp[1]);
        LineGroup.Add(path);

    }


 private int  Linesel(List<GraphicsPath> LineGroup)
    {
        int selectedline=-1;
        for (int i =0; i < LineGroup.Count; i++)
        {
            Pen pen = new Pen(Color.Navy, 8);
            if (LineGroup[i].IsOutlineVisible(Latest, pen))
            {
                selectedline = i;

            }
            else if (!LineGroup[i].IsOutlineVisible(Latest, pen))
            {
                selectedline = -1;

            }
            label1.Text = selectedline.ToString();
        }
        return selectedline;
    }



}
}

当我把测试label.Text放在if

if (LineGroup[i].IsOutlineVisible(Latest, pen))
            {
                selectedline = i;
                label1.Text = selectedline.ToString();
            }

它确实有效(因线而异) 任何人都可以找出原因吗?非常感谢。

问题是你找到线后没有跳出循环

假设您的鼠标所在的行位于索引 0。您将调用
selectedline = 0 第一次迭代
然后在第二次迭代

if (LineGroup[1].IsOutlineVisible(Latest, pen))

将是错误的,所以

selectedline = -1;

因此,除非您将鼠标放在最后一行,否则所选行将始终为 -1

你想做的大概是

    selectedline = -1;
    for (int i =0; i < LineGroup.Count; i++)
    {
        Pen pen = new Pen(Color.Navy, 8);
        if (LineGroup[i].IsOutlineVisible(Latest, pen))
        {
            selectedline = i;
            break;

        }
    }

    label1.Text = selectedline.ToString();
    return selectedline;