C# 从windows窗体中的光标位置到整数c获取x位置值#

C# 从windows窗体中的光标位置到整数c获取x位置值#,c#,integer,int,var,C#,Integer,Int,Var,如果我的鼠标光标(this.Width-200)位于右侧,我当前正在尝试显示一个选项。是否可以获取变量的整数值 到目前为止,我已经完成了值的来回转换,它看起来很混乱,甚至崩溃 代码如下: private void richTextBox1_MouseMove(object sender, MouseEventArgs e) { var relativePoint = this.PointToClient(Cursor.Position); //Output will look somet

如果我的鼠标光标(this.Width-200)位于右侧,我当前正在尝试显示一个选项。是否可以获取变量的整数值

到目前为止,我已经完成了值的来回转换,它看起来很混乱,甚至崩溃

代码如下:

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
    var relativePoint = this.PointToClient(Cursor.Position); //Output will look something like this: "{X=1016,Y=237}"

    string Xpos = relativePoint.ToString(); //convert to string
    int startIndex = Xpos.IndexOf("{X=") + "{X=".Length;
    int endIndex = Xpos.IndexOf(",Y=");
    string newString = Xpos.Substring(startIndex, endIndex - startIndex); //Only get X value

    int XposINT = Int32.Parse(Xpos); //Convert to int

    if (XposINT >= this.Width - 200)
    {
        MessageBox.Show("cursor is at the edge of the form.");
    }

}
有更简单的方法吗?是否可以将变量直接转换为整数


请帮助

您可以简单地使用
relativePoint.X

为什么不只取点的X和Y?为什么要使用这些代码?点的X和Y属性已经存在you@Steve基本上是因为我不知道。这就是堆栈溢出的作用。其他人可能会来这里看你的评论,因为他们也不知道答案。另外一条评论,使用
光标。位置
也是不必要的。传递到事件处理程序中的
MouseEventArgs
已经有了鼠标坐标:
e.X
e.Y
。哇,太快了,非常感谢。我会在7分钟后确认答案。