Cannot Align Text Box When Added Dynamically

angsuman

New Member
Joined
Aug 19, 2015
Messages
30
Hello All,

I have a blank user form. I have added two labels (Holiday & Holiday Description) which are heading of the columns and then two rows of text box underneath. These text boxes will accept input from user. Please refer the picture User form A below. The multi page, 4 text boxes are assigned to a collection and then a class is created to handle the event when they are clicked.



Code in initialization user form:

Code:
   For pageIndex = 0 To HolCalMulPg.Count - 1


      LblLeftMrgn = LeftMrgn
      For i = 1 To LblCount
         Set LblCtrl = HolCalMulPg.Pages(pageIndex).Controls.Add("Forms.Label.1", "Page" & pageIndex & "Label" & i)
         
         If ClmnNum = 0 Then
            LblText = "Holiday"
            Lblwdth = 60
            TxbWdth = 90
         Else
            LblText = "Holiday Description"
            Lblwdth = 80
            TxbWdth = 120
         End If
         
         With LblCtrl
            .Caption = LblText
            .Font = "Arial"
            .Font.Bold = True
            .TextAlign = fmTextAlignCenter
            .Top = LblTpMrgn
            .Left = LblLeftMrgn + 5
            .Width = Lblwdth
            .Height = LblHght
            .WordWrap = False
            .BackStyle = fmBackStyleTransparent
         End With
         
         RowNum = RowNum + 1
         TxbTpMrgn = LblTpMrgn + LblHght + GapBtRows
         
         For k = 1 To TxbCount
            
            If ClmnNum = 0 Then
               TxbText = "Enter/Select A Date"
               If TbxIdx = 0 Then
                  TbxIdx = 1
               End If
            Else
               TxbText = "Enter Holiday Description"
               If TbxIdx = 0 Then
                  TbxIdx = 2
               End If
            End If
         
            Set TxbCtrl = HolCalMulPg.Pages(pageIndex).Controls.Add("Forms.TextBox.1", "Page" & pageIndex & "TextBox" & TbxIdx)
            
            With TxbCtrl
               .Text = TxbText
               .Font = "Arial"
               .TextAlign = fmTextAlignLeft
               .Top = TxbTpMrgn
               .Left = LblLeftMrgn
               .Height = TxbHght
               .Width = TxbWdth
               .TabIndex = TbxIdx
               .Enabled = True
            End With
            
            RowNum = RowNum + 1
            TbxIdx = TbxIdx + 2
            TxbTpMrgn = TxbTpMrgn + GapBtRows + TxbHght
            
            Set clsHolidayObject = New clsHolidayCalendar
            Set clsHolidayObject.tbxCal = TxbCtrl
            colHoliday.Add clsHolidayObject
         Next
         
         TxbBtmMrgn = TxbTpMrgn + TxbHght
         
         LblLeftMrgn = LeftMrgn + TxbWdth + GapBtColms
         RowNum = 0
         TbxIdx = 0
         ClmnNum = ClmnNum + 1
      Next
      RowNum = 0
      ClmnNum = 0

' This variable is used to hold the position of the top margin of next tab when added by pressing tab key.
      
      NextTbxTopMrgn = TxbBtmMrgn + GapBtRows



While entering data, user may need to add more rows than available. This is handled through tab key. As user press tab from last text box, two more rows will be added. During user form initialize, the bottom margin of the text box is calculated and stored in a public variable. While adding new text boxes, top margin of the third row is calculated by adding gap to the bottom margin of last text box. However the new text box is not set at the position expected. Please refer the picture User form B below.

Code:
'Following is the code to add new text box 

Private Sub AddNewTextBox(ctrl As MSForms.MultiPage)


   Dim TbxIdx           As Long
   Dim TxbCtrl          As MSForms.TextBox
   Dim MulPgCtrl        As MSForms.MultiPage
   Dim k                As Long
   Dim TxbText          As String
   


   reset_values
   k = 1
   
   For k = 1 To TxbCount
      If ColumnNum = 0 Then
         TxbText = "Enter/Select A Date"
         TbxIdx = NextTbIndex
         TxbWdth = 90
      Else
         TxbText = "Enter Holiday Description"
         TbxIdx = NextTbIndex
         TxbWdth = 120
      End If
   
      Set TxbCtrl = ctrl.Pages(PageNum).Controls.Add("Forms.TextBox.1", "Page" & PageNum & "TextBox" & TbxIdx)
      
      With TxbCtrl
         .Text = TxbText
         .Font = "Arial"
         .TextAlign = fmTextAlignLeft
         '.Top = ctrl.Top + TxbTpMrgn
         .Top = TxbTpMrgn
         .Left = LeftMrgn
         .Height = TxbHght
         .Width = TxbWdth
         .TabIndex = TbxIdx
         .Enabled = True
      End With
            
      
      'RowNum = RowNum + 1
      TbxIdx = TbxIdx + 2
      TxbTpMrgn = TxbTpMrgn + GapBtRows + TxbHght
            
      'Set clsHolidayObject = New clsHolidayCalendar
      'Set clsHolidayObject.tbxCal = TxbCtrl
      'colHoliday.Add clsHolidayObject
   Next
   
End Sub


Public Sub reset_values()
   
   LeftMrgn = 20
   RghtMrgn = 20
   BtmMrgn = 10


   LblHght = 10
   
   TxbCount = 2
   TxbHght = 20
   TxbTpMrgn = NextTbxTopMrgn  <----- Assigning the text box position here
   CmdBtnHght = 18
   CmdBtnWdth = 54
   CmdBtnCount = 3


   GapBtColms = 25
   GapBtRows = 10
   GapBtBtmRows = 8
End Sub


Your advice on this will be really helpful.


Thanks
Angsuman
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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