views:

595

answers:

2

Hi all,

Having a slight problem on C#, still quite new to the language but hoping you can help. I have a program which dynamically creates tab forms and then I'm trying to add controls to the tabform (text boxes and labels), but no matter what I try it just doesn't seem to want to work. Here's the code I'm currently using (just to get one textbox in each form):

int i = dogresults;
while (i > 0)
{
    i--;
    DataRow dogrow = ds1.Tables["confirmdogs"].Rows[i];
    string dogname = dogrow.ItemArray.GetValue(3).ToString();
    TabPage newpage = new TabPage(dogname);
    tcNewCustomer.TabPages.Add(dogname);

    TextBox tb1 = new TextBox();
    tb1.Location = new Point(20, 10);
    newpage.Controls.Add(tb1);
    tb1.Name = "txtDogNo" + i;
}

Any help would be greatly appreciated!

+1  A: 

EDIT: Doh! Got it!

You're not adding the new TabPage you're creating. This line:

tcNewCustomer.TabPages.Add(dogname);

should be like this:

tcNewCustomer.TabPages.Add(newpage);

(A small test app shows the tab pages being created without any textboxes with the first version, but with the second version working fine.)


That looks okay at a glance (although I haven't tried it - a short but complete demo program would help). When you say it "just doesn't seem to want to work" - what exactly is happening?

Have you tried moving the location down a bit? I know some controls are odd in terms of where their logical "top" is (i.e. it's not the first visible pixel).

Jon Skeet
As I'm guessing you assumed, it simply isn't visible. Tried moving the x,y co-ords inside the location point, but still nothing.
David Archer
Okay, I'll try to reproduce it.
Jon Skeet
Oh goodness me, what a silly mistake that was! Thanks so much :)
David Archer
A: 

What about setting the text in the textbox? Currently you're just setting the name...

Although I'd still expect you to see a border on the box + background colour assuming that differs from the tabpage background.

Ian
Just given that a try but it's still not visible
David Archer