473,698 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing arrays of controls to functions

Hi i'm having a problem passing an arrays of controls on a form to a
function or sub. I can use this code for any datatype but when i change
'long' to 'label' in the public sub and i call the sub from a mousedown or
keydown event belonging to an array of labels i get an error saying: "tpye
mismatch, array or user defined type expected".

Public Sub ProcTest(By Ref lngNum() as Long)
lngNum(1) = 2
End Sub

Public Sub Something()
Dim lngArray(1 To 5) as Long
Call ProcTest(lngArr ay)
MsgBox("Index 1 = " & lngArray(1))
End Sub

Anybody know if it's even possible to send an array of controls, or at least
pointers to a sub ?

Thanks in advance,

Kevin
Jul 17 '05 #1
3 6568
> Hi i'm having a problem passing an arrays of controls on a form to a
function or sub. I can use this code for any datatype but when i change 'long' to 'label' in the public sub and i call the sub from a mousedown or keydown event belonging to an array of labels i get an error saying: "tpye mismatch, array or user defined type expected".

Public Sub ProcTest(By Ref lngNum() as Long)
lngNum(1) = 2
End Sub

Public Sub Something()
Dim lngArray(1 To 5) as Long
Call ProcTest(lngArr ay)
MsgBox("Index 1 = " & lngArray(1))
End Sub

Anybody know if it's even possible to send an array of controls, or at least pointers to a sub ?


You would pass a control array as an Object. I would think it best to
address the members of the control array using For Each loop rather than
a normal For-Next (the Indexes of a control array do not have to be in
consecutive order). Here is a simple example that takes a control array
of TextBoxes that is named Text1 and passes it to a Sub that changes all
of their BackColors to red.

Private Sub Command1_Click( )
MakeRed Text1
End Sub

Sub MakeRed(TextBox Array As Object)
Dim tbox As TextBox
For Each tbox In TextBoxArray
tbox.BackColor = vbRed
Next
End Sub

Rick - MVP

Jul 17 '05 #2
Kevin wrote:
Hi i'm having a problem passing an arrays of controls on a form to a
function or sub. I can use this code for any datatype but when i change
'long' to 'label' in the public sub and i call the sub from a mousedown or
keydown event belonging to an array of labels i get an error saying: "tpye
mismatch, array or user defined type expected".

Public Sub ProcTest(By Ref lngNum() as Long)
lngNum(1) = 2
End Sub

Public Sub Something()
Dim lngArray(1 To 5) as Long
Call ProcTest(lngArr ay)
MsgBox("Index 1 = " & lngArray(1))
End Sub

Anybody know if it's even possible to send an array of controls, or at least
pointers to a sub ?


I don't know if there are any better ways, but this works (example of an
array of CommandButtons named Command1):

Private Sub Command1_Click( Index As Integer)
TestSub Command1
End Sub

Private Sub TestSub(buttons As Object)
MsgBox buttons(1).Capt ion
End Sub

Object or Variant seem to work as parameter type. You don't need to
specify ByRef since all parameters are passed as references in VB unless
ByVal is specified.

If you want to have a control array as parameter it is possible to use a
temporary array (same example as above):

Private Sub Command1_Click( Index As Integer)
With Command1
ReDim ButtonList(.LBo und To .UBound) As CommandButton
Dim c
For c = .LBound To .UBound
Set ButtonList(c) = .Item(c)
Next
End With

TestSub ButtonList
End Sub

Private Sub TestSub(Buttons () As CommandButton)
MsgBox Buttons(1).Capt ion
End Sub

--
Olof Lagerkvist
ICQ: 724451
Web page: http://here.is/olof

Jul 17 '05 #3
I got it working now, thanks both of you guys!

"Kevin" <ms*******@mdof .com> wrote in message
news:40******** *************** @news.wanadoo.n l...
Hi i'm having a problem passing an arrays of controls on a form to a
function or sub. I can use this code for any datatype but when i change
'long' to 'label' in the public sub and i call the sub from a mousedown or
keydown event belonging to an array of labels i get an error saying: "tpye mismatch, array or user defined type expected".

Public Sub ProcTest(By Ref lngNum() as Long)
lngNum(1) = 2
End Sub

Public Sub Something()
Dim lngArray(1 To 5) as Long
Call ProcTest(lngArr ay)
MsgBox("Index 1 = " & lngArray(1))
End Sub

Anybody know if it's even possible to send an array of controls, or at least pointers to a sub ?

Thanks in advance,

Kevin

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5366
by: Andy Fish | last post by:
Hi, I have a HTML page with javascript in it which pops up another HTML page. I can pass simple variables fairly freely between the two pages. I can pass objects between them two, and I have noticed that when a method is invoked on an object, it invokes in the context of the window that created that object. Now, when I pass an array from one window to another, the expression:
12
6548
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the courses to pass the correct option value and then be displayed at the following URL: http://www.dslextreme.com/users/kevinlyons/selectResults.html I am passing countries, products, and courses. The first two display
58
10140
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
2
1964
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems to get bogged down. Do the arrays previously passed to the function stay memory resident? If not, what's causing it and what can I do to correct it? Thanks, Dave
10
3164
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
1
3438
by: Kurt Richardson | last post by:
Hi all Sorry to bother you with what is probably a really trivial question for you C++ experts. My programming skill are pretty amateur, but I'm pretty good at VB.NET. However, I'm wanting to realise some of the speed benefits of writing some of my routines in C++ and accessing them from my VB software. I have managed to do this with a few simple routines in
4
1244
by: news | last post by:
Can .NET handle the old system of arrays for controls used in Visual Basic? eg TextBox(1), (2) ...
11
8125
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
1
4565
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to do, but because I can't seem to locate something that is even close... I am posting... I would like to thank this community, I have received a great deal of help already. Background: I have a pre-existing C code, that I have written, which works....
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.