从windows形式的光标位置获取x位置值到整数c#
Get x position value from cursor location in windows form to integer c#
如果我的鼠标光标位于右侧 (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
如果我的鼠标光标位于右侧 (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