Re: can't load a textbox array at runtime

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 30 jul, 02:39, "Schmidt" <s...@xxxxxxxxx> wrote:
"catharinus" <csvanderw...@xxxxxxxxx> schrieb im Newsbeitragnews:a5d852ce-bd10-4e6a-ad8d-399f788f0273@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On 29 jul, 14:27, "Jim Mack" <jm...@xxxxxxxxxxxxxxx> wrote:

no, I want to use this method witout the textbox on the form, ...

Then you will need a bit of "class-vodoo" -
take a look at the following...:

'***first you will need a small class-wrapper for each of the TextBoxes:
'***(name of the class: cTBWrap) ... to receive the TB-Events
Option Explicit

Private mParentArr As cTBArray 'store a ref to the parent-array
Private WithEvents mTB As TextBox
Private mIndex As Long

Friend Sub Init(ParentArr As cTBArray, _
                TB As TextBox, ByVal Index As Long)
  Set mParentArr = ParentArr
  Set mTB = TB
  mIndex = Index
End Sub

Public Property Get TB() As TextBox
  Set TB = mTB
End Property

Private Sub mTB_Change()
  mParentArr.RaiseChange mIndex
End Sub
Private Sub mTB_Validate(Cancel As Boolean)
  mParentArr.RaiseValidate mIndex, Cancel
End Sub

Private Sub Class_Terminate()
  If mTB Is Nothing Then Exit Sub
  mTB.Parent.Controls.Remove "TBArr_" & mIndex
End Sub

'***then you will need an "Array-Class" for the above instances
'***of cTBWrap - name this Array-Class: cTBArray
Option Explicit

'redefine Array-Events as needed here (in our case for the TextBox)
'always with a leading Index-Param (the rest of params as in orig.)
Event Change(ByVal Index As Long)
Event Validate(ByVal Index As Long, Cancel As Boolean)

Private mCol As Collection

Private Sub Class_Initialize()
  Set mCol = New Collection
End Sub

Public Sub Add(F As Form, ByVal Index As Long, Left, _
               Optional Top, Optional Width, Optional Height)
Dim TB As TextBox, NewTBWrap As cTBWrap

  Set TB = F.Controls.Add("VB.TextBox", "TBArr_" & Index)
  TB.Visible = True
  TB.Move Left, Top, Width, Height

  Set NewTBWrap = New cTBWrap
  NewTBWrap.Init Me, TB, Index
  mCol.Add NewTBWrap, CStr(Index)
End Sub

Public Property Get Item(ByVal Index As Long) As TextBox
  Set Item = mCol(CStr(Index)).TB
End Property

Public Sub Remove(ByVal Index As Long)
  mCol.Remove CStr(Index)
End Sub

Public Sub RemoveAll()
  Set mCol = New Collection
End Sub

'and here the centralized Event-Delegation in appropriate Subs
Friend Sub RaiseChange(ByVal Index As Long)
  RaiseEvent Change(Index)
End Sub
Friend Sub RaiseValidate(ByVal Index As Long, Cancel As Boolean)
  RaiseEvent Validate(Index, Cancel)
End Sub

'***and finally some Test-Code into a Form:
Option Explicit

Private WithEvents TBArray As cTBArray

Private Sub Form_Load()
Dim i&
  Me.ScaleMode = vbTwips
  Set TBArray = New cTBArray
  For i = 0 To 9
    TBArray.Add Me, i, i * 900, 0, 900
  Next i
End Sub

Private Sub Form_Click()
  TBArray.Remove 1 'just a small remove-test...
  MsgBox "TB(1) was removed..."

  TBArray.Add Me, 1, 900, 0, 900  '..followed by a re-add
  MsgBox "...and now readded."
End Sub

'here we handle the Array-Events
Private Sub TBArray_Change(ByVal Index As Long)
  Caption = "TB(" & Index & "): " & TBArray.Item(Index).Text
End Sub

Private Sub TBArray_Validate(ByVal Index As Long, Cancel As Boolean)
  Caption = "TB(" & Index & "): Validate-Event"
End Sub

Private Sub Form_Unload(Cancel As Integer)
  TBArray.RemoveAll 'make sure, to call that on Form-Unload
End Sub

Olaf

OKE Olof, very, very nice. That's it. I needed other events as well,
they can easily be inserted.
Thanks
Catharinus van der Werf
csvanderwerf@xxxxxxxxx
.



Relevant Pages

  • Re: Ersatz für Textbox-Steuerelement
    ... Textbox.Text liefert den Inhalt der Textbox, ... Private mDT As DataTable ... Private Sub Form1_Click _ ... ByVal e As System.EventArgs _ ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Auf ein {Dynam.Control.Value} zugreifen...
    ... Private WithEvents TB2 As TextBox ... ByVal e As System.EventArgs _ ... Private Sub CreateControls() ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: KeyValuePair and index
    ... Dim myLink As New Dictionary(Of String, ... Private count As Integer = 0 ... Private Sub Back_Click(ByVal sender As System.Object, ...     Private myLink As New List ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Konvertierung in Textboxen
    ... Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ... Textbox und das sind eben keine Zahlen, ... Werte (z.B. Double) erstellen und kannst diese ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Referencing A Subprocedures Name (Re-post)
    ... Private WithEvents m_oTextBox As MSForms.TextBox ... Private Sub m_oTextBox_Change ... Type in the textbox. ... cellValue As String ...
    (microsoft.public.excel.programming)