检索鼠标位置

时间:2012-01-11 12:16:59

标签: c# winforms

我有以下代码,我试图检索光标/鼠标位置,当它在绘制的数组上方并输出到实验室时:

Label[ , ] _arr = new Label[4 , 4];

private void Form1_Load(object sender, EventArgs e) 
{  
    for(int i=0;i<4;i++)
    {    
        for(int j=0;j<4;j++)
           {
              _arr[i ,j] = new Label();        
              _arr[i ,j].Text = ""+i+","+j;  
              _arr[i ,j].Size = new Size(50,50);        
              _arr[i ,j].Location = new Point(j*50,i*50);         //you can set other property here like Border or else         
              _arr[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;   
              this.Controls.Add(_arr[i ,j]);
              **label2.Text = _arr[i, j].(System.Windows.Forms.Control.MousePosition).ToString();**
           }  
    }
}  

但我在双星号内遇到问题,有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。它可能不是最好的方法,但是当鼠标在表格中“阵列”中的每个单元格之间移动时,它会提供更新标签所需的结果:

private void Form1_Load(object sender, EventArgs e)
{
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    _arr[i, j] = new Label();
                    _arr[i, j].Text = "" + i + "," + j;
                    _arr[i, j].Size = new Size(50, 50);
                    _arr[i, j].Location = new Point(j * 50, i * 50);         //you can set other property here like Border or else         
                    _arr[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    this.Controls.Add(_arr[i, j]);

                    _arr[i, j].MouseMove += new MouseEventHandler(Form1_MouseMove);
                }
            }
}

void Form1_MouseMove(object sender, MouseEventArgs e)
{
    label1.Text = ((Label)sender).Text;
}