Beginner Help

Hey Everyone,

I need some help with a program im trying to make.
Im trying to make a personnel roster that is very flexible and customizable. I have a problems that I can't get around.

Problem:

The personnel roster is handled with a tabcontrol, and i want to make it so that when you click a certain button you create a new tab. My problem is when you click the button you get just a new blank tab instead of the other one that has textboxes that hold info like names, birthdate, etc, etc, i want to now how i can make it so that when you click the button it copy's the other tab but leaves the textboxes blank so the user can enter info themselves, and also how to make it so i can click a button an it erases the tab that i name in the textbox the User names. You see i have a textbox and 2 buttons for creating and erasing tabs, the user enters the name of the new tab in the textbox and clicks the button "create new tab", and the user can enter a name of a tab in the textbox then click the "erase button" and the name of the tab that was entered gets erased. fairly easy huh,
this is the code i got down so far.

Private void btnAdd_Tab click(object sender Event Args e)
{
// adds a new tab to the tabcontrol
tabcontrol1_Personnel.tabPages.Add(txtbox_TabName.Text);
}


This code works but every time i try to write code that erases a tab
I draw a blank.

// removes a tab from the tabcontrol
tabcontrol1_Personnel.tabPages.Remove(txtbox_TabName.Text);


This code doesn't work why i do not know.

I don't have a clue as to what code i should use to make it copy the
Info textboxes to the new tab. should i go to each textbox and copy the exact size and location of each textbox?

By the way Im using "visual C# 2008 express edition".











«1

