Solved

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

Posted on 2016-08-27
6
34 Views
1 Endorsement
Last Modified: 2016-10-15
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
Comment
Question by:Sia Azizi
  • 2
  • 2
6 Comments
 
LVL 3

Assisted Solution

by:Dave
Dave earned 100 total points
ID: 41773461
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
 
LVL 7

Assisted Solution

by:Scobber
Scobber earned 200 total points
ID: 41773507
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
 
LVL 7

Assisted Solution

by:Scobber
Scobber earned 200 total points
ID: 41773511
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
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 200 total points
ID: 41774090
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
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 total points
ID: 41793597
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now