none
How to pass reference of control to a property?

    Question

  • 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

    Public Property uDataGridView () As windows.Forms.DataGridView

    Get

    Return v_rdv

    End Get

    Set(ByVal value As windows.Forms.DataGridView )

    v_rdv= value

    End Set

    End Property

    </code> 

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

    Monday, August 21, 2006 11:01 PM

All replies

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

    Andrej

    Tuesday, August 22, 2006 6:02 AM
  • 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?

    Tuesday, August 22, 2006 2:20 PM
  • 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

    Tuesday, August 22, 2006 2:28 PM
  • 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.
    Tuesday, August 22, 2006 3:23 PM
  • 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

    Tuesday, August 22, 2006 3:30 PM
  • Yes, It works. Thank you
    Thursday, August 24, 2006 3:29 PM