Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 24-Oct-11 17:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
BillWoodruff at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v3
Comments
congtk88 at 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 at 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 at 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 at 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 at 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
BillWoodruff at 5-Jun-12 9:57am
   
Hi, please define what you mean by "flow char." And exactly what "triggers" the need to change the interface: some action by the end-user of your program: some change in signal you are receiving from some electronic device that you are monitoring via a serial-port ... or ... what ?
 
No, I can't take time to look at your source code; you'll have to post relevant sections here on CP QA. But, before you post any code here, you need to help us get much more clear on what is happening with your program. best, Bill
congtk88 at 5-Jun-12 11:48am
   
This is my problem.when my app start,its interface has some control.When i click a button,its interface changes(makes some new button,its location change).That is at all.Thanks!
BillWoodruff at 6-Jun-12 6:26am
   
Please answer these questions very carefully:
 
1. how many potential interfaces are there ?
 
a. unlimited ?
 
b. if limited: how many maximum
 
c. is there ever a time when you will want to display more than one interface ?
 
2. Why are you not using separate Forms right now, with each Form containing one possible interface, so that when the "whatever" triggers an interface change: you hide the current interface Form and show the appropriate other interface Form ?
 
3. Do you understand use of the 'Hide and 'Show methods on WinForms.
 
4. Is it your idea now to keep multiple Arrays of type 'Control, and then switch between them, using only one "master Form" ? Why do you think that's a good strategy ?
 
If you answer the above questions completely, I'll continue trying to help you with this, but I need you to show some effort here.
 
good luck, Bill
hothong at 13-Jun-12 3:49am
   
Thanks for your help.For your first question,i have ten interface and it is maxium. I wanna use alot of form but it is the way that i don't want using beacause of its volume. I also understand the "Hide" and "Show" properties.But in this case,it is not effect. I just wanna change interface when i click a button in older interface.
Thanks!
BillWoodruff at 13-Jun-12 4:06am
   
Hi Hothong, are you also "congtk88" who started this thread ?
 
To reply usefully to your question I need you to explain what you mean by "volume:" do mean that the ten interfaces you "switch" between are changed so often you are concerned that the end-user will have a bad visual experience ... or ?
 
Another question: are there controls common to all interfaces that are the same, and remain in the same position on every interface ? If there are, please tell us some specifics about those controls, and what they do.
 
good luck, Bill
congtk88 at 17-Jun-12 3:47am
   
Thanks for your help.But i think that we should stop it now.I will use the way what i used.Thanks again!
BillWoodruff at 17-Jun-12 4:07am
   
No problem, I hope I was helpful in some small ways. best, Bill

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 335
1 OriginalGriff 250
2 Kornfeld Eliyahu Peter 130
3 DamithSL 120
4 Peter Leow 95
0 OriginalGriff 7,355
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 25 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100