Solved

Form Inheritance Question.

Posted on 2006-10-24
8
243 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
ID: 17797298
What code are you using to add them ? And where have you put the code ?
0
 
LVL 10

Author Comment

by:Kinger247
ID: 17797351
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
ID: 17797731
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 10

Author Comment

by:Kinger247
ID: 17797848
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
 
LVL 62

Expert Comment

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

Author Comment

by:Kinger247
ID: 17798741

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
ID: 17802442
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
ID: 17803698
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

18 Experts available now in Live!

Get 1:1 Help Now