Solved

inheriting parent

Posted on 2004-09-01
7
374 Views
Last Modified: 2010-04-23
Hi all.
I have a form named form1 with a pubilc property. I can make new forms of form1:
Dim frmNew as New form1
frmNew.show()

Every copy of form1 can have it's own optionForm to set the pubilc property of it's parent form.
How can I inherit the parent form and how can I refer to the parent form's property or controls?
Here I give you a little sample to try:
===========Form1:====================================
Option Explicit On
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()
        num = 10
        '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

    '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 Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(80, 48)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 40)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Set Options"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(72, 96)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(112, 32)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Show value of num"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(80, 16)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(96, 24)
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "Make a new Me"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(264, 150)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private num As Double
    Public Property NumValue() As Double
        Get
            Return num
        End Get
        Set(ByVal Value As Double)
            num = Value
        End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newFrm As New FormOption
        newFrm.Parent = Me
        newFrm.ShowDialog()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox(NumValue)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim frmNew As New Form1
        frmNew.Show()
    End Sub
End Class
'===========FormOption:=============
Public Class FormOption
    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
    Private 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 TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(40, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(200, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "20"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(80, 80)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(128, 48)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Set"
        '
        'FormOption
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 198)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "FormOption"
        Me.Text = "FormOption"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Me.Parent.NumValue(Val(TextBox1.Text))
        Me.Hide()
    End Sub
End Class
0
Comment
Question by:kouroshparsa
  • 3
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 11959858
You are simply trying to pass a value from one form to another; in this case, from an instance of FormOption back to Form1.

There are a myriad of ways to accomplish this...here is one:

' On Form1:
    ' Set the tag property of your new instance of FormOption to your instance of Form1 using "Me"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newFrm As New FormOption
        newFrm.Tag = Me
        newFrm.ShowDialog()
    End Sub

' On FormOption:
    ' You can use the Form1 reference in your Tag property of FormOption to change the NumValue property
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Tag.NumValue = Val(TextBox1.Text)
        Me.Close()
    End Sub

Idle_Mind
0
 
LVL 2

Author Comment

by:kouroshparsa
ID: 11960302
The problem is at FormOption:
   Me.Tag.NumValue=val(TextBox1.text)
cause an error:  An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll
Additional information: Object does not support this property or method.

I do not think that FormOption can change the value of the property unless it inherits it !
Any feasible idea is great.

Regards, kourosh
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11960356
Here is what I did:

I created a new project and added a form.  Then I changed the default Form1 to the code you provided for Form1 and changed the default Form2 to the code you provided for FormOption.

Next, the only changes I made were the ones listed above which I will list again.

'-----------------------
' On Form1:
'-----------------------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newFrm As New FormOption
        newFrm.Tag = Me
        newFrm.ShowDialog()
    End Sub

'-----------------------
' On FormOption:
'-----------------------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Tag.NumValue = Val(TextBox1.Text)
        Me.Close()
    End Sub

The program works perfectly on my system with no errors.

Idle_Mind
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Assisted Solution

by:planocz
planocz earned 50 total points
ID: 11962530
Here is a sample of two forms passing info....

'FORM1

Option Strict On
Imports System
Imports System.Globalization
Public Class Form1
    Inherits ControlledForm

#Region " Windows Form Designer generated code "

    Public Sub New(ByVal Controller As FormController)
        MyBase.New(Controller)

        '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

    '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 Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(20, 28)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(108, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Hello"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(176, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(68, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm2 As New Form2(Controller)
        frm2.Location = New Point(Me.Left + 200, Me.Top + 100)
        frm2.Show()
    End Sub
End Class


'FORM2

Option Strict On
Public Class Form2
    Inherits ControlledForm
#Region " Windows Form Designer generated code "

    Public Sub New(ByVal Controller As FormController)
        MyBase.New(Controller)

        '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
            Dim frm As Form = Me.Controller.Form("Form1")
            Dim lbl As Label = CType(Me.FindControl(frm, "Label1"), Label)
            lbl.Text = ""
            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

    '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 Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(300, 12)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(136, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 362)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form2"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As Form = Me.Controller.Form("Form1")
        If Not frm Is Nothing Then
            Dim lbl As Label = CType(Me.FindControl(frm, "Label1"), Label)
            If Not lbl Is Nothing Then
                lbl.Text = "Got here"
            End If
        End If
    End Sub
    Private Function FindControl(ByVal frm As Form, ByVal ctrlName As String) As Control
        Dim ctrl As Control
        For Each ctrl In frm.Controls
            If ctrl.Name = ctrlName Then
                Return ctrl
            End If
        Next
        Return Nothing
    End Function

End Class


'FORM FormController

Imports System.Collections
Public Class FormController
    Dim forms As New ArrayList

    Public Sub Add(ByVal frm As ControlledForm)
        If Not forms.Contains(frm) Then
            forms.Add(frm)
        End If
    End Sub
    Public Sub Remove(ByVal frm As Form)
        If forms.Contains(frm) Then
            forms.Remove(frm)
        End If
    End Sub
    Public Function Form(ByVal frmName As String) As ControlledForm
        Dim o As Object
        For Each o In forms
            If CType(o, Form).Name = frmName Then
                Return CType(o, Form)
            End If
        Next
        Return Nothing
    End Function
    Public ReadOnly Property AllForms() As ArrayList
        Get
            Return forms.Clone()
        End Get
    End Property
End Class

'FORM ControlledForm

Public Class ControlledForm
    Inherits System.Windows.Forms.Form
    Private m_Controller As FormController
    Public Sub New()
    End Sub
    Public Sub New(ByVal controller As FormController)
        m_Controller = controller
        controller.Add(Me)
    End Sub
    Public ReadOnly Property Controller() As FormController
        Get
            Return m_Controller
        End Get
    End Property

    Private Sub InitializeComponent()
        '
        'ControlledForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(288, 229)
        Me.Name = "ControlledForm"

    End Sub

    Private Sub ControlledForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Me.Controller.Remove(Me)
    End Sub

End Class

'MOD CLASS

Module Module1
    Public Sub main()
        Dim Controller As New FormController
        Dim frm1 As New Form1(Controller)
        frm1.ShowDialog()
    End Sub
End Module
0
 
LVL 2

Author Comment

by:kouroshparsa
ID: 11969071
I'm sorry Idle_Mind. You are correct as always...I'm a bit sick.
I assume your solution is limited; you can only pass one value.Can you pass an array from the Tag?
===================
Hi planicz. Your idea works well. With a Global point of view and inheriting the form. I think ControlledForm holds the present forms...but what does FormController do?

I got some good ideas from you both.
I have a mother form "MDI" which I should use to make new forms. Since I display the option form modaly (...ShowDialog) I do not need to know all the present forms, just the parent form that called for options.
In order to pass several options, another easy idea is to temporarily hold the values in the module or the MDI...then close the optionForm...

The Tag property is pretty safe if it can pass an array of type object.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11970345
>> I assume your solution is limited; you can only pass one value.Can you pass an array from the Tag?

You can pass any kind and any number of variables that you need.

When you are inside FormOption and use Me.Tag, you are actually accessing an instance of Form1 and as such can access anything that is declared Public on it.

If you need to pass an array then declare a public variable on Form1 and set it in the same manner as I showed in my example.

Note that the Intellisense in the IDE will not show you anything available when you type Me.Tag since it doesn't actually know what is inside the Tag property.

Idle_Mind
0
 
LVL 2

Author Comment

by:kouroshparsa
ID: 11978405
That's right. I just tried passing an array or passing items one by one, both work as you say.
The Tag property is very handly...I'm glad

Thanks to a beautiful mind.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

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…

705 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

20 Experts available now in Live!

Get 1:1 Help Now