Results 1 to 6 of 6

Thread: How to Modify the Form behaviour?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    22

    How to Modify the Form behaviour?

    I have a problem with form instantiation. This is what I want to achieve:
    I want to create forms with a toolstrip attached to them right from the moment these forms are instantiated. I have several ideas:
    1. making custom Form Control
    2. modifying the new method for forms

    I'd like to focus on idea 2. I want to know if there's a way to alter the in-built New() method, so whenever the compiler instantiates a form, somehow when the constructor(New method) of each form is invoked, the compiler adds a toolstrip to the form.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to Modify the Form behaviour?

    You're looking at this the wrong way. You're talking about modifying the New method, i.e. the constructor, which requires you to create your own form anyway, which is just the first option anyway.

    You need to do the same as we always do: create a project and add a form to it. This will create a new class that inherits the Form class. You then modify that class as is appropriate by editing it in the designer or in the code window or both. If you want a ToolStrip then you can write code to create it if you want to but there's really no point. The form designer in VS is just a visual aid. Everything you do there is used to generate code anyway, so why write it yourself? If you add a ToolStrip to the form then code is added that creates a ToolStrip and configures it properties. That code goes into the InitializeComponent method, which is called from the constructor.

    If you want to see that code for yourself then open the Solution Explorer and click the Show All Files button, then expand the node for your form and double-click the Designer.vb file. Examine the contents of this file and then go to the designer and make a change. If you now go back to the designer code file you'll see that it's been updated to reflect your changes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    22

    Re: How to Modify the Form behaviour?

    Is there no way that I only need to write a piece of code once, then somehow propagate the code throughout the entire project. This way, I don't have to edit the constructor of each form in this project. The whole point is to cut the leg work. I declare the code once, and voila every time I add a form, the toolstrip is already attached to the form automatically.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to Modify the Form behaviour?

    Yes there is. It's called inheritence.

    1. Create a new project.
    2. Add a form named Form1.
    3. Add a ToolStrip to the form.
    4. Build your project.
    5. Right-click your project in the Solution Explorer and select Add -> New Item, then select Inherited Form.
    6. Select Form1 as the base class when prompted.

    Tada! Your new form has a ToolStrip, inherited from its base class, which is Form1.

    If you want to do this in an existing project then I think you should be able to just open the designer code file for each form and change the "Inherits System.Windows.Forms.Form" to "Inherits X" where X is your base form that contains the ToolStrip and any other controls you want on every form.

    Now, having said all that, it appears that you then can't actually make any changes to those controls, like add buttons to the ToolStrip, in the designer for the derived form. I thought that was only the case if they were declared Private but it appears not. If you don't need to make any changes in the derived form then there's no issue. I might investigate a bit further to see why you can't though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    22

    Re: How to Modify the Form behaviour?

    Gotcha!!
    That's exactly what I did. I couldn't make any changes in the toolstrip. It appears that you still have to add Sub New() in each of inherited forms. That's what I wanted to avoid. I don't want to do the same thing again and again. This is the case:
    I want to make forms with New, Edit and Delete Button. each user have their own unique permission for each button in each form. Let's say I have FormA and FormB. Both have all New Edit and Delete button. The permission list for User A and B:
    User A
    FormA --> New (means that this user can only see New button in FormA, the other buttons visible property is set to false)
    FormB --> Delete

    So I every time I open a form, in the form_load event I read the user's permission and set the toolstrip item's visible property accordingly to the corresponding user's permission.

    In my older coding, I do this by adding toolstrip to all the forms and during form_load event, I call a sub to set the toolstrip visible property. What I want now is that all the forms add a toolstrip to themselves automatically without me having to do anything. I tried using the inheritance trick, but there's still a downside. You still have to declare the New() sub in all the forms (which what I hate). Hope you can find what I coudn't. Thanks.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to Modify the Form behaviour?

    Just add all the buttons in the base class and then hide the ones you don't want in each derived class. There's no way to avoid writing some code in the base classes because you want to do something different in each one, but you can minimise the code you have to write by doing as much in the base class as possible. You could even define protected Boolean properties in the base class and have the base class read those property values and configure the ToolStrip accordingly. You then simply override those properties in the base classes as required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width