2017-12-12 5 views
0

OK。これは私をナットにしている。私はまだ永遠のループをしています。 私の以前の質問はこれまでに私を得ました>>>Using VBA code to modify OptionButton.value is activating the control.Click() SubVBAコードを抑制するcontrol.Click()サブ/イベントハンドラ

私はこれを試して試してみるためにいくつかのコード例を書き直しました。 私は、他の提案のとおり、 (例:Suppress events in UserForms)のようにブール値のハンドラを設定しました。 (SwitchOnEventResponder

クリックイベントが発生するコードに到達すると、ハンドラはTrue(これは_Click()サブをアクティブにする値の変更が可能)に戻ります。 OptionsDLG.OptionButton2.Value = True ステートメントが実行されます。どうして?

モジュール内のコード:

Sub StartDLG() 
Call changeframe(135) 
'code to change the UserForm (to hide the text box) 
Call PopulateText 
OptionsDLG.Show 
End Sub 

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
    If CloseMode = 0 Then Cancel = True 
End Sub 

Sub PopulateText() 
' Populates text box with initial text 
OptionsDLG.TextBox1.Value = "Enter your msg here" 
End Sub 

Sub changeframe(FormH As String) 

OptionsDLG.Height = FormH 
OptionsDLG.Label1.Visible = False 
OptionsDLG.TextBox1.Visible = False 
End Sub 

ユーザーフォーム "OptionsDLG" コード:

Public SwitchOnEventResponder As Boolean 

Private Sub UserForm_Initialize() 
     Me.SwitchOnEventResponder = True 
End Sub 

Private Sub OptionButton2_Change() 
     MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder 
     If Me.SwitchOnEventResponder = False Then 
      Exit Sub 
     End If 
    End Sub 

Private Sub OptionButton1_Click() 

Unload Me 
Call changeframe(135) 
'code to change the UserForm 
Call PopulateText 
OptionsDLG.Show 

End Sub 

Private Sub OptionButton2_Click() 
If Me.SwitchOnEventResponder = False Then 
      Exit Sub 
End If 

Unload Me 

Call PopulateText 

Me.SwitchOnEventResponder = False 
MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder 
OptionsDLG.OptionButton1.Value = False 
OptionsDLG.OptionButton2.Value = True '<< This is where the loop starts. grrrr 
Me.SwitchOnEventResponder = True 
OptionsDLG.Show 
End Sub 

' This runs when the Ok button is clicked 
Private Sub OK_Click() 
    If OptionButton1.Value = True Then MsgBox "HAPPY" 
    If OptionButton2.Value = True Then MsgBox "HAPPIER" 
End Sub 

' This runs when the cancel button is clicked 
Private Sub Cancel_Click() 
    ' Show the main dialogue 
    Stop 
End Sub 

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
      If CloseMode = 0 Then Cancel = True 
End Sub 
+0

「public SwitchOnEventResponder As Boolean」は、userformではなく別のモジュールで宣言しようとしてください。私はこの方法とその作業を使用します。 – Sphinx

+0

ちょうど好奇心から、なぜUserformでなく、モジュールの 'Private Sub Userform_QueryClose'ですか? – SilentRevolution

+0

@Sphinx - userformから宣言をモジュールに移動しました。これでuserformは 'Me.SwitchOnEventResponder = True'ステートメントを認識しません – Joe90

答えて

0

それはあなたのMsgBoxメッセージ

MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder & vbCrLf & "OptionsDLG.SwitchOnEventResponder = " & OptionsDLG.SwitchOnEventResponder 

を変更するのに役立ちます、問題を理解するためにenter image description here

Meは、オブジェクトの現在のインスタンスを参照します。 Unload Meを呼び出すと、このインスタンスがUserformsコレクションから削除されます。読み込まれていないUserformで変数またはコントロールを設定すると、新しいUserFormが自動的にインスタンス化されます。ユーザーフォームMeの元のインスタンスは、ユーザーフォームの新しいインスタンスを参照しません。

Me.SwitchOnEventResponder = FalseからOptionsDLG.SwitchOnEventResponder = Falseに変更すると問題が解決します。

現在のインスタンスをアンロードする前に、ユーザーフォームの新しいインスタンスを作成することをお勧めします。 Withステートメントを使用すると、すべての変数とコントロールが正しいインスタンスに完全修飾されます。

With New OptionsDLG 
    .SwitchOnEventResponder = False 
    .OptionButton1.Value = False 
    .OptionButton2.Value = True 
    .SwitchOnEventResponder = True 
    .Show 
End With 

Unload Me 
+0

それは素晴らしいです。私の母親のコードは今働きます。 – Joe90

関連する問題