2016-08-29 79 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