Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i would like to get reference of a parent class from an inner class, without passing any TAG or formatting an index in the name properties.
The parent class code is the following:
VB
Public Class clsTraino
        '--Grafica Manuali
    Public ButtTrain As CheckBox
    Public lblTrain As Label
    Public PictTrain As PictureBox
    
End Class


I have declared an array of clsTraino, like:
VB
Public RegVel() As clsTraino  

when clicking on ButtTrain, it happens this event, called 'ForzaTraino':
VB
AddHandler .ButtTrain.CheckStateChanged, AddressOf ForzaTraino
...
Public Sub ForzaTraino(ByVal source As Object, ByVal e As System.EventArgs)

I would like to get RegVel index into 'ForzaTraino' event, function of the ButtTrain checked.
How to do it, without using Tag or formatting into Name?

edit:
Ralf i try to explain in a better way. See sample code below:
VB
Public Class clsTraini
    Public ButtTrain As CheckBox
    Public lblTrain As Label
    Public PictTrain As PictureBox

End Class


VB
Public Class Form1
    Public RegVel() As clsTraini

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        ReDim RegVel(2)
        For I = 1 To RegVel.GetUpperBound(0)
            RegVel(I) = New clsTraini

            RegVel(I).ButtTrain = New CheckBox
            With RegVel(I).ButtTrain
                '-- some proprieties, TAG is used
                .Tag = 6 '---use no TAG!!!!
                .Height = 45
                .Width = 45
            End With
            AddHandler RegVel(I).ButtTrain.CheckedChanged, AddressOf ForzaTraino
        Next

    End Sub

    Public Sub ForzaTraino(ByVal source As Object, ByVal e As System.EventArgs)
        '--some code todo
        '--which 'RegVal' checkedButtTrain is member of????????


    End Sub

End Class
Posted
Updated 29-May-15 6:12am
v3
Comments
Ralf Meier 29-May-15 6:44am    
Your code is still working ?
The method "ForzaTraino" is called as you want ? You only want to know the parent from the "source" ?
Or does something in your construction-chain is not working yet ?
Member 11727760 29-May-15 6:52am    
Hello Ralf,
i would like to know the parent from the 'source'.

ButtTrain is a member of RegVel(I), initialized like RegVel(I).Buttrain; i would like to know into 'source' which RegVel (without passing any tag) or, better, get the control of RegVel. Is there a method to call?

thanks for help
Matteo M.

I would do the following :
Parse the source from your method "ForzaTraino" to Control.
Now you can get the Control-Parent and it's Properties.

VB
dim mySender as control = source
dim mySenderName as string = mySender.Parent.toString
' or what else you want ...
 
Share this answer
 
Comments
Member 11727760 29-May-15 8:52am    
Thank you Ralf, but in this solution you give me back the parent form control, a graphic object like a "groupbox".
My question is to get the control of base class "RegVel(I)", without any tag.
Can you help me?
Ralf Meier 29-May-15 11:40am    
I'm not sure if a had understand you right.
Please explain your question by an example - perhaps of the inheritance ...
Who is the parent or owner from whom ? And whom do you want to know ?
Please try to create a diagram or something like that ...
Member 11727760 29-May-15 12:13pm    
See the edit post
Matteo
Ralf Meier 29-May-15 12:30pm    
In cases like this a would give the class clsTraini a string-property 'Name'. When I create the instance I would give it a Name for further work with it.
Tomorrow I will try something with a collection but I think that you will not get the collection (or the Array) back as reflection because both are not the owner of your class. The Name-Property would be the best way to identify your instance later. Why isn't that a possible solution for you ?
Hi Matteo,

it's me again - I think, this solution will match to your problem ... ;)

I would do the following :
Catch the nescessary Events inside your class and rebuild them inside the class with the class itself as sender.

VB
Public Class clsTraini

    Public ButtTrain As CheckBox
    Public lblTrain As Label
    Public PictTrain As PictureBox

    Public Sub New()
        AddHandler ButtTrain.CheckStateChanged, AddressOf CheckBox_Checked
    End Sub

    Public Sub Dispose()
        RemoveHandler ButtTrain.CheckStateChanged, AddressOf CheckBox_Checked
    End Sub

    Private Sub CheckBox_Checked(sender As Object, e As EventArgs)
        RaiseEvent CheckedStateChanged(Me, e)
    End Sub

    Public Event CheckedStateChanged(sender As Object, e As EventArgs)

End Class


and inside your form this :
VB
Public Class Form1
    Public RegVel() As clsTraini
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        ReDim RegVel(2)
        For I = 1 To RegVel.GetUpperBound(0)
            RegVel(I) = New clsTraini
 
            RegVel(I).ButtTrain = New CheckBox
            With RegVel(I).ButtTrain
                '-- some proprieties, TAG is used
                .Tag = 6 '---use no TAG!!!!
                .Height = 45
                .Width = 45
            End With
            AddHandler RegVel(I).CheckedChanged, AddressOf ForzaTraino
        Next
 
    End Sub


I hope that I could help you ...


Edit:
Naturally you have to create all nescessary settings inside the constructor of this class ... (and not longer with the form-script)
 
Share this answer
 
v2
Comments
Member 11727760 3-Jun-15 6:36am    
Hi Ralf, thank you so much about your solution.
What about using Reflection and Assembly to find the upperlevel class index from the Button?
I have not any expierence about Assembly, can you help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900