Solved

Form Inheritance Question.

Posted on 2006-10-24
8
241 Views
Last Modified: 2010-04-23
I've created a class (cForm) that Inherits a another form (iForm).

So, the class (cForm) is derived from the (iForm) form ....

When I add controls to the inherited form (cForm) they do not appear at runtime … am I missing something ?
0
Comment
Question by:Kinger247
  • 4
  • 3
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
What code are you using to add them ? And where have you put the code ?
0
 
LVL 10

Author Comment

by:Kinger247
Comment Utility
This is the code thatwas generated in the form when I dropped the controls onto it.
The IMDIForm doesn't have any controls on it, just a few additional procedures.

Public Class frmSearch
    Inherits Infinity_MDI.IMDIForm

    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(38, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(52, 78)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(39, 13)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Label1"
        '
        'frmSearch
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "frmSearch"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi Kinger247;

If cForm is the startup form then make sure that the Property page for the project, "Startup Form", is set to cForm and not iForm.

Fernando
0
 
LVL 10

Author Comment

by:Kinger247
Comment Utility
There's actually an mdi form that loads a child form.
Its the child form that inherits from another form.

But as well as inheriting from another form, I need to place some controls on the form itself (which are nothing to do with the inherited form) and these controls are not displayed when I run the app.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Can you post the code that loads the child form and displays it?
0
 
LVL 10

Author Comment

by:Kinger247
Comment Utility

Heres the code that opens the second form:

                Dim Form As New frmSearch
                Form.MdiParent = Me
                Form.Text = "Search"
                Form.Name = "Search"
                Form.WindowState = FormWindowState.Maximized
                Form.Show()

0
 
LVL 10

Author Comment

by:Kinger247
Comment Utility
Ok, this must be something I'bve missed or don't understand.

I just created a new project :
1. With a default form (Form1).
2. I then create a class (called MainForm) which inherits the main Form1. (this new class is now a subclass of Form1)
3. Change the startup form to (MainForm).
4. Add a button to (MainForm).
5 Run the application.

The MainForm is visible, but the button is not ....

In acutal fact, if I inherit from just "System.Windows.Forms.Form", I still don't see the button ....

Heres my code:

'//-- CODE ---------------

Public Class MainForm
    Inherits System.Windows.Forms.Form

    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(34, 54)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'MainForm
        '
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Name = "MainForm"
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
End Class


Please Help !
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi Kinger247;

Your main form should have looked like this following your steps.

Public Class MainForm
    Inherits WindowsApplication1.Form1    '<=== Note compare with yours

        Friend WithEvents Button1 As System.Windows.Forms.Button

    Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 96)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 270)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "MainForm"
Me.ResumeLayout(False)

    End Sub
End Class

The reason why the above does not work is because the form is missing the following.

'========Start
    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
    Private components As System.ComponentModel.IContainer
'========End

So the initialize componets never get called in MainForm so they do not get displayed at runtime.

Follow these steps to create what you want

1. Create a new project with default Form1
2. Compile the project just created. This needs to be done before creating MainForm
3. Go to Solution Explorer Window and right click on the project name.
    Select Add from the context menu then click on the option "Add Inherited Form..."
4. In the new window change the form name to MainForm.vb and click Open
5. In the new window that opens called "Inheritance Picker" select the only Form in the list box "Form1" and then click OK.
6. Add controls to this new inherited class, compile and run.
7. You now have controls on the form.

Fernando
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now