从ChildControl获取ParentClass控件 [英] Get ParentClass control from ChildControl
问题描述
大家好,
i希望从内部类中获取父类的引用,而不在名称属性中传递任何TAG或格式化索引。
父类代码如下:
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:
Public Class clsTraino
'--Grafica Manuali
Public ButtTrain As CheckBox
Public lblTrain As Label
Public PictTrain As PictureBox
End Class
我已经宣布了一系列clsTraino,例如:
I have declared an array of clsTraino, like:
Public RegVel() As clsTraino
点击ButtTrain时会发生此事件,称为'ForzaTraino':
when clicking on ButtTrain, it happens this event, called 'ForzaTraino':
AddHandler .ButtTrain.CheckStateChanged, AddressOf ForzaTraino
...
Public Sub ForzaTraino(ByVal source As Object, ByVal e As System.EventArgs)
我想将RegVel索引转换为'ForzaTraino'事件,函数检查了ButtTrain。
怎么做,不使用标签或格式化名称?
编辑:
Ralf我试着以更好的方式解释。请参阅下面的示例代码:
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:
Public Class clsTraini
Public ButtTrain As CheckBox
Public lblTrain As Label
Public PictTrain As PictureBox
End Class
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
推荐答案
我会做的以下:
将您的方法ForzaTraino中的源解析为Control。
现在您可以获得Control-Parent及其属性。
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.
dim mySender as control = source
dim mySenderName as string = mySender.Parent.toString
' or what else you want ...
嗨Matteo,
又是我了 - 我想,这个解决方案将与您的问题相匹配.. 。;)
我会做以下事情:
在类中捕获必要的事件,并在类中重建它们,类本身作为发送者。
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.
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 :
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
我希望我能帮到你...
编辑:
当然,你必须在这个类的构造函数中创建所有必要的设置...(并且不再使用form-script)
I hope that I could help you ...
Naturally you have to create all nescessary settings inside the constructor of this class ... (and not longer with the form-script)
这篇关于从ChildControl获取ParentClass控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!