UserControl w WinForms - nie jest widoczny

0

Mam problem z dodawaniem UserControl do swojego okna w WinForms, możecie podpowiedzieć czego tu brakuje? Nic widocznego się nie dodaje, śledząc zawartość Controls podczas debudowania widać ze do panel1.Controls dodają się moje UserControls.

Zrobiłem taki projekt testowy, po kliknięciu klawisza ma się dodawać kolejna "KontrolkaWersjiProg : UserControl" na ciemniejszym panelu na Form'ie.
Każda niżej o 20px od poprzedniej.

Tak wygląda kod mojej "KontrolkaWersjiProg : UserControl"

    public partial class KontrolkaWersjiProg : UserControl
    {
        public static int Licznik = 1;

        public int Pozycja_X { 
            get { return Location.X; }
            set { Location = new Point(value, Pozycja_Y); } 
        }

        public int Pozycja_Y {
            get { return Location.Y; }
            set { Location = new Point(Pozycja_X, value); } 
        }

        public string Identyfikator { 
            get { return lbl_NrProg.Text; }
            set { lbl_NrProg.Text = value; }
        }

        public string WersjaNaEtykiete {
            get { return txtB_WerProg.Text; }
            set { txtB_WerProg.Text = value; }
        }

        public KontrolkaWersjiProg()
        {
            InitializeComponent();
            Pozycja_X = 20;   //250
            Pozycja_Y = 20 * Licznik;            
            Licznik++;

            Identyfikator = "Program" + Licznik + ":";
        }
    }

Tak wyglada zdarzenie pod klawiszem

        private void button2_Click(object sender, EventArgs e)
        {
            KontrolkaWersjiProg ctrl = new KontrolkaWersjiProg();
            ctrl.WersjaNaEtykiete = textBox2.Text;
            this.panel1.Controls.Add(ctrl);
            //this.panel1.Refresh();
        }

A tak wygladaja okno glowne i userControl.
okno glowne.PNGusercontrol.PNG

0

Automatyczny kod generowany przez środowisko dla okienka Form dowolnej WinForm aplikacji ma cos takiego.

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(389, 451);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.listBox1);
            this.DoubleBuffered = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

Rowniez tego probowalem i nadal nic

0

No dobra, a jakieś Visible = true albo Show()? Dodanie parenta?

0

tak juz probowalem:

        private void button2_Click(object sender, EventArgs e)
        {
            KontrolkaWersjiProg ctrl = new KontrolkaWersjiProg();
            ctrl.Visible = true;
            ctrl.WersjaNaEtykiete = textBox2.Text;
            this.panel1.Controls.Add(ctrl);
            this.DoubleBuffered = true;
            this.ResumeLayout(false);
            this.PerformLayout();
        }

i tez niec, a gdzie wywolywac Show()? i co masz na mysli "dodanie do parenta"? pol internetu twierdzi ze wystarczy

KontrolkaWersjiProg ctrl = new KontrolkaWersjiProg();
this.panel1.Controls.Add(ctrl);

ale u mnie nie dziala.

0

temat rozwiązany.

pomimo tego ze w designerze poprzeciągałem na UserControl Label i TextBox to to nic nie znaczy. dopiero jak zainicjowałem te kontroli w konstruktorze UserControl i w konstruktorze je dodałem do Usercontrol.Controls to wszystko ruszyło.

    public class KontrolkaWersjiProgramu : UserControl
    {
        private TextBox txtB_WerProg;
        private Label lbl_NrProg;

        public static int Licznik = 1;

        public int Pozycja_X  {
            get { return Location.X; }
            set { Location = new Point(value, Pozycja_Y); }
        }

        public int Pozycja_Y  {
            get { return Location.Y; }
            set { Location = new Point(Pozycja_X, value); }
        }

        public string Identyfikator   {
            get { return lbl_NrProg.Text; }
            set { lbl_NrProg.Text = value; }
        }

        public string WersjaNaEtykiete   {
            get { return txtB_WerProg.Text; }
            set { txtB_WerProg.Text = value; }
        }

        public KontrolkaWersjiProgramu()
        {   
            txtB_WerProg = new TextBox();
            txtB_WerProg.Font = new Font(new FontFamily("Microsoft Sans Serif"), 12);
            txtB_WerProg.Size = new Size(145, 26);
            txtB_WerProg.Location = new Point(89, 7);

            lbl_NrProg = new Label();
            lbl_NrProg.Font = new Font(new FontFamily("Microsoft Sans Serif"), 12);
            lbl_NrProg.Size = new Size(82, 20);
            lbl_NrProg.Location = new Point(2, 10);

            this.Controls.Add(txtB_WerProg);
            this.Controls.Add(lbl_NrProg);

            Pozycja_X = 20;
            Pozycja_Y = 30 * Licznik;
            Identyfikator = "Program" + Licznik + ":";
            this.Name = "UC_" + Licznik;
            Licznik++;

            this.Size = new Size(240, 40);
        }
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1