Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-27
6
Medium Priority
?
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 3

Assisted Solution

by:Dave
Dave earned 400 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 800 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 800 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 30

Accepted Solution

by:
anarki_jimbel earned 800 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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 800 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
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…
Suggested Courses

670 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