Solved

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

Posted on 2016-08-27
6
54 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 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 30

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 30

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array not updating 8 42
I need help parsing JSON in my MVC.Net Unit Test project 1 13
VB6 Enviroment 3 37
SQL Server 2012 to SQL Server 2016 24 57
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

751 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