1

I want to write a code for my form that when user clicks on each of text boxes to enter the data, textbox background change to green.

Something like this - in form current event:

Dim c as control For each c In me.controls If c.OnClick then C.backcolor= vbGreen C.tag="clicked" End If If c.AfterUpdate and c.tag="clicked" then C.backcolor= vbWhite C.tag="" Next c 

How can I find out when an event is triggered?

1
  • I don't want to call the event for each textbox separately and one by one Commented Sep 14, 2020 at 12:40

2 Answers 2

1

You will use WithEvents for that. This is poorly documented, but an example can be found in my project (too much code to post here):

VBA.ModernTheme

Full documentation and further links are in my article on the project:

Create Windows Phone Colour Palette and Selector using WithEvents

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Your program is very applicable and its WithEvents part help me so much. Thank you again.
1

You can create two functions in your form code as follows:

Private Function SetBackColor() Me.ActiveControl.BackColor = vbGreen End Function Private Function ResetBackColor() Me.ActiveControl.BackColor = vbWhite End Function 

Then set the On Got Focus property of all the text boxes to =SetBackColor() and the On Lost Focus property of all text boxes to =ResetBackColor()

This can be done easily by selecting all text boxes and write the property once and it will apply to all of them (no need to repeat the writes).

This will work whenever the user tries to edit any text box even if it was reached by TAB or ENTER buttons not only mouse click.

1 Comment

Thank you for your solution but it is a bit hand-operated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.