Autor Beitrag
TheLol
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Mi 13.08.08 21:14 
Meine Frage ist im Prinzip sehr einfach, jedoch schwer zu umschreiben ;).
Beispiel:
ausblenden 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:
ausblenden 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:
ausblenden C#-Quelltext
1:
2:
3:
pictureBox1
pictureBox2
...

statt
ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 123
Erhaltene Danke: 7

Win7 x64/86 WinServer 2008 R2 x64
C#/C++/C VS2010
BeitragVerfasst: Mi 13.08.08 21:50 
Hey,
sofern ich dich richtig verstanden habe, könntest du es zum Beispiel so machen:

ausblenden 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 Threadstarter
Hält's aus hier
Beiträge: 9



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

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 123
Erhaltene Danke: 7

Win7 x64/86 WinServer 2008 R2 x64
C#/C++/C VS2010
BeitragVerfasst: 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:

ausblenden 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


Zuletzt bearbeitet von Flitzs am Mi 13.08.08 22:42, insgesamt 6-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 14.08.08 10:54 
Wenn es um Komponenten auf einer Form geht, kannst Du Dir das hier mal angucken :-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".