Solved

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

Posted on 2013-12-09
9
427 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:Ess Kay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39707137
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

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39707156
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.
0
 
LVL 15

Author Comment

by:Ess Kay
ID: 39708659
@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
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39708698
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

0
 
LVL 15

Author Comment

by:Ess Kay
ID: 39709943
How will that return the parentform
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39709948
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.
0
 
LVL 15

Author Comment

by:Ess Kay
ID: 39709968
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
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39710072
What I am suggesting now will yield what you are after, but it will do so with just a tad more work. I showed you how to set the Text property. To do more involved controls like your Grid and your Tree, you will require an additional cast.

e.g.

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

If c IsNot Nothing AndAlso c.Length > 0 Then
    Dim gv As DataGridView = TryCast(c, DataGridView)

    If gv IsNot Nothing Then
        gv.DataSource = somesource
    End If
End If

Open in new window


You won't get the simplicity that you show in bold because you are locating controls at runtime. If you were instead doing it at compile-time--like I demonstrated with the interface--then you could have more simplified syntax.
0
 
LVL 15

Author Closing Comment

by:Ess Kay
ID: 39717249
Awesome, youre the man
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

738 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