Minimize & maximize userform with toggle button

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Minimize & maximize userform with toggle button

Post by adam »

Hi Anyone,

The following code when applied to the userform minimizes and maximizes the userform with the use of a toggle button. When the form is loaded it shows the maximized view of the form. & when the toggle button is clicked, it shows the minimized view.

How could the code be changed so that when the form is loaded, it shows only the minimized view and when the button is clicked it shows the maximized view.

Code: Select all

Dim dHeight As Double



Private Sub ToggleButton1_Click()

    If ToggleButton1.Value = True Then

        Me.Height = Me.Height * 0.25

    Else

        Me.Height = dHeight

    End If

End Sub



Private Sub UserForm_Initialize()

    dHeight = Me.Height

End Sub
Any help would be kindly appreciated.

Thanks in advance.
Last edited by HansV on 15 May 2010, 11:26, edited 1 time in total.
Reason: to correct typo in subject (Minimze > Minimize)
Best Regards,
Adam

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

Re: Minimze & maximize userform with toggle button

Post by HansV »

By setting the toggle button to True in the UserForm_Initialize event procedure:

Code: Select all

Private Sub UserForm_Initialize()
  dHeight = Me.Height
  Me.ToggleButton1 = True
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimze & maximize userform with toggle button

Post by adam »

Thank You Hans, for the help. I do really appreciate it. When I placed your line in my code and try to load the form it does not seem to load up. I did change the size or height of it.

What may be the reason for this?

What I’m trying to create is when the form is loaded , only the text boxes starting from First Name to the command buttons at the bottom of the from to be visible. And when I click the toggle button “Edit Record” the combo box and textbox Enter search parameter at the top to be visible.

How could I change the form accordingly to achieve this?

Workbook Uploaded for reference.
You do not have the required permissions to view the files attached to this post.
Best Regards,
Adam

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

Re: Minimze & maximize userform with toggle button

Post by HansV »

All declarations of variables and constants that are not local to a procedure or function must be placed at the top of the module. You placed the declaration of dHeight above the ToggleButton1_Click event procedure; that is not correct.

The userform will still not work, however, since your code refers to a text box txtRow that doesn't exist. And all defined names in your workbook are invalid.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimze & maximize userform with toggle button

Post by adam »

All declarations of variables and constants that are not local to a procedure or function must be placed at the top of the module. You placed the declaration of dHeight above the ToggleButton1_Click event procedure; that is not correct.
Does this referes that the

Code: Select all

Dim dHeight As Double


Should be placed below the code

Code: Select all

Private Sub ToggleButton1_Click()

    If ToggleButton1.Value = True Then

        Me.Height = Me.Height * 374.25

    Else

        Me.Height = dHeight

    End If

End Sub
Please let me know your response regarding the matter.
Best Regards,
Adam

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

Re: Minimze & maximize userform with toggle button

Post by HansV »

No, the line

Dim dHeight As Double

should be at the top of the module, below the line

Option Explicit

but before all Subs and Functions.
Best wishes,
Hans

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

Re: Minimze & maximize userform with toggle button

Post by HansV »

By the way, the line

Me.Height = Me.Height * 374.25

makes no sense - why would you like the form over 300 times as tall?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimze & maximize userform with toggle button

Post by adam »

Thanks for the reply & the help Hans.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimze & maximize userform with toggle button

Post by adam »

Me.Height = Me.Height * 374.25

makes no sense - why would you like the form over 300 times as tall?
I should I replace the line?
Best Regards,
Adam

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

Re: Minimze & maximize userform with toggle button

Post by HansV »

Change it to something that makes sense.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimze & maximize userform with toggle button

Post by adam »

Like what? is that without a decimal?
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimze & maximize userform with toggle button

Post by adam »

Ive understood what you have said Hans. I have changed the size. Thanks for letting me know
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimize & maximize userform with toggle button

Post by adam »

on the form in the attached workbook I have assigned a toggle button in order to minimize and maximize the user form. What I'm trying to do is when the user form is loaded only the first combo box, text box and the list box to be visible with the buttons in the user form.

When I click the edit button; the form to display the text boxes and combo boxes below the form by maximizing the form.

Any advice on how to do the above mentioned with the code posted in the beginning of this thread would be kindly appreciated.
You do not have the required permissions to view the files attached to this post.
Best Regards,
Adam

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

Re: Minimize & maximize userform with toggle button

Post by HansV »

If you reduce the height of the form, the Edit button will be hidden, so the user won't be able to click it.
You should move the Edit button to the top of the form (first make room for it by making the combo box and text box less wide and moving the text box a bit to the left).

Change the code for the Edit button to

Code: Select all

Private Sub ToggleButton1_Click()
  If ToggleButton1.Value = True Then
    Me.Height = Me.ListBox1.Top + Me.ListBox1.Height + 30
  Else
    Me.Height = dHeight
  End If
End Sub
You may have to experiment a bit with the value 30 to get it right.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimize & maximize userform with toggle button

Post by adam »

Thanks for the help Hans. Your code works just as I was assuming.

By the way I wonder if the form code could be adjusted so that when the form is opened, as mentioned before the combobox, list box and text box will be visible with the toggle button just below the listbox.

And when the toggle button is clicked it disappears and the remaining text boxes together with the command buttons are made visible in the form.

Any help of how to do this would be kindly appreciated.

The attached workbook does provide such an example.
You do not have the required permissions to view the files attached to this post.
Best Regards,
Adam

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

Re: Minimize & maximize userform with toggle button

Post by HansV »

You can study the code behind that userform to see how it is done.

Hint: the Visible property of controls is turned to True to make the controls visible, or to False to hide them.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimize & maximize userform with toggle button

Post by adam »

I've Changed the code provided by you as

Code: Select all

Private Sub TogEdit_Click()
  If TogEdit.Value = True Then
    Me.Height = Me.ListBox1.Top + Me.ListBox1.Height + 30
    TogEdit.Visible = False
  Else
    Me.Height = dHeight
  End If
End Sub
hoping that when the toggle button is clicked it gets hidden and the rest of the form gets visible. But, The code is not effective as the toggle button is already clicked when the userform is loaded.

How could I overcome this? Any suggestion would be kindly appreciated.
Best Regards,
Adam

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

Re: Minimize & maximize userform with toggle button

Post by HansV »

You could remove the line that clicks the toggle button from Userform_Initialize.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Minimize & maximize userform with toggle button

Post by adam »

When the line

Code: Select all

Me.TogEdit = True
is removed the form displays in maximized view. meaning with the text boxes.

is this the line you have referred?
Best Regards,
Adam

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

Re: Minimize & maximize userform with toggle button

Post by HansV »

Yes.
Best wishes,
Hans