.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 13th, 2007, 11:14 AM
esmithz esmithz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 5 esmithz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 13 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old December 14th, 2007, 09:58 AM
esmithz esmithz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 5 esmithz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 13 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > How to bind two custom controls at desing time w/o any form code?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT