首页 > 解决方案 > 鼠标移动时更新表单文本

问题描述

我有一个简单的表单,我想在用户移动鼠标时显示不断更新的光标位置。我遇到的问题是移动鼠标时文本没有更新。

public void mouse_position(object sender, MouseEventArgs e)
 {
    TextBox textBox1 = new TextBox();
    Label label1 = new Label();

    // Initialize the controls and their bounds.

    label1.Location = new Point(1400, 500);
    label1.Size = new Size(10, 10);
    label1.BringToFront();
    label1.BackColor = Color.Aqua;

    // Add the Label control to the form's control collection.
    Controls.Add(label1);
    label1.Text = Cursor.Position.Y.ToString();

 }

就像我说的,它给了我最初的鼠标位置,但从不更新

标签: c#mouseevent

解决方案


我想你想要这样的东西:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   int mouseX = e.X;
   int mouseY = e.Y;

   textBox1.Text = "X: " + e.X.ToString() + "Y: " + e.Y.ToString();

}

基本上每次您在表单上移动鼠标时,textbox1 都会更新鼠标的 X/Y 位置。

输出(仅用于演示目的):

在此处输入图像描述


推荐阅读