Create class module for option buttons

YasserKhalil
PlatinumLounger
Posts: 4930
Joined: 31 Aug 2016, 09:02

Create class module for option buttons

Post by YasserKhalil »

Hello everyone
I have multiple option buttons on userfom and I need simply to do two things if any option button is clicked:
first to clear the textbox named `txtSearch`
second to clear the listbox named `lstResults`

I have more than 10 option buttons and it is not logical to repeat the same procedure for each option button

Code: Select all

Private Sub optName_Change()
    With Me
        .txtSearch = Empty
        .lstResults.Clear
    End With
End Sub

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create class module for option buttons

Post by HansV »

Class module named clsOpt:

Code: Select all

Option Explicit

Public WithEvents opt As MSForms.OptionButton

Private Sub opt_Click()
    With UserForm1
        .txtSearch = Empty
        .lstResults.Clear
    End With
End Sub
In the userform module:

Code: Select all

Option Explicit

Dim arrOpt() As New clsOpt

Private Sub UserForm_Initialize()
    Dim ctl As MSForms.Control
    Dim i As Long
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.OptionButton Then
            i = i + 1
            ReDim Preserve arrOpt(1 To i)
            Set arrOpt(i).opt = ctl
        End If
    Next ctl
End Sub

' plus other code of course
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4930
Joined: 31 Aug 2016, 09:02

Re: Create class module for option buttons

Post by YasserKhalil »

Thanks a lot my tutor.
But the name UserForm1 hard-coded in the class and I need to make it available for more than a userform. also the reference to textbox and the listbox need them to be passed as parameters or arguments.
Regards

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create class module for option buttons

Post by HansV »

Class module:

Code: Select all

Option Explicit

Public WithEvents opt As MSForms.OptionButton
Public txt As MSForms.TextBox
Public lst As MSForms.ListBox

Private Sub opt_Click()
    txt = ""
    lst.Clear
End Sub
Userform module:

Code: Select all

Option Explicit

Dim arrOpt() As New clsOpt

Private Sub UserForm_Initialize()
    Dim ctl As MSForms.Control
    Dim i As Long
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.OptionButton Then
            i = i + 1
            ReDim Preserve arrOpt(1 To i)
            Set arrOpt(i).opt = ctl
            Set arrOpt(i).txt = Me.txtSearch
            Set arrOpt(i).lst = Me.lstResults
       End If
    Next ctl
End Sub
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4930
Joined: 31 Aug 2016, 09:02

Re: Create class module for option buttons

Post by YasserKhalil »

Amazing. Thank you very much for your support all the time.
Best Regards

snb
4StarLounger
Posts: 584
Joined: 14 Nov 2012, 16:06

Re: Create class module for option buttons

Post by snb »

Simpler:

Code: Select all

Public WithEvents m_opt As MSForms.OptionButton

Private Sub m_opt_Click()
  With m_opt.parent
    .txtSearch = ""
    .lstResults.Clear
  End With
End Sub