winforms 单击对象后更新窗体中的值C#

gr8qqesn  于 2022-11-16  发布在  C#
关注(0)|答案(1)|浏览(160)

我有这段代码,它运行一个函数(MouseTracker)后,点击标签这个MouseTracker,应捕捉鼠标位置x和y后,按下确定对话框的结果。

namespace AdvancedStashHelper
{
    public partial class Settings : Form
    {
        public int xTemp;
        public int yTemp;

        public Settings()
        {
            InitializeComponent();
        }

        private void MouseTracker()
        {
            while (true)
            {
                var result = MessageBox.Show("Move your cursor to position and press ENTER");

                if (result == DialogResult.OK)
                {
                    xTemp = MousePosition.X;
                    yTemp = MousePosition.Y;
                }
                break;
            }
        }

        private void orbTransLabel_Click(object sender, EventArgs e)
        {
            Thread thread = new(MouseTracker);
            thread.Start();

            orbTransXPos.Text = xTemp.ToString();
        }

        private void orbAltLabel_Click(object sender, EventArgs e)
        {
            Thread thread = new(MouseTracker);
            thread.Start();
        }
    }
}

这里的问题是,当你第一次点击标签时,什么都没有发生,但在你第二次点击后,值会更新为以前的鼠标位置。
其功能如下:
1.点击标签
1.运行对话框,说按回车捕捉鼠标位置
1.按“确定”后更新表单上的值
1.对具有不同鼠标位置值的所有标注执行此操作

mnemlml8

mnemlml81#

感谢所有您的信息,代码是简单的,因为它是

namespace AdvancedStashHelper
{
    public partial class Settings : Form
    {
        public int xTemp;
        public int yTemp;

        public Settings()
        {
            InitializeComponent();
        }

        private void MouseTracker()
        {
            var result = MessageBox.Show("Move your cursor to position and press ENTER");

            if (result == DialogResult.OK)
            {
                xTemp = MousePosition.X;
                yTemp = MousePosition.Y;
            }
        }

        private void orbTransLabel_Click(object sender, EventArgs e)
        {
            MouseTracker();

            orbTransXPos.Text = xTemp.ToString();
            orbTransYPos.Text = yTemp.ToString();
        }

        private void orbAltLabel_Click(object sender, EventArgs e)
        {
            MouseTracker();

            orbAltXPos.Text = xTemp.ToString();
            orbAltYPos.Text = yTemp.ToString();
        }
    }
}

相关问题