Modify Last Selected Button

0

I have a UserForm containing several 1-20 selection buttons.

When I press the Red Dye or Dye button, the last selected button should be dyed red or blue. Could someone help me work out this Sub?

    
asked by anonymous 22.08.2018 / 14:45

1 answer

1

You will need to use Class modules to do this generically and a form.

Form

AnexamplewiththefollowingForm:

WhereNameandCaptionpropertiesarethesameintheimageexample.

Containing4buttonsforthetestsandalabeltostorethevalueofthelastbuttonclicked.

Followthecodewiththeinitializationoftheclassmoduleintheformandmakingthelabelinvisible.

PrivatecollBtnsAsCollectionPrivateSubUserForm_Initialize()'Crédito:www.andypope.info'www.cpearson.com/excel/Events.aspxDimcls_btnAsClasse1SetcollBtns=NewCollectionMe.Label1.Visible=FalseForEachctrlInMe.ControlsIfTypeName(ctrl)="CommandButton" Then
            Set cls_btn = New Classe1
            Set cls_btn.btn = ctrl
            collBtns.Add cls_btn, CStr(collBtns.Count + 1)
        End If
    Next ctrl
End Sub

Class Module

This code is inserted into the class module Classe1

    Public WithEvents btn As MSForms.CommandButton

Private Sub btn_Click()
    Dim ultimo_botao As MSForms.Control
    Set ultimo_botao = UserForm1.Controls(UserForm1.Label1.Caption)
    If btn.Name = "TingirVermelho" Then
        ultimo_botao.BackColor = RGB(255, 0, 0)
        ultimo_botao.ForeColor = RGB(255, 255, 255)
    ElseIf btn.Name = "TingirAzul" Then
        ultimo_botao.BackColor = RGB(0, 0, 255)
        ultimo_botao.ForeColor = RGB(255, 255, 255)
    Else
        UserForm1.Label1.Caption = btn.Name
    End If
End Sub

In this, the button click event is declared in btn_Click() .

If the button name is "TingirVermelho" or "TingirAzul" , it changes the color of the last selected button with .BackColor and .ForeColor . Other changes can be made to the button. See Button Properties

Otherwise, store the name of the button in Label1 .

Result

    
23.08.2018 / 15:41