Solved

Form Inheritance Question.

Posted on 2006-10-24
8
245 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 63

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
Industry Leaders: 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 63

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 63

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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