vb.net 在 Visual Basic 中更改光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15212426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change cursor position in visual basic
提问by Alpagut
I want to change the position of the cursor over a particular label. I use:
我想更改光标在特定标签上的位置。我用:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Position = Label17.Location
End Sub
but it doesn't change where I want it. I tried:
但它不会改变我想要的地方。我试过:
Label16.Location = Label17.Location
And this move the label16 properly.
这将正确移动 label16。
So How do we move the cursor to the location of label17 or any label/object.
那么我们如何将光标移动到 label17 或任何标签/对象的位置。
回答by Jamby
The problem is that Label.Location (and any other Control.Location) refers to location relative to the upper-left corner of its container. This mean that you must call the PointToScreen method of the parent container. In a simple application that just have a Form without any other container, it will be:
问题在于 Label.Location(和任何其他 Control.Location)是指相对于其 container 左上角的位置。这意味着您必须调用父容器的 PointToScreen 方法。在一个只有一个没有任何其他容器的 Form 的简单应用程序中,它将是:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Position = Me.PointToScreen(Label17.Location)
End Sub
A more elegant one:
一个更优雅的:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Position = Label17.Parent.PointToScreen(Label17.Location)
End Sub

