Forum

textbox.setfocus is...
 
Notifications
Clear all

textbox.setfocus is executing textbox_exit event twice

3 Posts
2 Users
0 Reactions
149 Views
(@rajjindu)
Posts: 2
New Member
Topic starter
 

i have 3 checkbox,1 listbox and 3 textbox with values as 10. Iam adding some values based on check box1.value=true (i.e) AA,ABand ACto the listbox .
Iam adding some values based on check box2.value=true (i.e) BA,BBand BCto the same listbox .
Iam adding some values based on check box3.value=true (i.e) CA,CBand CCto the listbox .
if I select an item in the listbox(AA,AB,AC) and if the textbox1.text is >10 or null then I have to set the focus(text1.setfocus) in the listbox_change event
Private Sub ListBox1_Change()
For i = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(i) Then
if listbox1.list(i)="AA" then

If (CheckBox1.Value = True) and CInt(TextBox1.Text) >= 10 Then

TextBox1.SetFocus

End If

end if
if listbox1.list(i)="BA" then

If (CheckBox2.Value = True) and CInt(TextBox2.Text) >= 10 Then

TextBox2.SetFocus

End If
end if

end if
next I
end sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = False
If (CheckBox1.Value = True) Then

If (CInt(TextBox1.Text) >= 10 Or TextBox1.Text = Null) = True Then
MsgBox "PLEASE CHK THE value(<10) "
Cancel = True
TextBox1.SetFocus
Else
Cancel = False
Exit Sub
End If
End If
End Sub
in this code when I select AA in the Listbox1 I am getting the msg MsgBox "PLEASE CHK THE value(<10) " and the selected item in the listbox gets deselected and the focus goes to textbox1.this happens for the first chkbox other chkbox and txtbox works fine.
Pls Help

 
Posted : 29/05/2021 5:37 am
(@catalinb)
Posts: 1937
Member Admin
 

Hi Raj,

At the top of the form module, add this variable:
Private EnableFormEvents as Boolean

 

Changes to:

Private Sub ListBox1_Change()

EnableFormEvents=False

' your code

EnableFormEvents=True

End Sub

 

Changes to:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

if EnableFormEvents=False then Exit Sub

'your code

End sub

Basically, when you set focus on a textbox and the focus was on the other textbox, the form needs to Exit from one textbox to focus on the other box, so you need to disable form events.

 
Posted : 01/06/2021 1:58 pm
(@rajjindu)
Posts: 2
New Member
Topic starter
 

hai,

Iam not getting the error, but textexit event code is not doing its purpose(i.e) if I select a list in the listbox then the particular textbox value should be reduced from 10. if the value is 10 it should display the msgbox

 
Posted : 03/06/2021 1:03 am
Share: