Avatar billede kgndksv Juniormester
22. maj 2015 - 21:49 Der er 8 kommentarer og
1 løsning

Auto tilpasse størrelsen på userform i forhold til checkboxes i userformen

Jeg har følgende kode, hvor jeg tilføjer en dynamisk antal checkboxes. Jeg er nu kommet i det problem at antallet af checkboxes ikke kan rummes i den foruddefinerede størrelse på userformen og derfor vil jeg gerne høre om der er nogle der kan fortælle hvordan man kan auto resize en userform evt. på baggrund af placeringen af den nederste checkbox i userformen.

min kode til at tilføje checkboxes er denne, som henter antallet af checkboxes fra antallet af celler i et en-dimensionel array:


Private Sub UserForm_Initialize()

Dim LastColumn  As Long
Dim i          As Long
Dim chkBox      As MSForms.CheckBox

Call ProList ' Fylder arrayet

LastColumn = UBound(UniqueProvisionArray)

For i = 0 To LastColumn
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = UniqueProvisionArray(i)
    chkBox.Left = 5
    chkBox.Top = 5 + (i * 20)
   
Next i

End Sub


Kan man samtidig resize userformens størrelse og hvis, hvordan?
Avatar billede supertekst Ekspert
22. maj 2015 - 23:52 #1
Private Sub UserForm_Initialize()

Dim LastColumn  As Long
Dim i          As Long
Dim chkBox      As MSForms.CheckBox

Call ProList ' Fylder arrayet

LastColumn = UBound(UniqueProvisionArray)

For i = 0 To LastColumn
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = UniqueProvisionArray(i)
    chkBox.Left = 5
    chkBox.Top = 5 + (i * 20)
   
Next i

Me.Height = chkBox.Top + chkBox.Height + 20  '<++++++++++


End Sub
Avatar billede kgndksv Juniormester
24. maj 2015 - 22:36 #2
Du er genial som altid :-)

Kan du forklare mig, hvordan jeg identifcerer bredden af den tekst der står i checkbox.caption.

Hvis min teksten er for lang deler den teksten op på 2 linjer og så går den næste checkbox indover den dobbelt linjede CB...

Jeg ville gerne have at captionen ikke bryder teksten og lader caption teksten blive skrevet ud i en linje...
Avatar billede kgndksv Juniormester
24. maj 2015 - 22:38 #3
Jeg vil også gerne have bredden tilpasset udfra størrelsen af caption teksten.

Min kode ser således ud nu:

'Option Explicit

Private Sub CommandButton1_Click()
Call FilterProvisions
End Sub

Private Sub UserForm_Initialize()

Dim LastColumn  As Long
Dim i          As Long
Dim chkBox      As MSForms.CheckBox

Call ProList ' Fylder arrayet

LastColumn = UBound(UniqueProvisionArray)

chkBoxWidth = 0

For i = 0 To LastColumn
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = UniqueProvisionArray(i)
    chkBox.Left = 5
    chkBox.Top = 5 + (i * 20)
   
    If chkBoxWidth < chkBox.Width Then
        chkBoxWidth = chkBox.Width
    End If
 
 
 
Next i

CommandButton1.Left = 5
CommandButton1.Top = 5 + ((i) * 20)


Me.Height = Me.CommandButton1.Top + Me.CommandButton1.Height + 25

Me.CommandButton1.Width = Me.Width - 15
'Me.Width = Me.Left ' + chkBoxWidth


End Sub
Avatar billede supertekst Ekspert
24. maj 2015 - 22:58 #4
Tak for de pæne ord - vender tilbage senere..
Avatar billede supertekst Ekspert
26. maj 2015 - 10:36 #5
Svar på #2:

For i = 0 To LastColumn
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = UniqueProvisionArray(i)
    chkBox.AutoSize = True                  '<++++++++++
   
    chkBox.Left = 5
    chkBox.Top = 5 + (i * 20)
Next i
Avatar billede supertekst Ekspert
26. maj 2015 - 10:57 #6
Svar på #3
PS: UniqueProvisionArray er substitueret......

'Option Explicit

Private Sub CommandButton1_Click()
Call FilterProvisions
End Sub

Private Sub UserForm_Initialize()

Dim LastColumn  As Long
Dim i          As Long
Dim chkBox      As MSForms.CheckBox
Const tabel = "A;BBBBBBBBB BBBBBBB bbb;CCC;DDDD;EEEEEEEEEEEE EEEEEEEE"

UniqueProvisionArray = Split(tabel, ";")

'Call ProList ' Fylder arrayet

LastColumn = UBound(UniqueProvisionArray)

chkBoxWidth = 0

For i = 0 To LastColumn
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = UniqueProvisionArray(i)
    chkBox.AutoSize = True
   
    If chkBox.Width > chkBoxWidth Then      '<+++++
        chkwidth = chkBox.Width
    End If
   
    chkBox.Left = 5
    chkBox.Top = 5 + (i * 20)
   
    If chkBoxWidth < chkBox.Width Then
        chkBoxWidth = chkBox.Width
    End If
 
Next i

CommandButton1.Left = 5
CommandButton1.Top = 5 + ((i) * 20)


Me.Height = Me.CommandButton1.Top + Me.CommandButton1.Height + 25

Me.Width = Me.Left + chkBoxWidth + 15        '<++++++

Me.CommandButton1.Width = Me.Width - 15

End Sub
Avatar billede kgndksv Juniormester
01. juni 2015 - 09:02 #7
hmm... Jeg ved ikke helt hvad jeg gør forkert, men den bryder stadig teksten i checkbox caption og tilpasser ikke bredden:

Private Sub UserForm_Initialize()

Dim LastColumn  As Long
Dim i          As Long
Dim chkBox      As MSForms.CheckBox
Dim chkBoxWidth As Integer

Call ProList ' Fylder arrayet

LastColumn = UBound(UniqueProvisionArray)

chkBoxWidth = 0

For i = 0 To LastColumn
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = UniqueProvisionArray(i)
    chkBox.AutoSize = True

    If chkBox.Width > chkBoxWidth Then
        chkBoxWidth = chkBox.Width
    End If
 
    chkBox.Left = 5
    chkBox.Top = 5 + (i * 20)
 
 
 
Next i

CommandButton1.Left = 5
CommandButton1.Top = 5 + ((i) * 20)


Me.Height = Me.CommandButton1.Top + Me.CommandButton1.Height + 25

Me.Width = Me.Left + chkBoxWidth + 15


Me.CommandButton1.Width = Me.Width - 15


End Sub
Avatar billede supertekst Ekspert
01. juni 2015 - 09:35 #8
Skal se på det - vender tilbage senere
Avatar billede supertekst Ekspert
01. juni 2015 - 18:10 #9
Når jeg indsætter min særlige linjer i stedet for "Call ProList - som jeg jo ikke har" i din version (#7) så virker det ok.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester