Passing form and object array 
Author Message
 Passing form and object array

Alright, i have a question i know you guys have an answer
for, that i just don't understand.  Why, in the following
code:

Private Sub Form_Load()
    Call CreateControlArray(Form1, cmdButton)
End Sub

Sub CreateControlArray(ByRef FormName As Form, ByRef
Control As Object)
'...
End Sub

doesn't the statement:

Form1.cmdButton
equal
FormName.Control
?

It would be very convenient for me to be able to pass my
parent form as a reference to a module and access the
controls or the control i passed along with it to that
very module.  Any input would be greatly appriciated.  
Thnx.

Andrew



Sun, 30 Oct 2005 09:25:52 GMT  
 Passing form and object array
Is this question being ignored, or you guys still working
on it?

Andrew

Quote:
>-----Original Message-----
>Alright, i have a question i know you guys have an answer
>for, that i just don't understand.  Why, in the following
>code:

>Private Sub Form_Load()
>    Call CreateControlArray(Form1, cmdButton)
>End Sub

>Sub CreateControlArray(ByRef FormName As Form, ByRef
>Control As Object)
>'...
>End Sub

>doesn't the statement:

>Form1.cmdButton
>equal
>FormName.Control
>?

>It would be very convenient for me to be able to pass my
>parent form as a reference to a module and access the
>controls or the control i passed along with it to that
>very module.  Any input would be greatly appriciated.  
>Thnx.

>Andrew
>.



Fri, 04 Nov 2005 22:57:29 GMT  
 Passing form and object array

Quote:
> >Sub CreateControlArray(ByRef FormName As Form, ByRef
> >Control As Object)

ByRef FormName As Form.... you're passing a generic Form class. VB has no way of knowing
what controls are available on that form. That means... no early binding allowed. I'm not
sure what you're doing with the form in that sub... but, you can pass the control(s) by
themselves. The only reason to pass the form is if you're actually doing something to the
form (like setting backcolor, adding to the controls collection.. or whatever)

--
Ken Halter - MS-MVP-VB - http://www.mvps.org/vbsight - Please keep it in the groups..


Quote:
> Is this question being ignored, or you guys still working
> on it?

> Andrew
> >-----Original Message-----
> >Alright, i have a question i know you guys have an answer
> >for, that i just don't understand.  Why, in the following
> >code:

> >Private Sub Form_Load()
> >    Call CreateControlArray(Form1, cmdButton)
> >End Sub

> >Sub CreateControlArray(ByRef FormName As Form, ByRef
> >Control As Object)
> >'...
> >End Sub

> >doesn't the statement:

> >Form1.cmdButton
> >equal
> >FormName.Control
> >?

> >It would be very convenient for me to be able to pass my
> >parent form as a reference to a module and access the
> >controls or the control i passed along with it to that
> >very module.  Any input would be greatly appriciated.
> >Thnx.

> >Andrew
> >.



Fri, 04 Nov 2005 23:05:29 GMT  
 Passing form and object array

Quote:
> Is this question being ignored, or you guys still working
> on it?

> Andrew
>> -----Original Message-----
>> Alright, i have a question i know you guys have an answer
>> for, that i just don't understand.  Why, in the following
>> code:

>> Private Sub Form_Load()
>>    Call CreateControlArray(Form1, cmdButton)
>> End Sub

>> Sub CreateControlArray(ByRef FormName As Form, ByRef
>> Control As Object)
>> '...
>> End Sub

>> doesn't the statement:

>> Form1.cmdButton
>> equal
>> FormName.Control
>> ?

Because there is no 'Control' property on a form.  BTW, 'Control' is a poor
choice for a variable name because it conflicts with the type name for VB
controls.

What are you trying to do inside the 'CreateControlArray' routine?  You can
reference the control object directly via the parameter (e.g.
Control.Caption="xyz") but you aren't going to be able to load a control
array element that way so your procedure name sounds to me like you are
trying to do something you can't.



Fri, 04 Nov 2005 23:14:46 GMT  
 Passing form and object array

<cut>

Quote:
> What are you trying to do inside the 'CreateControlArray' routine?
> You can reference the control object directly via the parameter (e.g.
> Control.Caption="xyz") but you aren't going to be able to load a
> control array element that way so your procedure name sounds to me
> like you are trying to do something you can't.

