Form Inheritance Problem

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I have a class that inherits form a form that I have written with new property's and three of my own buttons for clsing, minimizeing, and resizing. This works great. When I inherit from this form the ide shows the buttons and extra property's as well. Also when I run the code the buttons and all work exaclty like I wanted.. flawless. However..... WHen I add just one control to the inherited form on the ide the three control buttons I placed on the base form return to their defualt positions and do not anchor. I tried to overcome this by changing the anchor properties on the load event of the new form. Dosn;t work. Also, I can't access the buttons on the IDE with the new form. That makes since to me b/c I have inherited them. I over came this by making changes to the buttons during the resize event which is was the anchor property does behind the scenes but is there a better way. If you need code I can post it but it's alot and I basically just wanted to know if this is a known glitch to anyone and or if there is a known technique that overcomes it. If need be I will post code.
 
Hello Buddy,

Well, I could not get the actual situation of yours but I have been dealin with much more complex form inheritance. Could u please post ur code so that it is easy to have a better understandin of the whole picture. I would try to find some solution then.
 
here is a copy of the form I inherit from.. it's only this windows generated code b/c that's all there is to it.

Imports Microsoft.Win32
Imports System.Runtime.InteropServices 'allows dll import
Imports System.Threading
Public Class Basic_Form
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Protected components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ButtonClose As System.Windows.Forms.Button
Friend WithEvents ButtonResize As System.Windows.Forms.Button
Friend WithEvents ButtonMinimize As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ButtonClose = New System.Windows.Forms.Button
Me.ButtonResize = New System.Windows.Forms.Button
Me.ButtonMinimize = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'ButtonClose
'
Me.ButtonClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ButtonClose.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.ButtonClose.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ButtonClose.Location = New System.Drawing.Point(532, 8)
Me.ButtonClose.Name = "ButtonClose"
Me.ButtonClose.Size = New System.Drawing.Size(32, 17)
Me.ButtonClose.TabIndex = 36
Me.ButtonClose.Text = "C"
'
'ButtonResize
'
Me.ButtonResize.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ButtonResize.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.ButtonResize.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ButtonResize.Location = New System.Drawing.Point(500, 8)
Me.ButtonResize.Name = "ButtonResize"
Me.ButtonResize.Size = New System.Drawing.Size(32, 17)
Me.ButtonResize.TabIndex = 35
Me.ButtonResize.Text = "R"
'
'ButtonMinimize
'
Me.ButtonMinimize.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ButtonMinimize.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.ButtonMinimize.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ButtonMinimize.Location = New System.Drawing.Point(468, 8)
Me.ButtonMinimize.Name = "ButtonMinimize"
Me.ButtonMinimize.Size = New System.Drawing.Size(32, 17)
Me.ButtonMinimize.TabIndex = 34
Me.ButtonMinimize.Text = "M"
'
'Basic_Form
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(580, 300)
Me.ControlBox = False
Me.Controls.Add(Me.ButtonClose)
Me.Controls.Add(Me.ButtonResize)
Me.Controls.Add(Me.ButtonMinimize)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Name = "Basic_Form"
Me.Text = "Basic Form"
Me.ResumeLayout(False)

End Sub

#End Region



Here is a code sample from the new form in which I inherit from the previous one. This code is simple

Public Class Form_Main
Inherits Basic_Form

End Class


This code works and the form behaves just as it should on the IDE and when called. But, when I add a control to it say a button, then the buttons I placed on the previous form lose the anchoring properties. In order to make it work I had to position the MyBase.buttons each time during resizing. WHy did the anchor properties not transfer? And also, I tried reassigning the anchor properties to the base buttons but that didn't work either.
 
Are you creating these inherited forms by selection Add -> Add Inherited Form. I tried to inherit a form when I first started by just creating a form and then adding the "Inherits" line myself. It seemd to do some things properly but not all so I gave up. It wasn't until a while later that I noticed the additional option on the right-click menu.
 
Good point... i think i did that with some of the forms and some not. I learned that after I started too. I'll have to double check but I think I tried it both ways and still got the error. It works great to few the inherited version of the form on the IDE with the new property's and I can even change the property's and all and watch the effects on the IDE, however, if I add a new control to the form it seems that when the IDE re-writes the auto generated code and overwrites somthing in the base class that I need. Everything still works fine but the base class buttons lose the anchor propery's immediately and I can see it on the IDE right away. If the Base form was originally 300, 300 then the buttons move back to that original position regardless of what size the inherited form is. Same thing when it's running.
 
Heyy, Just try to make the controls in your base form as Protected n then inherit the form by specifyin the inherits keyword in the codin only.

The main aim behind this is to make the Base Controls as non-editable in the inherited form so that u r not allowed to change any property.

Just give one try to this.

Cheers
 
Back
Top