|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to bind two custom controls at desing time w/o any form code?
Hello
I want to build two separate custom controls. Control A and Control B. How do I pass a reference from one control to another without having to write any code on the form? During runtime both controls will have a reference to the other control. Any help would be greatly appreciated. Thanks |
|
#2
|
|||
|
|||
|
I found a solution to my problem
I found a solution to this problem so I thought I would share it with the rest of the universe.
1.Create a new vb.net 2005 windows application project to test the custom controls. 2. add a new windows control library project. Name the control ucServer. Add a new 2nd control to this same windows control library project. Name the 2nd control ucClient. 3. add a button to both controls 4. overrite the ucServer class close with the following: '================== Option Strict Off Public Class ucServer Public Event ucServerEvent() Private Sub Uc1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try Dim x As UserControl Dim xx As ucClient = New ucClient Dim i As Integer 'look through the collection to find a ucServer control then get a reference to it For Each x In Me.ParentForm.Controls i = System.String.Compare(x.GetType().ToString(), xx.GetType().ToString()) If i = 0 Then xx = x 'pass instance of self to client xx.Server = Me End If Next Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click RaiseEvent ucServerEvent() End Sub Public Sub HandleClientString(ByVal s As String) MsgBox("got " + s) End Sub End Class '============= .5 overwrite the ucClent code with the following: Option Strict Off Public Class ucClient Public WithEvents _Server As ucServer Private Sub Uc1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try Dim x As UserControl Dim xx As ucServer = New ucServer Dim i As Integer 'look through the collection to find a ucServer control then get a reference to it For Each x In Me.ParentForm.Controls i = System.String.Compare(x.GetType().ToString(), xx.GetType().ToString()) If i = 0 Then _Server = x 'get instance of server End If Next Catch ex As Exception MsgBox(ex.Message) End Try End Sub Public WriteOnly Property Server() As ucServer Set(ByVal value As ucServer) _Server = value End Set End Property Private Sub _Server_ucServerEvent() Handles _Server.ucServerEvent MsgBox("server event", , Me.Name) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try _Server.HandleClientString(Me.Name) Catch ex As Exception End Try End Sub End Class 6. Rebuild the control project. Make a reference to the project form the windows test project to the control library project. The new controls will be available to the test project. 7. Dray one server control and multiple client controls to the test form. 8. run the test form project. Click the buttons to see the response. I hope this helps whoever is interested. Added note: I had to create an instance of the controls within the control load event. I did this in order to check for equality of types. This is causing lower performance due to garbage collection. There may be a better way to do this. Thanks |
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > How to bind two custom controls at desing time w/o any form code? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|