Entwickler-Ecke

WinForms - bezeichner zur Leufzeit bestimmen


TheLol - Mi 13.08.08 21:14
Titel: bezeichner zur Leufzeit bestimmen
Meine Frage ist im Prinzip sehr einfach, jedoch schwer zu umschreiben ;).
Beispiel:

C#-Quelltext
1:
2:
3:
4:
for(int i = 0; i <= 50; i++)
{
this.pictureBox[i].Location = new System.Drawing.Point(0,0);
}


sehr simples beispiel ich möchte mehre pictureboxen bewegen.
Und um nicht z.b. 50 mal zu schreiben:

C#-Quelltext
1:
2:
3:
this.pictureBox1.Location = new System.Drawing.Point(0,0);
this.pictureBox2.Location = new System.Drawing.Point(0,0);
....

würde ich das gerne so wie in den Beispiel oben machen, blos die pictureBox ist ja keine Array.

In einer anderen Sprache gingt so was in so ^^: pictureBox&i&
Das & ist sich im Pinzip weg zu denken, so das dann nur da stehen würde:

C#-Quelltext
1:
2:
3:
pictureBox1
pictureBox2
...

statt

C#-Quelltext
1:
2:
3:
pictureBox&1&
pictureBox&2&
...


Ich hoffe ich konnte das Problem ausreichend schildern.

mmfg. TheLol

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Mi 13.08.2008 um 21:17


Flitzs - Mi 13.08.08 21:50

Hey,
sofern ich dich richtig verstanden habe, könntest du es zum Beispiel so machen:


C#-Quelltext
1:
2:
3:
    foreach(Control C in this.Controls)
                if (C is PictureBox)                                   
                      ((PictureBox)C).Location = new Point(00);


Dies funktioniert aber nur wenn alle Pictureboxen auf dem selben Steuerelement sind.

mfg Flitzs

PS: Den cast kannst du auch weglassen wenn du eine Eigenschaft ändern willst, die "Control" auch hat.


TheLol - Mi 13.08.08 22:22

Danke für deine Antwort
Und wie müsste man so was machen? (ohne die Array Variante)


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
namespace ConsoleApplication1
{
    class Program
    {
        string str0 = "Hallo 0";
        string str1 = "Hallo 1";
        string str2 = "Hallo 2";
        string str3 = "Hallo 3";
        string str4 = "Hallo 4";
        string str5 = "Hallo 5";
        string str6 = "Hallo 6";
        string str7 = "Hallo 7";
        string str8 = "Hallo 8";
        string str9 = "Hallo 9";
        string str10 = "Hallo 10";

        static void Main(string[] args)
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine(((str)i).ToString());
            }
        }
    }
}


Flitzs - Mi 13.08.08 22:34

Hey,

ähm ja mir fällt dazu im Moment nur die Variant mit Reflection ein die in etwa so aussehen könnte:


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
 
        public string str0 = "Hallo 0";
        public string str1 = "Hallo 1";
        public string str2 = "Hallo 2";
        public string str3 = "Hallo 3";
        public string str4 = "Hallo 4";
        public string str5 = "Hallo 5";
        public string str6 = "Hallo 6";
        public string str7 = "Hallo 7";
        public string str8 = "Hallo 8";
        public string str9 = "Hallo 9";
        public string str10 = "Hallo 10"

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {           
            for (int i = 0; i < 11; ++i)
                MessageBox.Show(this.GetType().GetField("str" + i).GetValue(this).ToString());                     

              
        }


Allerdings rate ich dir davon ab, da:
1) Reflection langsam ist
2) Ich mir fast sicher bin, dass es eine bessere Lösung für dein Problem gibt (schau dir mal System.Collections.Generic.List<T> an).

mfg Flitzs


Christian S. - Do 14.08.08 10:54

Wenn es um Komponenten auf einer Form geht, kannst Du Dir das hier [http://www.c-sharp-forum.de/viewtopic.php?p=512891#512891] mal angucken :-)