Solved

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

Posted on 2016-08-27
6
29 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

17 Experts available now in Live!

Get 1:1 Help Now