0

我正在为 Autodesk Inventor(3D 绘图软件)创建一个插件,目前我正在使用位置约束。

我创建了一个自定义用户菜单,用于快速编辑某些值,在本例中是高度和方向值。

首先,我使用该textbox.textchanged事件来更改我的约束值。但这不是 100% 有效的。按下海拔 1000 时的示例将更改海拔 4 次(每位数)。

现在我开始使用validated event. 这效果更好,但我希望在Enter按下按钮时让文本框启动验证。为此,我提出了这个,但我确定它是不正确的。我应该如何正确地写这个?

下面的代码有效,但我想有一个正确的方法来实现结果。

Private Sub tbElevationValue_TextChanged(sender As Object, e As EventArgs) _
    Handles tbElevation.Validated

    ' If the elevation parameter and textbox value are the same
    ' The sub must be aborted
    If CDbl(tbElevation.Text) = oElevationParameter.Value * 10 Then Exit Sub

    ' Check the entered value
    Dim oValue As Double
    If tbElevation.Text = "" Then
        oValue = 0
        tbElevation.Text = 0
    Else
        oValue = tbElevation.Text
    End If

    ' Set the parameter value
    oElevationParameter.Value = oValue / 10

    ' Update the document
    EM_AddIn.StandardAddInServer.m_inventorApplication.ActiveDocument.Update()

End Sub

Private Sub tbOrientation_TextChanged(sender As Object, e As EventArgs) _
    Handles tbOrientation.Validated

    ' If the orientation parameter and textbox value are the same
    ' The sub must be aborted
    If CDbl(tbOrientation.Text) = cRandiansToDegrees(oOrientationParameter.Value) Then Exit Sub

    ' Check the entered value
    Dim oValue As Double
    If tbOrientation.Text = "" Then
        oValue = 0
        tbOrientation.Text = 0
    Else
        oValue = tbOrientation.Text
    End If

    ' Set the parameter value
    oOrientationParameter.Value = cDegreesToRandians(oValue)

    ' Update the document
    EM_AddIn.StandardAddInServer.m_inventorApplication.ActiveDocument.Update()

End Sub

Private Sub OrientationElevationEnterKey_Pressed(sender As Object, e As Windows.Forms.KeyEventArgs) Handles tbElevation.KeyUp, tbOrientation.KeyUp

    If e.KeyCode = Windows.Forms.Keys.Enter Then

        CType(sender, Windows.Forms.TextBox).Parent.Focus()
        CType(sender, Windows.Forms.TextBox).Focus()

    End If


End Sub
4

1 回答 1

0

我认为你是在正确的方式。为每个 TextBox 对象注册一个 key_down 事件可能会很痛苦,但您的想法很好。

您可以为您的表单设置一个 key_down 事件侦听器。

尝试这样的事情:

Private Sub Main_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.KeyCode.Enter Then

        MyBase.Focus() ' this will cause the currently focused control to validate

    End If     
End Sub
于 2016-08-29T13:08:21.690 回答