Beginner Help - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Beginner Help

VGKingVGKing Posts: 18Member
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".











«13

Comments

  • DataDinkDataDink Posts: 135Member
    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.
  • VGKingVGKing Posts: 18Member
    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:)
  • VGKingVGKing Posts: 18Member
    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?
  • DataDinkDataDink Posts: 135Member
    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.
  • DataDinkDataDink Posts: 135Member
    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.
  • VGKingVGKing Posts: 18Member
    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:)
  • DataDinkDataDink Posts: 135Member
    I believe "Point" is in System.Drawing
  • VGKingVGKing Posts: 18Member
    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:)


  • DataDinkDataDink Posts: 135Member
    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.
  • VGKingVGKing Posts: 18Member
    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:)

«13
Sign In or Register to comment.