After sending that I realized that I didn't know for sure so I tried it:

Private Sub Form_Click()
CreateControlArray Command1
End Sub

Private Sub CreateControlArray(ByVal CArray As Object)
Dim n As Long
For n = 1 To 5
  Load CArray(n)
  CArray(n).Move CArray(n - 1).Left, CArray(n - 1).Top + CArray(n).Height
  CArray(n).Visible = True
Next
End Sub

Believe it or not, it worked!



Fri, 04 Nov 2005 23:17:43 GMT  
 Passing form and object array
Yes, i know it works:).  The reason i'm trying to pass the
form is so that i can create a module that has code used
to create a two-dimensional command button control array.  
Should i just have it pass the control and work with that?

Andrew

Quote:
>-----Original Message-----


><cut>
>> What are you trying to do inside

the 'CreateControlArray' routine?
Quote:
>> You can reference the control object directly via the
parameter (e.g.
>> Control.Caption="xyz") but you aren't going to be able
to load a
>> control array element that way so your procedure name
sounds to me
>> like you are trying to do something you can't.

>After sending that I realized that I didn't know for sure
so I tried it:

>Private Sub Form_Click()
>CreateControlArray Command1
>End Sub

>Private Sub CreateControlArray(ByVal CArray As Object)
>Dim n As Long
>For n = 1 To 5
>  Load CArray(n)
>  CArray(n).Move CArray(n - 1).Left, CArray(n - 1).Top +
CArray(n).Height
>  CArray(n).Visible = True
>Next
>End Sub

>Believe it or not, it worked!

>.



Sat, 05 Nov 2005 00:02:10 GMT  
 Passing form and object array

Quote:
> Yes, i know it works:).  The reason i'm trying to pass the
> form is so that i can create a module that has code used
> to create a two-dimensional command button control array.
> Should i just have it pass the control and work with that?

Yes, the 'control' includes the form it belongs on, but...

You are not actually passing a control.  You are passing
(or must pass) the control array.  That array is called by
the name you gave it, and it shows up as the Name property
for all the controls in the array, but that 'object' is a different
beast altogether, which is why you must pass it As Object, and
not As Control.

Just for more evidense, type that array name in the IDE followed
by a period instead of paretheses:

Instead of:  Buttons(0).
Try:   Buttons.

You'll see you get a drop down list of the properties of that array 'object'.

FWIW:
If my memory serves, I recall Rick Rothstein answered this
question already.

LFS



Sat, 05 Nov 2005 00:30:48 GMT  
 Passing form and object array

[...]

Quote:
> After sending that I realized that I didn't know for sure so I tried it:

> Private Sub Form_Click()
> CreateControlArray Command1
> End Sub

> Private Sub CreateControlArray(ByVal CArray As Object)
> Dim n As Long
> For n = 1 To 5
>   Load CArray(n)
>   CArray(n).Move CArray(n - 1).Left, CArray(n - 1).Top + CArray(n).Height
>   CArray(n).Visible = True
> Next
> End Sub

> Believe it or not, it worked!

Yep it's pretty good (I use it on Winsock based servers to load copies of
the winsock control for each client, and leaving my intial "listener" socket
to remain unblocked/unused for additional concurrent clients trying to
connect).

Although limited, here's a quick "calendar month" type demo of a dynamically
created button array.. obviously if you wanted to use it for real you'd want
to specify different total of days per month and to offset the "grid"
display so that it conformed to a more intuitive "Mo Tu We ... Su" output
(instead of placing button [1] in the first available cell as I do).

But it still demonstrates that the capability is clearly available and
easily at hand.  If perhaps instead of actual calendar days (since there are
plenty 3rd party controls for this already) you wanted to be able to display
perhaps working shift patterns or other non-calendar timetables flexibly.

As with your code, just a form with a Command1 button with its Index
property set to 0 before running is all you need:

'---VB6 CODE BEGINS---
Option Explicit

