Click here to Skip to main content
16,018,637 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys,
I have about 20 checkboxes in a UserForm.
All checkboxes have to call a similar function when they get checked or uncheckt:
VB
Private Sub CheckBox2_Click()
    TextBox4.Visible = True
        If CheckBox2.Value = True Then
        TextBox4.ForeColor = RGB(0, 0, 0)
        TextBox4.Locked = False
    ElseIf CheckBox2.Value = False Then
        TextBox4.ForeColor = RGB(127, 127, 127)
        TextBox4.Locked = True
    End If
End Sub

CheckBox3 would just have number 3 instead of 2 and all 4 would be replaced by nnumber 5. So The TextBox is always the number of the checkbox+2.

My problem:
It's not smart to write 20 times a similar function. How can I do this in one or 2 functions?
I have no idea. I'm a beginner in VBA
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jul-15 9:19am    
I would rather say, "I'm not an idiot to write 20 times a similar function". So, you cannot say that you are not smart. But it's not clear what's the problem. Of course it should be only one function. Generally, in programming nothing should be repeated.
—SA
Chi Ller 13-Jul-15 9:38am    
Yes. Well every Checkbox has a unice code:
checkbox1_click()
checkbox2_click()
I need something like:
checkbox(number)_click()
And every CheckBox has to call this function and not his own. But i have no idea and the solutions i found on google always had an error. Also if i copied the sourcecode
Sergey Alexandrovich Kryukov 13-Jul-15 9:47am    
It's is either unique, then you would really need individual handlers, or not, then you would need only one handler method. The code sample in the question shows no sign of uniqueness. Please see my detailed answer.
—SA

1 solution

Please see my comment to the question. See also: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

The question looks extremely trivial, but, come to think about, it is not. Here is why. Your problem is not that it would be difficult to write one universal function. The problem is that you have all those CheckBox2 and TextBox4 in principle. The problem is that you already created them in a way too manual way — using the designer. It was manual clicking many times. You see, your code shows that CheckBox2 is in certain relationship with TextBox2. But what is the relationship between CheckBox2 and some other control? There is no a way to calculate it automatically, because all you have are only ad-hoc names of controls, which means nothing.

Of course, you can abstract out these two controls to make then two parameters of some function. But anyway, you will have to write those 20 event handlers and call this function 20 times from those handlers, which would make only one line of the implementation code of each handler. Maybe you would be happy with such advice, but I would be disgusted with it.

Your problem that you already gone wrong with the form. I would redo it. Just the fact you have names line CheckBox2 and TextBox4 is a good indicator of wrong code design (you should never use auto-generated names and rename all controls using refactoring language, to give semantic name and observe good Microsoft naming conventions, but it cannot help you in this case). You have to have arrays of controls. First idea is to create array on check boxes and another array, of text boxes, and then your code would use the the arrays elements accessed by index, something like if checkBox(index).Value then texBox(index)… but then you would write some code populating the arrays with the control members of the form. At least you can do it only once.

But it also would be not so good. Why would you even need those members, if you really need only one array for all those check boxes (maybe, not all of them, but those in the similar relationships with the elements of the array of text boxes), and only one array of those text boxes, and so on? The designer won't create those arrays for your?

The answer is: design properly. Perhaps you don't need that many check boxes (it already sounds suspicious, maybe you can redesign it), but let's say this is a reasonable design, so you really need 20 check boxes and related text boxes. As the designer won't do right thing, don't use the designer. You need to create all those controls in code, in a loop. This is the key. And add only one handler of each event instance to each of those controls, in a loop. You only need to pass the index to the handler method. The handler methods takes only two parameters, sender (System.Object) and event arguments (System.EventArgs or derived class). One possible technique is this: create regular values of TabIndex for all controls. Then you can cast sender to correct control type (which you know in the code of the handler), and then calculate the array index out of TabIndex. Alternatively, you could use anonymous methods for handler and closure (https://en.wikipedia.org/wiki/Closure_%28computer_programming%29[^]).

—SA
 
Share this answer
 
Comments
Chi Ller 14-Jul-15 2:26am    
Well I'll try to do it without the designer and to create the checkboxes and textboxes in an array. Your solution sounds to be the right way but atm I don`t exactly know how to realize it. Seems to me like I have to do much research :D
Btw: How can I create a CheckBox for the Userform in vba? I don't find someting on google ://
Sergey Alexandrovich Kryukov 14-Jul-15 2:42am    
I hope you got the main idea. I would suggest you try to implement it and ask new questions if you face some particular problems with that. "Much research" is probably somewhat exaggerated.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900