• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

How to have an array of a control in VS.net?

How to make some one-type controls like to number-buttons or functional-buttons of a calculator by same names as an array in vb.net or c#?
Is it easier than vb6?
1
Sia Azizi
Asked:
Sia Azizi
  • 2
  • 2
5 Solutions
 
DaveSoftware DeveloperCommented:
C# is simpler and it comes under the Microsoft Support statement, wherein VB6 is deprecated.

Here is a simple example with TextBox control, that you can look at it. In .NET you would create an array of controls, then you would instance a TextBox control for each element of the array, setting the properties of the control and positioning it on the form.

TextBox[] txtArray = new TextBox[500];
    for (int i = 0; i < txtArray.length; i++)
    {
      // instance the control
      txtArray[i] = new TextBox();
      // set some initial properties
      txtArray[i].Name = "txt" + i.ToString();
      txtArray[i].Text = "";
      // add to form
      Form1.Controls.Add(txtArray[i]);
      txtArray[i].Parent = Form1;
      // set position and size
      txtArray[i].Location = new Point(50, 50);
      txtArray[i].Size = new Size(200, 25);
    }

.
.
.
Form1.txt1.text = "Hello World!";

Open in new window

If you want to learn in detail, then have a look at Microsoft documentation as well. https://msdn.microsoft.com/en-us/library/aa289500(v=vs.71).aspx
0
 
ScobberCommented:
dim k as new list of textbox
for I as integer = 1 to 100
dim j as new textbox
j.top=I*10
j.left=I*10
j.text=I
j.visible = true
me.controls.add(j) ' add to form surface
k.add(j)
next

Open in new window


You can set all the parameters of a textbox at runtime.
if you want to find one easily, you can use a dictionary instead and follow the same idea

dim k as new dictionary (of integer, textbox)

for I as integer = 1 to 100
dim j as new textbox
j.top=I*10
j.left=I*10
j.text=I
j.visible = true
k.add(I,j) ' Add to associative array
me.controls.add(j) ' add to form surface
next

Open in new window

to control the controls using the dictionary you can then say
 k(100).text = "Hi There I am box 100"

Open in new window

0
 
ScobberCommented:
Both replies above can intercept events using the addhandler statement

if you create your controls. and you have a subroutine with a statement like

Private Sub txChanged(sender As Object, e As EventArgs) 
     Dim o As TextBox
        o = CType(sender, TextBox)
        MsgBox(o.Text)
   end sub

Open in new window

The sender object can be any object that is raising the sub.
to make the autocomplete work properly you want to cast this object, otherwise you could just do the following. Bearing in mind that autocomplete wont know the function names because it is expecting the primitive 'object' class
Private Sub txChanged(sender As Object, e As EventArgs) 
        MsgBox(sender.Text)
   end sub

Open in new window


to enable the handler you just call something like this
addhandler ##event source##, addressof ##subroutine to execute##
 AddHandler k(100).TextChanged, AddressOf txChanged
0
 
anarki_jimbelCommented:
The main point here for me - do you really need an array of controls? What is the goal?
I'd considered control arrays as not really useful. I worked on converting huge VB6 application to .Net. And all control arrays were removed over couple of years, and no one regretted this :).

Still, if you want to do so, you may follow MS guidelines (pretty old):
Creating Control Arrays in Visual Basic .NET and Visual C# .NET

See also:

Life Without Control Arrays in Visual Basic .NET
0
 
anarki_jimbelCommented:
I decided to add a new comment. To support that control arrays is not a good idea.
For example, Dave above says that
C# is simpler and it comes under the Microsoft Support statement, wherein VB6 is deprecated.
If you have a look at the link provided https://msdn.microsoft.com/en-us/library/aa289500(v=vs.71).aspx you'll see:
1. Nothing simple. You need to create a new class, add events to this class (the example has Click event only, but what if you need other evets, mouse events etc?), quite a lot of plumbing you didn't need in VB.6.
2. You may also see "This documentation is archived and is not being maintained.". This simply means that Microsoft does not really support the idea itself, this was needed in the transitional period only.

In some cases it makes sense to add controls to collections but this is not same as control arrays in VB.6.
You may also add a common event handler to all controls. However you need to provide handling  based on a control name (?) or Tag value (?) or even control text (???). Still in VB 6 handling was based on index which wasn't that different.

From my point of view, Scobber provided may be not very clear but still good example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now