Private Sub Form_Load()
    Dim iLoop As Integer
    Dim iRow As Integer, iColumn As Integer

    Const BOX_SIZE As Integer = 350
    Const BOX_COUNT As Integer = 31
    Const BOX_BORDER As Integer = 120
    Const ROW_LENGTH As Integer = 7

    With Command1(0)
        .Left = BOX_BORDER
        .Top = BOX_BORDER
        .Height = BOX_SIZE
        .Width = BOX_SIZE
        .Caption = "1"
    End With

    iColumn = 1

    For iLoop = 1 To BOX_COUNT - 1
        If iColumn = ROW_LENGTH Then
            iColumn = 0
            iRow = iRow + 1
        End If

        Load Command1(iLoop)

        With Command1(iLoop)
            .Top = (iRow * BOX_SIZE) + BOX_BORDER
            .Left = (iColumn * BOX_SIZE) + BOX_BORDER
            .Caption = CStr(iLoop + 1)
            .Visible = True
        End With

        iColumn = iColumn + 1
    Next iLoop
End Sub

Private Sub Command1_Click(Index As Integer)
    MsgBox Format(DateSerial(2003, 5, Index + 1), "Long Date")
End Sub
'---VB6 CODE ENDS

I've never really used the Move method before, no reason I set things like
.Top and .Left individually other than due to habit (Move I guess is better
at least for visible controls as it means less potential repaints).

Anyway, this is also a useful technique if, for example, you'd like to
create a generic Wizard template within your applications.. the Wizard class
could display different control types and quantities programmatically at
runtime from page to page based on a simple (developer defined)
configuration format for setting it up, after each Next button was clicked
(or whatever) the Wizard could raise an event allowing you to decide based
on the chosen options what controls and such to present on the following
page.

Obviously you could re-code all your Wizards completely by hand from app to
app to app.. and maybe they will and maybe they won't be consistent.. so
there are reasons why you could capitalise on formalising such an approach,
and if you do it right, you could even upgrade your Wizard class and
everything that uses it could benefit.

Back when I first made a stab at such a beast (VB4 days) for a client I
didn't know about Load'ing a control array (or if it was even possible,
though I guess it probably was) and I was still able to use it this way
quite successfully in that and other projects.  Since playing with this
new-found capability I can see how it can be applied to improve upon it.

Regards,
Nick



Sat, 05 Nov 2005 00:50:20 GMT  
 Passing form and object array

Quote:


> [...]
>> After sending that I realized that I didn't know for sure so I tried
>> it:

>> Private Sub Form_Click()
>> CreateControlArray Command1
>> End Sub

>> Private Sub CreateControlArray(ByVal CArray As Object)
>> Dim n As Long
>> For n = 1 To 5
>>   Load CArray(n)
>>   CArray(n).Move CArray(n - 1).Left, CArray(n - 1).Top +
>> CArray(n).Height   CArray(n).Visible = True
>> Next
>> End Sub

>> Believe it or not, it worked!

> Yep it's pretty good (I use it on Winsock based servers to load
> copies of the winsock control for each client, and leaving my intial
> "listener" socket to remain unblocked/unused for additional
> concurrent clients trying to connect).

Loading additional elements into control arrays I've used often.  What
surprised me is that I could pass the control array object around and load
that.  notice that my code uses "Load CArray(n)" and not "Load Command1(n)"


Sat, 05 Nov 2005 00:58:40 GMT  
 Passing form and object array

[...]
Quote:
> Loading additional elements into control arrays I've used often.  What
> surprised me is that I could pass the control array object around and load
> that.  notice that my code uses "Load CArray(n)" and not "Load

Command1(n)"

Ah ok, I didn't realise that was what surprised you.  To me, Command1(n) is
no different (in this example) from CArray(n).. just as Command1 is no
different from CArray -- you assigned one to the other in effect didn't you?
So being able to subscript the object down into individual elements seems
natural too.  CArray is just a pointer/reference (depending on your point of
view) so it needs to support and expose whatever Command1 can.

Or am I still missing your point :)

Regards,
Nick



Sat, 05 Nov 2005 01:09:52 GMT  
 Passing form and object array

Quote:


> [...]
>> Loading additional elements into control arrays I've used often.
>> What surprised me is that I could pass the control array object
>> around and load that.  notice that my code uses "Load CArray(n)" and
>> not "Load Command1(n)"

