Avatar of Ess Kay
Ess Kay
Flag for United States of America asked on

CType(Me.ParentForm, Form1).control1.Text = "Title bar text"

i have two forms

Form1 and Form2

Form1 has a panel. It loads form2

Form2 has code like this


Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

'Title of Control1
CType(Me.ParentForm, Form1).control1.Text = "Title bar text"

End Sub

Open in new window


Needless to say this works fine

Here comes the problem:
I added a new form: Form3
also with a panel with Form2 inside

now i have a problem with this CType(Me.ParentForm, Form1).control1.Text


I would also like to have this fixed to accept any future parent form, instead of writing something like

if parentform.name = "form1" then
    CType(Me.ParentForm, Form1).control1.Text = "bla1"
else if parentform.name = "form2" then
   CType(Me.ParentForm, Form2).control1.Text = "bla bla2"
else if .....to infiniti
    CType(Me.ParentForm, FormInfini).control1.Text  = "blablablafiniti"
end if



please help
Visual Basic.NET.NET ProgrammingProgramming

Avatar of undefined
Last Comment
Ess Kay

8/22/2022 - Mon
kaufmed

Create a new interface definition which both child forms will implement. This interface will provide a way to access the control1.Text.

Public Interface IControl1
    Property Control1Text() As String
End Interface

Open in new window


Then implement that interface on both forms:

Public Class Form2 : Form
    Implements IControl1

    '  Original code

    Public Property Control1Text() As String
        Get
            Return Me.control1.Text
        End Get
        Set
            Me.control1.Text = value
        End Set
    End Property

End Class

Public Class Form3 : Form
    Implements IControl1

    '  Original code

    Public Property Control1Text() As String
        Get
            Return Me.control1.Text
        End Get
        Set
            Me.control1.Text = value
        End Set
    End Property

End Class

Open in new window


Finally, cast to the interface type:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    'Title of Control1
    CType(Me.ParentForm, IControl1).Control1Text = "Title bar text"
End Sub

Open in new window

Angelp1ay

You might as well swap the CType for a DirectCast too. For reference types they are functionally identical and DirectCast is marginally more performant. It will make minimal difference in this case but it's a good habit to get into.
Ess Kay

ASKER
@kaufmed, that was just an example. the real Form2 has over two thousand lines and tons of controls ( grids, textboxes, labels, trees, you name it)

I can't sit there and recreate all controls, nor the form itself



i need something to return the parentForm


Like the If statement, but dynamiccally
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
kaufmed

You might try:

Dim c() As Control = Me.ParentForm.Controls.FindControl("control1", True)

If c IsNot Nothing AndAlso c.Length > 0 Then
    c(0).Text = "bla bla2"
End If

Open in new window

Ess Kay

ASKER
How will that return the parentform
kaufmed

It won't, but based on your original code snippet you're after a control that resides on the parent form. That is what the above should do.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ess Kay

ASKER
the problem is that i have many differnt controls.

instead of this

if parentform.name = "form1" then
    CType(Me.ParentForm, Form1).control1.Text = "bla1"
else if parentform.name = "form2" then
   CType(Me.ParentForm, Form2).control1.Text = "bla bla2"
else if .....to infiniti
    CType(Me.ParentForm, FormInfini).control1.Text  = "blablablafiniti"
end if


i need to cast the parent form as an object
and replace the form1, form2 ...form3456...

so i can use something like

 CType(Me.ParentForm, FormObject).control1.Text
 CType(Me.ParentForm, FormObject).grid.datasource=  somesource
 CType(Me.ParentForm, FormObject).tree.node.remove(node(1))


and so on
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ess Kay

ASKER
Awesome, youre the man