Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Form Inheritance Question.

Posted on 2006-10-24
8
Medium Priority
?
250 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 64

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month5 days, 19 hours left to enroll

772 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