> Ah ok, I didn't realise that was what surprised you.  To me,
> Command1(n) is no different (in this example) from CArray(n).. just
> as Command1 is no different from CArray -- you assigned one to the
> other in effect didn't you? So being able to subscript the object
> down into individual elements seems natural too.  CArray is just a
> pointer/reference (depending on your point of view) so it needs to
> support and expose whatever Command1 can.

> Or am I still missing your point :)

I understand it, I'm just surprised that the Load command works when the
argument is not the actual control name.  I guess I shouldn't be since the
TypeName of a control array is Object so Load must be doing all late bound
checks.  It's not a big deal, I just expected it to fail.


Sat, 05 Nov 2005 01:28:21 GMT  
 Passing form and object array
And how would a module know the form name the control
comes from?

Andrew

Quote:
>-----Original Message-----

>> Yes, i know it works:).  The reason i'm trying to pass
the
>> form is so that i can create a module that has code used
>> to create a two-dimensional command button control
array.
>> Should i just have it pass the control and work with
that?

>Yes, the 'control' includes the form it belongs on, but...

>You are not actually passing a control.  You are passing
>(or must pass) the control array.  That array is called by
>the name you gave it, and it shows up as the Name property
>for all the controls in the array, but that 'object' is a
different
>beast altogether, which is why you must pass it As
Object, and
>not As Control.

>Just for more evidense, type that array name in the IDE
followed
>by a period instead of paretheses:

>Instead of:  Buttons(0).
>Try:   Buttons.

>You'll see you get a drop down list of the properties of

that array 'object'.

- Show quoted text -

Quote:

>FWIW:
>If my memory serves, I recall Rick Rothstein answered this
>question already.

>LFS

>.



Sat, 05 Nov 2005 06:57:08 GMT  
 Passing form and object array

Quote:
> And how would a module know the form name the control
> comes from?

> Andrew

[...]

MsgBox myControl.Parent.Name

You may wish to test the control or error trap the Parent object if for any
reason it isn't set, since it won't work all the time as-is without
trapping.

Regards,
Nick



Sat, 05 Nov 2005 07:31:19 GMT  
 Passing form and object array
Alright, that was neat.  So then how do i call up object
methods from the parent form in the module, e.i.

myControl.visible = false

Andrew

Quote:
>-----Original Message-----


>> And how would a module know the form name the control
>> comes from?

>> Andrew
>[...]

>MsgBox myControl.Parent.Name

>You may wish to test the control or error trap the Parent
object if for any
>reason it isn't set, since it won't work all the time as-
is without
>trapping.

>Regards,
>Nick

>.



Sat, 05 Nov 2005 23:29:43 GMT  
 Passing form and object array

Quote:
> Alright, that was neat.  So then how do i call up object
> methods from the parent form in the module, e.i.

> myControl.visible = false

> Andrew

[...]

Well the Parent is a reference /to/ the object, so whilst I chose to display
the text name of the parent form with myControl.Parent.Name you could easily
have done myControl.Parent.Visible = False (or myControl.Parent.Hide) and so
on.  Everything exposed (i.e. Public, both properties and methods) in the
Parent (form) object reference can be manipulated this way.

As I said before though, your only caveat to watch out for is in case for
whatever reason the object you are using either:
- Doesn't have a Parent property.
- Has a Parent property, but the Parent isn't a form.
- Has a Parent property, but isn't set to anything.

I don't know all the conditions which may or may not make any of the above
an issue, maybe some of those conditions are impossible to create, but
regardless they are easy enough to check and/or error trap (the second one
you can test to see with TypeOf myControl.Parent Is Form).

Regards,
Nick



Sun, 06 Nov 2005 02:02:15 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Passing data arrays from object to object

2. Passing dynamic array of custom type to form method from another form

3. Passing Array to COM Object

4. Problem passing a variant array out of VBasic object to ASP

5. Need help passing an array to an Oracle procedure using Oracle Objects for OLE

6. Passing an Array of Objects to DLL

7. Passing Array from VB to COM objects

8. Passing arrays between objects...

9. Passing Arrays to COM Object Slows Performance

10. Passing array to COM object

11. How to pass vbscript array variable to shared object using ByRef

12. Passing Multi-Dimensional arrays to COM objects from VBScript/ASP

 

 
Powered by phpBB® Forum Software