0

I'm creating a AddIn for Autodesk Inventor (3D drawing software), and for the moment I am playing with positional constraints.

I created a custom user menu for quick editing certain values, in this case the elevation and orientation value.

First I used the textbox.textchanged event to change my constraint value. But this was working not 100%. Example when pressing elevation 1000 would change the elevation 4 times ( on per digit ).

Now I went to using the validated event. This works better, BUT I would like to have the textbox to initiate validation when the Enter button is pressed. For this I whipped up this, but it's not correct i'm sure of it. How should I write this correctly?

The code below works but, I would like to have a proper way to achieve the result.

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
Mech_Engineer
  • 535
  • 1
  • 19
  • 46

1 Answers1

0

I think you're on the right way. It can be a pain to register a key_down event for each TextBox object, but your idea is good.

You can set a key_down event listener to your form instead.

Try something like this:

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
theBugger
  • 441
  • 3
  • 14
  • 2
    Will this not result that each time a keystroke is made, even if it's not in the `text-boxes` I want to monitor for events, the event is triggered? Will this not consume more resources than required? – Mech_Engineer Aug 29 '16 at 14:16
  • It's true, but you can easily add a test inside the KeyDown event to give focus to the form only if the current focused control is a textbox – theBugger Aug 30 '16 at 05:54