Comments

  • So what you actually want to do here is create a "template" tab-page.

    The easy way to do this is to right-click on your project in the solution explorer. Select "Add", then Select "Class".

    Name it "TabControlTemplate" and add it to your project.
    Open up the new class file.
    you should see something like the following:
    [code]
    class TabPageTemplate
    {
    }
    [/code]

    Replace all the code with the following:
    [code]
    public class TabPageTemplate : TabPage
    {
    public TextBox SampleTextBox1 { get; private set; }
    public TextBox SampleTextBox2 { get; private set; }
    public TextBox SampleTextBox3 { get; private set; }

    public TabPageTemplate()
    {
    SampleTextBox1 = new TextBox();
    Controls.Add(SampleTextBox1);
    SampleTextBox2 = new TextBox();
    Controls.Add(SampleTextBox2);
    SampleTextBox3 = new TextBox();
    Controls.Add(SampleTextBox3);

    SampleTextBox1.Width = 200;
    SampleTextBox1.Height = 25;
    SampleTextBox1.Location = new Point(10, 10);
    SampleTextBox2.Width = 200;
    SampleTextBox2.Height = 25;
    SampleTextBox2.Location = new Point(10, 40);
    SampleTextBox3.Width = 200;
    SampleTextBox3.Height = 25;
    SampleTextBox3.Location = new Point(10, 70);
    }
    }

    [/code]

    Now - this class is inheriting "TabPage" which means you can add it to your TabControl.

    All the code in the constructor is adding 3 textboxes to the tab page.

    The three properties give you access to the three textboxes.


    Now all you need to do is create a new "TabPageTemplate" to your tab control each time you want to add a new tab and an identical - but new - version of your template.

    so...

    [code]
    tabControl1.TabPages.Add(new TabPageTemplate());
    tabControl1.TabPages.Add(new TabPageTemplate());
    tabControl1.TabPages.Add(new TabPageTemplate());
    [/code]

    That code will add 3 indentical tabs to your tab control.

    You can set up whatever controls you want on it and make it look however you want.

    If you want more detail let me know

    Hope that helps.
  • Hey man,

    thanks for taking the time and answering my question.

    I did what you said and created a Template, but im getting a build error
    that's saying im missing a Directive or and Assemble Reference,

    Error: The type or namespace name 'TextBox' could not be found,(are you
    missing a directive or an Assembly Reference?).

    How do i get around this?

    thanks:)
  • And the way i make so that when you click on a curtain button it creates a new tab with the same stuff as the other one is i add the
    add.tabpages(Tabpage Template); to the buttons Click Event right?
  • You may need to add a "using System.Windows.Forms;" to the top of your template page - basically in my example I am just adding 3 text boxes to the template page as an example of how to add controls to your template and position/size them.
  • you can basically do this off of any event you want - the exact functionality is up to you. I'm afraid I can't tell you want a standard functionality for this would be because I don't often work with tab-pages.
  • Thanks a lot you've been a big help

    but i've still got one more build error,

    it says im missing a Direct or Assembly reference for 'Point'

    any suggestions on how to get around this?

    Thanks:)
  • I believe "Point" is in System.Drawing
  • Yeah System Drawing did it

    Im coming up to the home stretch!, but i have a few build errors:(

    After i finished writing the necessary code I was ready to set it all in motion with a click of a certain button but...Build Errors were found

    I put this code into a buttons click event

    Private Void btnAddTab Click(objectsender EventArgs e)
    {
    TabControl.TabPages.Add(new TabPageTemplate());
    }

    And I got the following build Error

    Error (1): The best overloaded method match for
    'System.Windows.Forms.TabControl.TabPageCollection.Add
    (System.Windows.Forms.TabPage)' has some invalid arguments

    Error (2): Argument 1: cannot convert from
    'Personnel.TabPageTemplate' to
    'System.Windows.Forms.TabPage'

    I have no clue what this means

    so...Please Help!

    thanks:)


  • It means you didn't inherit from TabPage

    Look closely at the TabPageTemplate class I wrote out and make sure nothing is missing. I suspect the error you are receiving is because you did not put " : TabPage" after your class name.
  • yeah i forgot.

    well i made it past all the build errors, NOW im getting a run-time
    error that has to to with the textbox.width command,

    Textbox.Width = 243;

    Error: Null Reference Exception was unhandled
    Object reference not set to an instance of an
    object

    TroubleShooting tips:

    Use the "new" Keyword to create an object instance.

    Check to determain if the object is null before
    calling this method.


    I know i must be a drag answering all my stupid beginner questions

    im not just throwing problems out here without trying to figure them out on my own first.

    but thanks a lot you've been a big help:)

  • Forget the last question, i figured it out.

    i have a new problem, all my textboxs on my original tab have borders set to FixedSingle and some of my FontSizes are different, and some of the textBoxs have BackColors set to different colors besides White. my question is, how do i access the properties of a control to suit my requirements? I tried to figure it out but i drew a blank, and also i have a button for changing a picture, i got it copied just fine but..the code for changing the picture is only on the original, not the copy.

    Please help!

    thanks:)
  • I was expecting that question and forgot to post on it:

    so you can retrieve a TabPage like so...

    TabPage myPage = tabControl1.TabPages[0];

    However it will only be a TabPage so you can't access the text boxes. So what you have to do is "cast" it as a "TabPageTemplate" like so...


    TabPageTemplate myPage = (TabPageTemplate)tabControl1.TabPages[0];

    Now "myPage" will magically have .SampleTextBox1 on it.
  • Im sorry i don't quite follow.

    im putting this in a buttons click event right?
    i tried that and i got a run-time error, if you want to see it let me know, but i think i just put the code in the wrong place.
    everything copied just fine but my only problem at the moment is all the copied stuffs properties is all wrong, all my original textboxes and stuffs borderstyle is set to fixedSingle but all my copied textboxes and stuff is automatically set to Fixed3d and i don't want this,
    I have other property problems to but i won't wast time mentioning them,
    because if i can get this one fixed im almost positive that i can fix the others.

    im sorry could you explain it once more with a little bit more beginner detail


  • If you have changes you want made to all of your tab pages this should be done in the template class. So if you added a textbox to your template, then you should also set the .BorderStyle to whatever you want there. By altering this inside the template code - you don't have to do it multiple times elsewhere.



  • Thanks for clearing that up for me:)

    but...I hit a road Block,

    I tried to make one of the textboxes Backcolor to blue, but i got a build error

    Textbox1.Backcolor = new Color(45,82,255);

    This does not work and i cant figure out why.

    I tried to make the FontSize bigger but I got a build error.

    Textbox1.FontSize = 16;

    this does not work as well.

    and i tried to make the BorderStyle on some of the textboxes FixedSingle...no dice

    Textbox1.BorderStyle = Fixed Single;

    Nothing..

    some stuff did work though like making the MultiLine Property to True
    and the ReadOnly Property to False stuff like that.

    Any suggestions?

    Thanks:)


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories