can't load a textbox array at runtime 
Author Message
 can't load a textbox array at runtime

Hello you all

I want to create dynamically textboxes in an array
I use the following code, but when the program reaches to
the line "load tbbox(1) the program leaves the subroutine
and the textbox is not created. Any idea??
Thanks
Catharinus van der Werf

dim tbBox(5) as textbox

   Set tbBox(0) = meControls.Add("vb.textbox", "tbBox")
    tbBox(0).Width = 2000
    tbBox(0).Top = me.Height - 1600 ' / 2 - tbbox(0).Height / 2 - 100
    tbBox(0).Left = me.Width / 2 - tbBox(0).Width / 2
    tbBox(0).Text = "Dynamic Textbox"
    tbBox(0).Visible = True
    Load tbBox(1)



Sun, 15 Jan 2012 18:24:37 GMT  
 can't load a textbox array at runtime

Quote:

> Hello you all

> I want to create dynamically textboxes in an array
> I use the following code, but when the program reaches to
> the line "load tbbox(1) the program leaves the subroutine
> and the textbox is not created. Any idea??

> dim tbBox(5) as textbox

>    Set tbBox(0) = meControls.Add("vb.textbox", "tbBox")
>     tbBox(0).Width = 2000
>     tbBox(0).Top = me.Height - 1600 ' / 2 - tbbox(0).Height / 2 -
>     100 tbBox(0).Left = me.Width / 2 - tbBox(0).Width / 2
>     tbBox(0).Text = "Dynamic Textbox"
>     tbBox(0).Visible = True
>     Load tbBox(1)

It looks as if you're confusing an 'array of controls' with a 'control
array'. In order to do what it looks like you want, tbBox must already
exist at design time and must have an .Index of 0.

--
   Jim Mack
   Twisted tees at http://www.cafepress.com/2050inc
   "We sew confusion"



Sun, 15 Jan 2012 19:06:53 GMT  
 can't load a textbox array at runtime

Quote:

> > Hello you all

> > I want to create dynamically textboxes in an array
> > I use the following code, but when the program reaches to
> > the line "load tbbox(1) the program leaves the subroutine
> > and the textbox is not created. Any idea??

> > dim tbBox(5) as textbox

> > ? ?Set tbBox(0) = meControls.Add("vb.textbox", "tbBox")
> > ? ? tbBox(0).Width = 2000
> > ? ? tbBox(0).Top = me.Height - 1600 ' / 2 - tbbox(0).Height / 2 -
> > ? ? 100 tbBox(0).Left = me.Width / 2 - tbBox(0).Width / 2
> > ? ? tbBox(0).Text = "Dynamic Textbox"
> > ? ? tbBox(0).Visible = True
> > ? ? Load tbBox(1)

> It looks as if you're confusing an 'array of controls' with a 'control
> array'. In order to do what it looks like you want, tbBox must already
> exist at design time and must have an .Index of 0.

> --
> ? ?Jim Mack
> ? ?Twisted tees athttp://www.cafepress.com/2050inc
> ? ?"We sew confusion"- Tekst uit oorspronkelijk bericht niet weergeven -

> - Tekst uit oorspronkelijk bericht weergeven -

Oke Jim

I have already tried, but I keep having problems
Is it possible to add textboxes without that tbBox already on the
form?
Thanks
Catharinus van der Werf



Sun, 15 Jan 2012 19:51:07 GMT  
 can't load a textbox array at runtime

Quote:


>> It looks as if you're confusing an 'array of controls' with a
>> 'control array'. In order to do what it looks like you want, tbBox
>> must already exist at design time and must have an .Index of 0.

> Oke Jim

> I have already tried, but I keep having problems
> Is it possible to add textboxes without that tbBox already on the
> form?

Not as far as I know, and even if you could they wouldn't be wired to
anything, so there would be no event procedures written to handle
them.

The original tbBox must exist and have a non-null .Index, but it
doesn't have to be visible, if that's what's concerning you.

--
        Jim



Sun, 15 Jan 2012 20:27:50 GMT  
 can't load a textbox array at runtime

Quote:


> >> It looks as if you're confusing an 'array of controls' with a
> >> 'control array'. In order to do what it looks like you want, tbBox
> >> must already exist at design time and must have an .Index of 0.

> > Oke Jim

> > I have already tried, but I keep having problems
> > Is it possible to add textboxes without that tbBox already on the
> > form?

> Not as far as I know, and even if you could they wouldn't be wired to
> anything, so there would be no event procedures written to handle
> them.

> The original tbBox must exist and have a non-null .Index, but it
> doesn't have to be visible, if that's what's concerning you.

> --
> ? ? ? ? Jim

thanks Jim
no, I want to use this method witout the textbox on the form, because
this method is ment for 40 frorm and it is quite messy to put as
textbox on
every frorm first.


Sun, 15 Jan 2012 21:05:14 GMT  
 can't load a textbox array at runtime



Quote:

>thanks Jim
> no, I want to use this method witout the textbox on the form,
> because this method is ment for 40 frorm and it is quite
> messy to put as textbox on every frorm first.

Re-read Jim's suggestion. Adding a single hidden textbox is no messier than
creating a textbox out of thin air and then trying to 'attach' it to a
specific form. (Then all that testing to make sure it worked as desired.)

If all that 'clicking' concerns you, take a look at Project/Form templates
for any new Forms, or simply open the Form file and insert a textbox using a
multiple buffer code editor for any existing Forms. Adding a Textbox to 40
existing forms should take you less than 5 minutes. Fif{*filter*} if you like to
dawdle.

-ralph



Sun, 15 Jan 2012 22:18:15 GMT  
 can't load a textbox array at runtime


thanks Jim
no, I want to use this method witout the textbox on the form, because
this method is ment for 40 frorm and it is quite messy to put as
textbox on every frorm first.
------------

What does 'messy' mean to you?

For example, how are you going to respond to the events of several
'created' textboxes, without 'messing' up your form's code module???

LFS



Sun, 15 Jan 2012 23:24:08 GMT  
 can't load a textbox array at runtime

Quote:


>> The original tbBox must exist and have a non-null .Index, but it
>> doesn't have to be visible, if that's what's concerning you.

> no, I want to use this method witout the textbox on the form,
> because this method is ment for 40 frorm and it is quite messy to
> put as textbox on
> every frorm first.

I'm not sure what part you consider messy. Another alternative is to
create the textbox on one form, then text-edit all the FRM files,
cutting from the sample and pasting into the others. That should take
all of a minute for 40 forms.

--
   Jim Mack
   Twisted tees at http://www.cafepress.com/2050inc
   "We sew confusion"



Mon, 16 Jan 2012 01:25:51 GMT  
 can't load a textbox array at runtime




Quote:
> 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



Mon, 16 Jan 2012 08:39:10 GMT  
 can't load a textbox array at runtime

Quote:


> > 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



Mon, 16 Jan 2012 16:07:58 GMT  
 can't load a textbox array at runtime
Sounds like Olaf's class voodoo is doing the trick. Just for
completeness here's a link to Karl Peterson's sample on this, which
demonstrates two alternative designs for the class voodoo.

http://vb.mvps.org/samples/ObjArrays/



Mon, 16 Jan 2012 19:19:12 GMT  
 can't load a textbox array at runtime




s.com...
<snipped>

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

Indeed. That is far less "messy" than a ugly hidden textbox.

lol
-ralph



Mon, 16 Jan 2012 22:33:01 GMT  
 can't load a textbox array at runtime


Quote:






> s.com...
> <snipped>

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

> Indeed. That is far less "messy" than a ugly hidden textbox.

> lol
> -ralph

And SO much easier to implement!
To think of all the time I have wasted placing hidden Array(0) objects on
forms, what a fool I feel.

Dave O.



Mon, 16 Jan 2012 23:10:51 GMT  
 can't load a textbox array at runtime

Quote:
>> Indeed. That is far less "messy" than a ugly hidden textbox.

>> lol
>> -ralph

> And SO much easier to implement!
> To think of all the time I have wasted placing hidden Array(0) objects on
> forms, what a fool I feel.

I think we've all finally been shown up for the amateurs we are.


Tue, 17 Jan 2012 01:49:35 GMT  
 can't load a textbox array at runtime



Quote:
> > Indeed. That is far less "messy" than a ugly hidden textbox.

> > lol
> > -ralph

> And SO much easier to implement!
> To think of all the time I have wasted placing hidden Array(0)
> objects on forms, what a fool I feel.

:-)

Heh - that was just posted, to "complete" the thread somewhat ...
The Array(0)-variant is of course the easier one, requiring
not that much efforts (also much more "intuitive" usable).
Especially with regards to VBs intrinsic controls - since these
do not support the implementation of that wrapping-approach
in a generic way (over the VB6- vbControlExtender-Type,
declared WithEvents).

If the intrinsics would allow a Controls.Add against a
vbControlExtender-Variable (as e.g. all the "external"
Controls, hosted in OCXes do), then such a wrapping would
only have to be written once, for all kind of Controls, and
all the Events these Controls may fire.
But since that is not the case with the intrinsics, one will have
to "repeat" the same code (in separate classes) for each
different intrinsic-type (VB.ListBox, VB.CommandButton, etc.)
and also the implementation of the appropriate (needed)
Events for the given Intrinsic-Type in question.

Sadly the Controls.Add-functionality (and its counterpart, the
receiving vbControlExtender-Type) was not consequently
implemented in that regard by MS - that's probably the main-
reason, why Controls.Add was always sitting in its niche -
compared with the alternative method; the far more popular
Control-Array usage.

Olaf



Tue, 17 Jan 2012 02:51:57 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Create array of TextBox's at runtime?

2. 'Canned' data in VB program

3. Create textbox array at runtime

4. Help for grid control array load at runtime

5. Load Controls at Runtime - Visible / Array

6. Loading and calling DLL's during runtime

7. Runtime error '429' at runtime

8. Runtime error '429' at runtime

9. Runtime error '429' at runtime

10. VB6: application in runtime environment with error on loading form: ado cannot load object

11. loading several Textboxes with Load Text1(x)

12. Runtime error ''800a01b6''

 

 
Powered by phpBB® Forum Software