Autor Beitrag
3marci
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 61
Erhaltene Danke: 5

Windows 7 / Kubuntu 11.04
C# / vb.net / php / progress (VS 2010 Express / SharpDevelop / NetBeans / proAlpha)
BeitragVerfasst: Fr 29.04.11 13:48 
Hi Leute,

ich habe lasse während der Laufzeit das Programm mehrere Controls erstellen.
Diese Controls werden alle unterschiedlich benannt, z.B.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
string title = (tabControl1.TabCount + 1).ToString() + ". Instanz";
TabPage newPage = new TabPage(title);

TextBox t1 = new TextBox();
t1.Name = "textBoxUrl" + (tabControl1.TabPages.Count);

newPage.Controls.Add(t1);
tabControl1.Controls.Add(newPage);


Der Benutzer kann so viele neue TabPages erstellen wie er will.
Meine Frage ist nun, wie kann ich auf, sagen wir, textBoxUrl2.Text zugreifen wenn es die TextBox noch gar nicht gibt?


Übrigens habe ich schon eine for-Schleife:
ausblenden C#-Quelltext
1:
2:
3:
for (int i = 0; i < (tabControl1.TabCount - 1); i++ )
{
}
Dr. Hallo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110
Erhaltene Danke: 13

XP
C/C++/C#
BeitragVerfasst: Fr 29.04.11 14:18 
wo liegt der sinn auf eine textbox zuzugreifen, die es nicht gibt? :lupe:
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Fr 29.04.11 14:25 
Grundsätzlich gilt:
Auf ein Control das es nicht gibt kann man auch nicht zugreifen.
Wenn es beliebig viele Tabs und TextBoxes geben kann woher willst du dann auch wissen das du genau auf textbox 2 zugreifen willst?

Du könntest dir das richtige Control zum Beispiel aus der "Controls"-Collection der TabPage besorgen oder dir beim erstellen schon merken welche Controls es gibt (z.B. per Dictionary) oder wozu sie gehören.
3marci Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 61
Erhaltene Danke: 5

Windows 7 / Kubuntu 11.04
C# / vb.net / php / progress (VS 2010 Express / SharpDevelop / NetBeans / proAlpha)
BeitragVerfasst: Fr 29.04.11 15:02 
user profile iconDr. Hallo hat folgendes geschrieben Zum zitierten Posting springen:
wo liegt der sinn auf eine textbox zuzugreifen, die es nicht gibt? :lupe:

Ich will nicht auf eine TextBox zugreifen die es nicht gibt, ich will auf die TextBoxen zugreifen die es gibt.
Ich weiß genau wie viele TextBoxen es gibt und wie sie heißen, siehe int i bei meiner for-Schleife.
Das Problem ist dass ich zur Laufzeit des Programms auf die Textboxen zugreifen muss, da diese erst zur Laufzeit erstellt werden.

@ bakachan (oder Baka-chan xD)
Thx, werds mal versuchen...
Wer noch ne Idee hat bitte posten.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19315
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 29.04.11 19:26 
Auch wenn es über Controls.Find geht:
msdn.microsoft.com/d...collection.find.aspx

Besser ist es du nimmst einfach ein Array oder Dictionary und steckst da deine Controls hinein. Dann kannst du die direkt finden. ;-)
Dr. Hallo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110
Erhaltene Danke: 13

XP
C/C++/C#
BeitragVerfasst: Fr 29.04.11 23:38 
du kannst dir auch ne klasse von tabPage ableiten und eine TextBox reinstecken etwa so...

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
    class CTabPage : TabPage
    {
        private TextBox _tb = null;
        public CTabPage(string name)
        {
            _tb = new TextBox();
            _tb.Location = new System.Drawing.Point(1010);
            _tb.Name = name;
            this.Controls.Add(_tb);
        }

        public TextBox GetTextBox()
        {
            return _tb;
        }
    }


Anwendung dan etwa so...

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
CTabPage tp = new CTabPage("textBoxName");
tabControl1.Controls.Add(tp);

...
for (int i = 0; i < (tabControl1.TabCount); i++)
{
    ((CTabPage)tabControl1.TabPages[i]).GetTextBox().Text = "Huhu";
}