none
Control auf Form erzeugen im Code RRS feed

  • Frage

  • Hi,

    will gerne wissen wie ich ein Control auf meiner Form erzeugt bekomme.

    pb[i] = new PictureBox();
    pb[i].Left = 400;
    pb[i].Top = 10;
    pb[i].Width = 20;
    pb[i].Height = 20;
    
    this.Controls.Add(pb[i]);
    

    Mit Controls.Add funktioniert es leider nicht.

    Sollte es ja eigentlich, aber tut es leider nicht.

    Mittwoch, 8. Dezember 2010 18:53

Antworten

  • Hallo,

    pb[i] = new PictureBox();
    pb[i].Left = 400;
    pb[i].Top = 10;
    pb[i].Width = 20;
    pb[i].Height = 20;
    
    this.Controls.Add(pb[i]);
     

    Mit Controls.Add funktioniert es leider nicht.

    schaut nicht verkehrt aus. Kann es sein, dass Du die PictureBox nicht siehst, weil sie die gleiche Hintergrundfarbe hat, wie das Formular (Standard Schaltflächenfarbe). Probier mal zusätzlich:

    pb[i].BackColor = Color.Red;
    
    

    IMO wird Dein Code übersichtlicher, wenn Du mit Size und Location arbeitest:

    pb[i].Size = new Size(20, 20);
    pb[i].Location = new Point(400, 10);
    

    Aber das hat nichts damit zu tun, dass Du die hinzugefügte PictureBox nicht siehst.


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    vb-faq.de
    Mittwoch, 8. Dezember 2010 21:17
  • Hallo alle zusammen,

    Versuch es dochmal mit:

    System.Windows.Forms.PictureBox PB = new System.Windows.Forms.PictureBox();
    PB.Size = new System.Drawing.Size(20, 22);
    PB.Location = new System.Drawing.Point(400, 10);
    

    Ich hoffe ich habe die Frage richtig Verstanden

    Wenn man sie nun noch nicht sieht, dann folgende Zeile hinzufügen:

    PB.Show();
    

     

    Freitag, 10. Dezember 2010 20:00

Alle Antworten

  • Hab auch schon mit folgendem versucht:

     

        pb[i].Visible = true;
        pb[i].Enabled = true;
        pb[i].Show();
        pb[i].Refresh();
        pb[i].Update();

    Mittwoch, 8. Dezember 2010 19:02
  • Hallo,

    pb[i] = new PictureBox();
    pb[i].Left = 400;
    pb[i].Top = 10;
    pb[i].Width = 20;
    pb[i].Height = 20;
    
    this.Controls.Add(pb[i]);
     

    Mit Controls.Add funktioniert es leider nicht.

    schaut nicht verkehrt aus. Kann es sein, dass Du die PictureBox nicht siehst, weil sie die gleiche Hintergrundfarbe hat, wie das Formular (Standard Schaltflächenfarbe). Probier mal zusätzlich:

    pb[i].BackColor = Color.Red;
    
    

    IMO wird Dein Code übersichtlicher, wenn Du mit Size und Location arbeitest:

    pb[i].Size = new Size(20, 20);
    pb[i].Location = new Point(400, 10);
    

    Aber das hat nichts damit zu tun, dass Du die hinzugefügte PictureBox nicht siehst.


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    vb-faq.de
    Mittwoch, 8. Dezember 2010 21:17
  • Hallo alle zusammen,

    Versuch es dochmal mit:

    System.Windows.Forms.PictureBox PB = new System.Windows.Forms.PictureBox();
    PB.Size = new System.Drawing.Size(20, 22);
    PB.Location = new System.Drawing.Point(400, 10);
    

    Ich hoffe ich habe die Frage richtig Verstanden

    Wenn man sie nun noch nicht sieht, dann folgende Zeile hinzufügen:

    PB.Show();
    

     

    Freitag, 10. Dezember 2010 20:00