Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Arrays for User Defined Controls

Status
Not open for further replies.

ianwin

Programmer
Jul 5, 2005
44
US
I have developed a custom control which is going to be used in a polling program to access different data streams. On my form I have 12 instances of this control each with a GetData method which connects to the data source. Also on the form I have a timer which triggers every second to loop through each control to poll the data.

In VB6 I would have done this as a control array so that when the timer triggers I would have an index which would be used to access each control.

<code>

Private Sub Timer1_Timer()
Index = Index + 1
MyControl(Index).GetData.
EndSub
</code>
Is there a way I can implement this functionallity in VB.Net?

Any help would be greatly appreciated.

-Ian
 
Yes.

You can create an array for your controls:

Dim MyControlArray(11) as MyCustomControl

MyControlArray(0) = MyCustomControl1
MyControlArray(1) = MyCustomControl2

Then reference the controls with MyControlArray(0).GetData(), MyControlArray(1).GetData(), etc.

Or, you can use a collection:

Dim MyControlCollection As Collection

MyControlCollection.Add(MyCustomControl1)
MyControlCollection.Add(MyCustomControl2)

To reference the objects in the collection, you will need to cast them back to your control's type:

CType(MyControlCollection(0), MyCustonControl).GetData()
CType(MyControlCollection(1), MyCustonControl).GetData()

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
I have looked at code similar to that the problem I am having is using my custom control as a type either for definition of the array or for casting from a collection.

I have developed the control as a Windows Control Library and have then compiled it to a dll. I have included this as a component on the toolbar so that I can put the control on forms and I have also added a reference to the dll in project references however I get an error Type expected when using the following code.

dim myControlArray(11) as myCustomControl

-Ian
 
How about something like this:

Code:
dim ctlMyCustomControl as myCustomControl
dim alControls as new arraylist

alControls.add(ctlMyCustomControl)

dim c as control
for each c in alControls
  cast(c, myCustomControl).getdata()
next

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I'm still having problems defining anything as myCustomControl. So far I have used drag and drop to put the controls on the form from the toolbox. When I dim as myCustomControl I still get the wavy line underneath saying type expected. Is there an include I'm missing somewhere?

thanks,

-Ian
 
Once you have it dragged and droped on to your form switch to code view.

Expand the "Windows generated code, do not modify" region.

Look for the line that defines that control that you dragged and droped. Use that definition as a template for defining the control in your other code.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks Rick, the definition in the windows code was myCustomControl.myCustomControl.

Using this as the type is now working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top