Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everybody,i have some problem with C#.Example,my project have a form which contains one button and one textbox.Can i make a array(example named "arr") contains the button and textbox.When i use button or textbox,i use arr[0] or arr[1].
Thanks!
Posted

Control Arrays in .Net is something that you should not really require.
However, Creating Control Arrays in Visual Basic .NET and Visual C# .NET[^] should help you out in case if you do want to do this.
 
Share this answer
 
Comments
BillWoodruff 25-Oct-11 2:39am    
Hi, Really agree with you that Control Arrays are not a wise choice.

I checked out the pseudo-code, and code, in the January, 2002, article you gave a link to. I see several things I think are errors, and I'd say the article itself is dis-organized and confusing. While it was probably a great waste of time to leave feedback to MS, I did. Just a heads-up.

However, I will admit to being quite confused about .NET in 2002, and since then, I have added whole new chapters to my encyclopedia of confusion :)
congtk88 4-Jun-12 4:03am    
i wanna change my interface's application.It is a reason that i wanna use array to manage my control.I use those array to contain my controls.When its interface changes,through those array,i can delete this and change my interface.Can you give me some advice to solve this problem.Thanks
Yes, you can use that now rather outdated, and very limited, type 'Array to hold Controls
Control[] BunchAControls = new Control[];
//
BunchAControls[0] = textBox1;
BunchAControls[1] = button1;
Or, you could use the ControlCollection object which at least supports 'AddRange:
// at your application top-level's Form scope
ControlCollection cCollection;
//
// you have to initialize a ControlCollection inside a Form instance because a Form instance reference must be supplied as a parameter to the ControlCollection constructor.
//
// example: in a Form Load event:
cCOllection = new ControlCollection(this);
//
cCollection.Add(textBox1);
cCollection.Add(button1);
Or you could get modern and use a generic List<Control>:
List<control> lControls = new List<Control>();
lControls.AddRange { textBox1, button1 };</control>
But, the big question to ask here is why you want such a collection of Controls:

1. since the collection contains only those "common" properites and methods shared by all Controls: if you want to access properties or methods unique, say, for a TextBox ... compared to Button: you'll have to convert the Control back to its "finalized" Type to expose those properties, methods, etc. Which means casting, which is usually something to be avoided.

Now, you may have a very good strategic reason to make an Array, Collection, or generic List of Controls. If you share what your goal is, here. Hopefully we can make some creative suggestions.

good luck, Bill
 
Share this answer
 
v3
Comments
congtk88 4-Jun-12 4:03am    
i wanna change my interface's application.It is a reason that i wanna use array to manage my control.I use those array to contain my controls.When its interface changes,through those array,i can delete this and change my interface.Can you give me some advice to solve this problem.Thanks
BillWoodruff 4-Jun-12 4:45am    
You got two thoughtful answers to this question 8 months ago: you have not bothered to respond to those answers, or, to answer the questions raised in my first answer. Now, eight months later, you are asking the same thing again ?
congtk88 4-Jun-12 11:03am    
Sorry about it.I read all your answer and used it to solve my problem.But now i just wanna improve it.I'm so sorry
BillWoodruff 4-Jun-12 11:24am    
Okay, I'll be happy to help you if I can. But first, I really need ... and I think anyone else trying to assist you will need ... a clear statement of what exactly is happening here, and what your goal is: how will the end-user experience your program: why does the interface need to change: why don't you just switch to another Form that you have hidden with a different interface, hide the Form that contains the interface you don't want the user to see, and make visible the second Form with the different interface

And, posting the code you are using now, with comments, please, I, and others, can look at it, and better understand what you want.

You say you are trying to improve your solution: please tell us exactly why it needs improving.

Finally, when someone helps you here, you could at least vote; when someone posts a helpful answer: vote it based on how helpful it is: when someone gives you an answer that is not helpful: you can vote that answer down.

Feedback, and quick reaction to answers given on QA, is important for the whole CP community !

thanks, Bill
congtk88 5-Jun-12 3:17am    
Thanks for your answer.At first, i am a student studying electronic,not IT.I just studying it because of my hobby and applying to my project.
I wanna change interface because that i have a lot of flow char which must display in my program.Each one have different interface(consist of some button and tooltip).When i change interface,my program must some seconds to load new interface.I wanna improve that.If you have email,i will send code to you.It is too long,i don't copy it here.Thanks

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