views:

52

answers:

3

hi, I just created a "Usercontrol" in WINFORMS- it just contains 1-Button with some style.

And i need to use the same as array(10) and load it to a form.

Ex:

Dim myButton() As Button = New ucSpecialButton(dataset4Category(i).Tables(0).Rows.Count - 1) {}

Here my usercontrol name is ucSpecialButton

can we create a ONE-Dimensional Array of a WINFORM usercontrol.?
+2  A: 

Yes, you can.

Control[] controls = new Control[10]; 

So, what's the problem?

MAKKAM
+3  A: 

With MAKKAM's words: Yes, you can. I guess you're actually uncertain about whether you can add a dynamic number of controls to a form, because in the designer you cannot define any arrays, you just drag and drop a certain number of controls on the form.

However, in fact Visual Studio simply generates some code in background that adds these controls to a collection. You can just as well write your own code to add an arbitrary number of UserControls to the collection dynamically. Just look at the forms' .designer.cs file to see how it works.

Taking MAKKAM's array controls it could look like this, e.g.:

public MyForm()
{
    InitializeComponent(); // this is the call to the auto-generated code

    // Here you could add you own code:
    foreach (Control control in controls)
    {
        this.Controls.Add(control); // this is how to add a control to the form.
    }
}
chiccodoro
A: 

I've just noticed that you edited your question. If I got it right, the only thing you're missing now is (I'm a C# guy, might be that there are some flaws in the following VB.NET code):

for i = 0 to dataset4Category(i).Tables(0).Rows.Count - 1
    myButton(i) = New ucSpecialButton();
    // ... specific button properties ...
next

For, the code you pasted in your question doesn't create the buttons yet, it only allocates memory for the array:

Dim myButton() As Button = New ucSpecialButton(
    dataset4Category(i).Tables(0).Rows.Count - 1) {}

New in this place means to create a new array for the references, not to create new objects. ucSpecialButton(...) in this place is not the constructor for an object. Instead it only denotes the type of object you want to prepare the array for. You can IMHO just as well write New Button(...).

By the way: IMHO it should be

`New ucSpecialButton(dataset4Category(i).Tables(0).Rows.Count)`

Without the - 1. In the for loop however, the - 1 is correct (an array of size 10 goes from 0..9).

chiccodoro
Yup u r rite but..! i cudn't ateast declaring array. Thats why posted this question.?
pvaju896
try removing the `{ }` and say "New Button" instead of "New ucSpecialButton". But with the very spare information you provide it's really hard to help you at all.
chiccodoro