WHY INHERIT SYSTEM.WINDOWS.FORMS.FORM?

my question is why to inherit system.windows.forms.form?
i've been doing it for all forms in all my projects, but i still dont understand why i should be doing it. seems that without it the form still works, so whats the advantage then?
thanks
LVL 13
newyuppieAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi newyuppie;

It sounds like you are using VS .Net 2005. In the code for the form you do not have to insert Inherits System.Windows.Forms.Form because that is alread set for you in the partial class code file which gets combined at compile time with the user code for the form. So in that case you do not need to insert the inherts statement because it has been done for you already in the designer code.

Fernando
0
 
tolgaongCommented:
Suppose that you have a project with 100 screens (screen = form)
At the end of the development, you decided to put your logo to each forms right bottom.
If you haven't used inheritance, you should edit all the forms (100 forms), otherwise it will be enough to only change the derived form.
This is  just a single case.
0
 
newyuppieAuthor Commented:
i understand. so suppose i wanted to add my logo then, how would i do that supposing that on every form in my application i have used inherit system.windows.forms.form.
e.g. where do i find this top level system.windows.forms.form where all the other ones are inheriting from and how do i edit it?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Fernando SotoRetiredCommented:
Hi newyuppie;

To your question, "my question is why to inherit system.windows.forms.form?"

If a class inherits from System.Windows.Forms.Form then that derived class will now have access to the UI which places the visable form on the screen and all the objects that come with it such as Windows title bar, min and max buttons and the X button to close the window. It also gives you the ability to drag and drop other controls to the form for your use as well as the properties and functions defined in the System.Windows.Forms.Form object.

To the other question, "i've been doing it for all forms in all my projects, but i still dont understand why i should be doing it. seems that without it the form still works, so whats the advantage then?"

Unless a class inherits from System.Windows.Forms.Form or a class derived from System.Windows.Forms.Form it can not function as a windows form.

To your statement, "seems that without it the form still works"

Can you post some code where a class has NOT inherited from System.Windows.Forms.Form and functions as a form.

Thanks;

Fernando
0
 
Fernando SotoRetiredCommented:
Hi newyuppie;

In the situation that tolgaong gave you above you would need to create a new class which inherits from System.Windows.Forms.Form. Then add the company logo to this class and save it, Lets call it FormCompany.

Then in the 100 classes that you create those 100 classes should inherit from FormCompany. Now when you make changes to FormCompany it will be propagated down to the 100 classes.


Fernando
0
 
tolgaongCommented:
After reading FernandoSoto's post, I see that I got your question worng.

Let me clear myself.

The standart code contains the following code

public class Form1
   implements System.Windows.Forms.Form
... (Lots of code here)
End Class

Without the implements line, your code cannot be functional.

If you use "your inherited form" -Explained below-, you can use the benefits in my previous post.

Crate a new project.
Add a class.
write:

public Class MyForm
     implements system.windows.forms.form

    Private tempStr As String
    Public Property Tempa() As String
        Get
            Return tempStr
        End Get
        Set(ByVal value As String)
            tempStr = value
        End Set
    End Property

end class


Now, change your code

From:
public class Form1
   implements System.Windows.Forms.Form
... (Lots of code here)

To:
public class Form1
   implements MyForm
... (Lots of code here)
End Class
End Class
0
 
tolgaongCommented:
Try the code on a seperate solution. Or take a backup ;)
0
 
newyuppieAuthor Commented:
Fernando,
thanks for your explanation, i tried to make an example that didnt inherit and worked, but then i realized that the forms.designer partial class (where the buttons are drawn and usually hidden from the user) does inherit. i tried removing that line and nothing worked...
0
 
newyuppieAuthor Commented:
thanks to both of you for your very good answers. i will raise the question points to 500 so i can split 250 for each of you.
Fernando made me realise that without the inheritance forms won't work and also made me look further into my code and discover that the partial designer classes were indeed inheriting.

on the other hand, tolgaong assisted on this answer and gave me a very good piece of information and suggestion, which i will look into using on my next projects, i think it could be very useful.

thanks again,
P
0
 
Fernando SotoRetiredCommented:
Glad I was able to help. :=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.