Color the selected textbox

Solved
DEVPLUS Posted messages 30 Status Member -  
f894009 Posted messages 17417 Registration date   Status Member Last intervention   -
Bonjour,
I have 43 text boxes, and I would like to replace the formulas below
for example:
Private Sub TextBox2_Enter()
TextBox2.BackColor = vbYellow
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox2.BackColor = vbWhite

End Sub
Private Sub TextBox3_Enter()
TextBox3.BackColor = vbYellow
End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox3.BackColor = vbWhite

End Sub
with a simpler formula that encompasses everything, like for example objcontrol, which I can't manage to solve

Private Sub TextBox_Enter()
for each objcontrol in U1.controls
if type of objcontrol is ms form.textbox then
objcontrol.backcolor=vbYellow

End Sub
it doesn't work

Configuration: Windows / Chrome 69.0.3497.100

3 answers

  1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,

    Private Sub TextBox2_Enter() TextBox_Enter End Sub Private Sub TextBox_Enter() Dim objcontrol As Object For Each objcontrol In U1.Controls If TypeOf objcontrol Is msform.TextBox Then objcontrol.BackColor = vbYellow End If Next End Sub

    But all the textboxes will be yellow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    0
    1. DEVPLUS Posted messages 30 Status Member
       
      It is not what I want, the selected textbox must be colored and once deselected it becomes white again.
      0
  2. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,

    Is it you who wrote this:
    objcontrol that I can't solve
    Private Sub TextBox_Enter() for each objcontrol in U1.controls if type of objcontrol is ms form.textbox then objcontrol.backcolor=vbYellow End Sub 

    it doesn't work

    and I wrote this with the code I wrote:
    But all the textboxes will be yellow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    It's quite simple to solve, are you doing it or do we help you??
    0
    1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
       
      Re,
      You will do for the rest of the TextBoxes
       Private Sub TextBox2_Enter() Call TextBox_Enter(2, "En") End Sub Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean) Call TextBox_Enter(2, "Ex") End Sub Private Sub TextBox3_Enter() Call TextBox_Enter(3, "En") End Sub Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean) Call TextBox_Enter(3, "Ex") End Sub Private Sub TextBox4_Enter() Call TextBox_Enter(4, "En") End Sub Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean) Call TextBox_Enter(4, "Ex") End Sub Private Sub TextBox_Enter(NTB, Ev) Dim objcontrol As Object If Ev = "En" Then coul = vbYellow ElseIf Ev = "Ex" Then coul = vbWhite End If Me.Controls("Textbox" & NTB).BackColor = vbYellow For Each objcontrol In Me.Controls If TypeOf objcontrol Is MSForms.TextBox And objcontrol.Name <> "TextBox" & NTB Then objcontrol.BackColor = vbWhite End If Next End Sub 
      0
  3. DEVPLUS Posted messages 30 Status Member
     
    Your proposal doesn't suit me, what I put works well, I just wanted to reduce the code,
    not resolved.
    0
    1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
       
      Hello,
      it doesn't suit me, just reduce the code
      Class module for textbox, you will find it on google.........
      0