Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    C# help with getting stuff to populate

    Right I have this peace of code. I want it duplicate the textBox, and at the same time, I want them to appear on other tabs in a multidimensional array, I know there is a way of doing it, but I just dont have a bloody clue where to go it has had me perplexed most for over a week now. Any help would be appreciated

    private void button1_Click(object sender, EventArgs e)
    {
    string title = "Round " + (tabControl1.TabCount + 1).ToString();
    TabPage myTabPage = new TabPage(title);
    tabControl1.TabPages.Add(myTabPage);
    tabControl1.SelectedIndex = tabControl1.TabCount;

    textBox9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
    label9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
    }

  • #2
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IIRC, you need to add the TextBox and Label controls to the Controls member of your TabPage... myTabPage.Controls.Add(...);

    This: textBox9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
    ... looks like it can be simplified to this: textBox9.Parent = myTabPage;

  • #3
    New Coder
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    private void button1_Click(object sender, EventArgs e)
    {
    	string title = "Round " + (tabControl1.TabCount + 1).ToString();
    	TabPage myTabPage = new TabPage(title);
    	tabControl1.TabPages.Add(myTabPage);
    	tabControl1.SelectedIndex = tabControl1.TabCount;
    
    	textBox9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
    	label9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
    }
    I can't understand why you are setting the SelectedIndex just to use it's value later on though here. Why not just use tabControl1.TabCount if that's the value you know you'll be using later on? SelectedIndex has other purposes.

    If you're just setting the parent to a specific TextBox though then you're not duplicating anything. Same thing with that Label. Although pionting that out, this won't work anyways unless you use tabControl1.TabCount - 1, becuase the indexing is 0 based...

    Example:
    Code:
    string title = "Round " + (tabControl1.TabCount + 1).ToString();
    TabPage myTabPage = new TabPage(title);
    tabControl1.TabPages.Add(myTabPage);
    
    TextBox T = new TextBox()
    {
    	Text = textBox1.Text,
    	Size = textBox1.Size
    };
    
    tabControl1.TabPages[tabControl1.TabCount - 1].Controls.Add(T);
    *Set other properties for the new TextBox accordingly if you want to change the position or anything else... You can do it this way because each TabPage is a container for controls, so it has a Controls.Add() method.
    Last edited by AceInfinity; 01-20-2013 at 03:44 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •