Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i have made an array. first of all i want to make it global and on form load i wanna show it's first position and on a button' s click i want to increase it's position by one how can i do that

Dim LabelArray(12) As Integer
LabelArray(0) = Label3.BackColor = Color.White
LabelArray(1) = Label4.BackColor = Color.White
LabelArray(2) = Label5.BackColor = Color.White
LabelArray(3) = Label6.BackColor = Color.White
LabelArray(4) = Label7.BackColor = Color.White
LabelArray(5) = Label8.BackColor = Color.White
LabelArray(6) = Label9.BackColor = Color.White
LabelArray(7) = Label10.BackColor = Color.White
LabelArray(8) = Label11.BackColor = Color.White
LabelArray(9) = Label12.BackColor = Color.White
LabelArray(10) = Label13.BackColor = Color.White
LabelArray(11) = Label14.BackColor = Color.White
Posted
Updated 8-Apr-12 3:55am
v2
Comments
[no name] 8-Apr-12 9:55am    
Format code with <pre> not [code]
Sergey Alexandrovich Kryukov 8-Apr-12 11:39am    
You want to show what? What is first position?
--SA

First of all, you are doing a bad thing. The name "Label114" indicates that you have inserted many labels manually using the designer. You have used so much patience — but for what? It's like Harry Potter who was punished by having to write "I shall not tell lies" on a blackboard many times, but who punished you? :-)

Also, who would need to see so many controls on a form at the same time? OK, I can assume you are doing it for learning purposes.

Instead of doing this useless job by hand, you should have written it all in code. Something like:

C#
int labelCount = //...
int interval = //...
int left = //...
int top = //...
Control parent = // some instance of a form, panel, etc.

Label[] labels = new Label[labelCount];
for (index = 0; index < labelCount; ++index) {
    Label label = new Label();
    labels[index] = label;
    label.Top = top;
    label.Left = left;
    label.BackColor = Color.Write;
    label.Text = //...
    //whatever else...
    parent.Controls.Add(label); //will show it
    top += label.Height + interval; 
}


To move them all together, I would rather move the containing panel; but if you really need to move the whole array, you can do it like this:
C#
foreach (Label label in labels)
   label.Top += 1;


Isn't that simple?

And finally, about the "global". There is no a concept of "global" in .NET (finally, thanks goodness!). You maybe simply need to use and modify some properties of some forms in other forms or other classes. This is basically reduced to a popular question on the form collaboration. One of the most robust approached is implementing appropriate interface in the form's class, instead of reference to a "whole instance" of a Form.

Please see my past solution for more detail:
How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
v8
Comments
VJ Reddy 8-Apr-12 11:59am    
Nice answer. +5
Sergey Alexandrovich Kryukov 8-Apr-12 12:09pm    
Thank you, VJ.
--SA
ProEnggSoft 8-Apr-12 12:53pm    
Well explained. +5
Sergey Alexandrovich Kryukov 8-Apr-12 12:57pm    
Thank you.
--SA
Monjurul Habib 10-Apr-12 1:02am    
5!
In addition to Solution 1, as related to form collaboration:

As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900