Adding and/or removing textbox and label with button on UserForm

harveya915

Board Regular
Joined
Sep 4, 2015
Messages
141
I have a UserForm with TextBoxes, Labels, CommandButtons, and a Checkbox.

I need that for when the checkbox value is true for a new label and textbox to appear. I have this part figured out.

However what I need now is for when I uncheck the the checkbox for those new label and textbox to go away.

The checkbox value is false by default. So when I first open the UserForm, the checkbox is not selected (value=false). I would like for my code to be able to where if I select (true) the extra label and checkbox appear, but if I change my mind I unselect (false) the checkbox and they dissapear, but if I check again they appear, and so on and so forth.

Oh and when the new label and textbox appears, certain commandbuttons, labels, and textboxes move to make way for the new ones. and when checkbox is unselected they go back to their original spot.

This is what I have so far:

VBA Code:
Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then

'this is to shift down and make way for new label and textbox
CommandButton1.Top = 312
CommandButton1.Left = 144
CommandButton2.Top = 312
CommandButton2.Left = 252
Label7.Top = 354
Label7.Left = 30
TextBox7.Top = 366
TextBox7.Left = 30
CommandButton3.Top = 498
CommandButton3.Left = 198

'this renames some labels
Label3.Caption = "Amount of Payment Increase"
Label4.Caption = "New Payment Amount"
Label5.Caption = "First Additional Payment Due"
 
'this adds the new textbox and label to a certain position
Dim ctlTxt As MSForms.Control
Dim ctlLbl As MSForms.Control
    Set ctlTxt = Me.Controls.Add("Forms.TextBox.1")
    ctlTxt.Top = 276
    ctlTxt.Left = 222
    ctlTxt.Height = 24
    ctlTxt.Width = 120
    ctlTxt.BorderStyle = 0
    ctlTxt.SpecialEffect = 0
    ctlTxt.BackColor = &H80000018
    ctlTxt.Value = ""
    Set ctlLbl = Me.Controls.Add("Forms.Label.1")
    ctlLbl.Top = 276
    ctlLbl.Left = 96
    ctlLbl.Height = 24
    ctlLbl.Width = 120
    ctlLbl.Font.Size = 10
    ctlLbl.Caption = "First Additional Payment Due Date"

End If

If CheckBox1.Value = False Then

'this puts them back to their original place
CommandButton1.Top = 270
CommandButton1.Left = 144
CommandButton2.Top = 270
CommandButton2.Left = 252
Label7.Top = 318
Label7.Left = 30
TextBox7.Top = 330
TextBox7.Left = 30
CommandButton3.Top = 462
CommandButton3.Left = 198

'puts original caption back on renamed labels
Label3.Caption = "# of Monthly Payments"
Label4.Caption = "Amount of Payment"
Label5.Caption = "First Due Date"
End If

End Sub

I don't have much experience with this. Everything has been by research here and there. Much thanks!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
It is a best practice to keep controls in the same place if you are continuing to show the same form, so as not to disorient the user. Having the same controls shift positions to reveal new controls, possibly in places where other controls were just a second ago, can be confusing. A better approach would be to create the label and textbox statically in the designer, and make Visible=False. Then when the checkbox is clicked simply set the Visible property to match the checkbox value:
VBA Code:
Private Sub CheckBox1_Click()

   TextBox1.Visible = CheckBox1.Value
   Label1.Visible = CheckBox1.Value

End Sub

The user still sees all controls in the same place but with a new textbox appearing. Not only is this clearer to the user, but it's a lot simpler for you as a developer.
 
Upvote 0
Solution
It is a best practice to keep controls in the same place if you are continuing to show the same form, so as not to disorient the user. Having the same controls shift positions to reveal new controls, possibly in places where other controls were just a second ago, can be confusing. A better approach would be to create the label and textbox statically in the designer, and make Visible=False. Then when the checkbox is clicked simply set the Visible property to match the checkbox value:
VBA Code:
Private Sub CheckBox1_Click()

   TextBox1.Visible = CheckBox1.Value
   Label1.Visible = CheckBox1.Value

End Sub

The user still sees all controls in the same place but with a new textbox appearing. Not only is this clearer to the user, but it's a lot simpler for you as a developer.
You were right! Much easier and simpler! Don't know why I didn't think of it that way.

For those that could use this, here's what I did:
Created the new Label6 and TextBox6. In their Properties box I set "Visible" to False. Therefore they are not shown when the UserForm is opened.
Still used my code mentioned previously but added @6StringJazzer suggested code to the "IF" statements

VBA Code:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
TextBox6.Visible = True
Label6.Visible = True
'include other code
End If

If CheckBox1.Value = False Then
TextBox6.Visible = False
Label6.Visible = False
'include other code
End If
End Sub

I still kept most of the original code as mentioned in my initial post so that it would move down other Labels, TextBoxes, and CommandButtons to make room for the new Label6 and TextBox6. And vice-versa when hiding the new Label6 and TextBox6 it moves up the other Labels, TextBoxes, and CommandButtons to their original places. At then end this is the code that I ended up with:

VBA Code:
Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then
CommandButton1.Top = 312
CommandButton1.Left = 144
CommandButton2.Top = 312
CommandButton2.Left = 252
Label7.Top = 354
Label7.Left = 30
TextBox7.Top = 366
TextBox7.Left = 30
CommandButton3.Top = 498
CommandButton3.Left = 198

Label3.Caption = "# of Increased Payments"
Label4.Caption = "Amount of Payment Increase ($)"
Label5.Caption = "New Payment Amount ($)"
 
Label8.Visible = True
TextBox6.Visible = True

End If

If CheckBox1.Value = False Then

CommandButton1.Top = 270
CommandButton1.Left = 144
CommandButton2.Top = 270
CommandButton2.Left = 252
Label7.Top = 318
Label7.Left = 30
TextBox7.Top = 330
TextBox7.Left = 30
CommandButton3.Top = 462
CommandButton3.Left = 198

Label8.Visible = False
TextBox6.Visible = False

Label3.Caption = "# of Monthly Payments"
Label4.Caption = "Amount of Payment ($)"
Label5.Caption = "First Due Date"
End If

End Sub

I have attached some images.
Image 1 - Shows the UserForm in design mode
Image 2 - Shows UserForm in use without CheckBox selected
Image 3 - Shows UserForm in use with CheckBox selected, displaying new label and textbox with with other controls moved down.

Hope this helps someone else! Much thanks to @6StringJazzer
 

Attachments

  • Image1.png
    Image1.png
    26.5 KB · Views: 13
  • Image2.png
    Image2.png
    12.6 KB · Views: 13
  • Image3.png
    Image3.png
    15.8 KB · Views: 13
Upvote 0
I am glad you felt my advice was worthy. Sometimes people have reasons for doing what they want to do despite my perspective. Glad it worked!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top