Form inheritance

I have to have form2 inheriting all the methods and variable in form1. but I don't want to have the graphical stuff from form1. how to by pass that because when i specify form2 inherit from form1 and run the program, it shows on the graphical components of form1 on form2.


Answer this question

Form inheritance

  • Phill Midwinter

    In Form2's Load event, you could set Visible = False for the controls you don't want to see. Marc's approach is the right OOP way.



  • Derek at Potters Clay

    oh Im not doing any inheritance at this moment. i have 2 forms and wanna access some public variables on form 2 from form1. but intellisense didn't pick up those variables after i use an instance of form2 to access them.
    Is there something wrong with my IDE

  • Jassim Rahma

    There is a property on the control (when accessed through the designer) that states the scope. It is called "Modifier"
    You need to set this to protected (so that the variable/member can be accessed by anybody inheriting from the control.)
    You could alternately open up the designer file and change the private declarations onthe form controls to protected. Might be easier to do unless you just plan to do it to a few controls.


  • Nidonocu

    the InitializeComponent is called in the Form's constructors.
    So as I siad in my opening statement, yes you could technically do it say by moving them around to the OnInit and then make sure that your Form2 never called the Form1's onInit do it but you have no guarantee that the Designer is not going to wipe your settings and put what it knows is supposed to go there. You may also run into issues if the 2 controls happen who end up having the same name.

    As I recommended you should look at the 2 provided methods that I specified.

  • marcy

    That's going to be extremely difficult to do just because the designer is always going to get in your way.

    There are probably 2 ways to do this and you can decide whichone is best for you .
    Method #1 - Probably the easiest but not the best method
    FormA - Move all of your methods here
    Form1 inherits from FormA and has all of its regular interface
    Form2 inherits from FormA and hgas its own interface

    Method #2

    Decouple all of your logic and move it into a class that both Form1 and Form2 instantiates from.
    This way both Form1 and Form2 can use same methods but not be reliant upon one another.
    This method requires the most reworking but in the long run is the best "design" choice.

    Hope this helps.

  • nhaas

    If you think there is a problem with your inttelisense you can delete the .ncb file in your project directory with the project closed. When you reopen your project the intellisense will rebuild itself.

  • Binu Jeesman

    I want to access un button properties on form2 from form1. like changing the button text. I set the visibility to public and still can't see it. I didn't do any inheritance at this point yet.

  • Tarey Wolf

    It's kinda silly that we don't have the ability to disable that. but im sure theres a way to do it.

  • Sanjukta

    Make sure that the variables are declared as public.
    All properties/methods/members are private by default.
    public class Form1 { String FullName;
    public String MyPlace; }
    in Form 2 you have
    Form1 frm1 = new Form1();
    You can't access FullName because it is not public.
    You can however access MyPlace because we specified that it was public.
    frm1.MyPlace;


    Take a gander here for more information.

    Hope this helps.

  • Form inheritance