2016-08-29 35 views
0

我正在为Autodesk Inventor(3D绘图软件)创建AddIn,并且目前我正在玩位置约束。输入按钮被按下时验证文本框

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

首先我使用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 

回答

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 
+2

这样不是会导致每一次击键制成,即使它不是在'文本boxes'我要监控的事件,该事件被触发?这是否会消耗比所需资源更多的资源? –

+0

确实如此,但您可以轻松地在KeyDown事件中添加测试,以便仅在当前的焦点控件是文本框 – theBugger