luka
luka

Reputation: 101

add control to Panel and control doesn't display anymore c#

I have one textbox (textBox1) and panel (Panel1) I have code like this

Panel1.Controls.Add(textBox1)

so when I run it I can't see textbox anymore, If I do like this I can see textBox

textBox1.Location  = Panel1.Location

can anyone tell me what's problem?

Upvotes: 1

Views: 594

Answers (2)

Napivo
Napivo

Reputation: 593

When a textbox (or any control) is part of a panel the top left of the panel is point(0.0);

so when textBox1.Location = Panel1.Location the textbox probably falls out of view in the panel.

try something like this instead/

        // 
        // panel1
        // 
        this.panel1.Controls.Add(this.textBox1);
        this.panel1.Location = new System.Drawing.Point(59, 27);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(193, 176);
        this.panel1.TabIndex = 1;
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(0, 0);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;

Upvotes: 1

Seth
Seth

Reputation: 199

I believe the reason you're not able to see the Textbox has to do with the Panel's properties. Try setting the AutoSize property to true and the AutoSizeMode property to GrowAndShrink.

Upvotes: 0

Related Questions