How to pass reference of control to a property?

I have a customer control and a datagridview control in one of my windows form. What do I want is to and a property in my customer control and refer to the datagridview, just like refer a contextmenu to a datagridview. then in my customer control I want to access that datagridview. my code is like below, but it doesn't work:

<code>

class windowform

Me.UsCtrl.uDataGridView =Me.myDataGridView

end class

class UsCtrl

private v_rdv as windows.Forms.DataGridView

PublicProperty uDataGridView ()Aswindows.Forms.DataGridView

Get

Return v_rdv

EndGet

Set(ByVal valueAs windows.Forms.DataGridView )

v_rdv= value

EndSet

EndProperty

</code>

Please help me to figure it out. Thanks in advance.

[1891 byte] By [Dongwei] at [2007-12-23]
# 1

The code seems fine, works at the design time too... Could you post some more details about the problem?

Andrej

AndrejTozon at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Yes, you are right. my code is like this. now I found the problem is when myuc initial, it will run uGetFlexGrid() first, and the value of v_rfg is nothing, and then v_rfg can get the right value.

public class form1

dim myuc as new uctrls
myuc.uFlexGrid=_flexGrid

end class

Public Class uctrls
Private v_sFilter As String
Private v_rfg As C1.Win.C1FlexGrid.C1FlexGrid

Public Property uFlexGrid() As C1.Win.C1FlexGrid.C1FlexGrid
Get
Return v_rfg
End Get
Set(ByVal value As C1.Win.C1FlexGrid.C1FlexGrid)
v_rfg = value
End Set
End Property

Public Sub New()
InitializeComponent()
uGetFlexGrid()
End Sub

Private Function uGetFlexGrid() As Boolean
If v_rfg IsNot Nothing Then
For Each col As C1.Win.C1FlexGrid.Column In v_rfg.Cols
If col.Caption <> "" Then
Console.WriteLine(col.Caption)
End If
Next
End If
End Function
End Class

Any good idea, pls?

Dongwei at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Yes, you're right, this wouldn't work.... I'd say you have two options... Add a new constructor to your control and pass your control as a parameter:

Public Sub New(ByVal grid As C1.Win.C1FlexGrid.C1FlexGrid)
InitializeComponent()
uFlexGrid = Grid
uGetFlexGrid()
End Sub

... and call it like:

Dim myuc as new uctrls(_flexGrid)

or...

remove uGetFlexGrid() call from the constructor (New()) and expose uGetFlexGrid() function as public:

Public Function uGetFlexGrid() As Boolean
...
End Function

... and call it like:

Dim myuc as new uctrls
myuc.uFlexGrid=_flexGrid
myuc.uGetFlexGrid()

Andrej

AndrejTozon at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
thanks, excellent. But, what I do is to build a customer control to get the caption of the FlexGrid. I want to drag this control to every form and set the uFlexGrid property to related FlexGrid by using Properties Windows of this control. Just like we set ContextMenuStrip to FlexGrid or DataGridView. Do you have any good idea ? please help me. Thanks in advance.
Dongwei at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

I guess you could also call uGetFlexGrid function in the property setter:

Public Property uFlexGrid() As C1.Win.C1FlexGrid.C1FlexGrid
Get
Return v_rfg
End Get
Set(ByVal value As C1.Win.C1FlexGrid.C1FlexGrid)
v_rfg = value
uGetFlexGrid()
End Set
End Property

Andrej

AndrejTozon at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
Yes, It works. Thank you
Dongwei at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...