Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call a method from other place 1

Status
Not open for further replies.

TipGiver

Programmer
Sep 1, 2005
1,863
Hi and Merry Xmas!

It is much time that i left high level programming (i was dealing with microcontrollers and assembly) and i need help. It is simple, i know the solution which is delegates, but i cant get it work. So.

I have a usercontrol that has a button. The control is... on a form. The form has a method SetError(msg as string). When i press the button i want to call the form's SetError and pass the string argument. I use the usercontrol.parentform to get the form.

Note that: I can't use an other dll to store a "Byref" copy. The code structure is complex and causes circular depedency. Also, the SetError messes other controls so i can't just use the parentform's .Control("name") to transfer the message.

Thank you in advance.
 
Lets say it again.

The usercontrol and the form that hosts the UC are in different projects. To be more specific, they are in ClassLibrary projects.

When i press a button of the UC i want to call the form's SetError(msg) method.
 
You can't use a UC that way.

What you want to do is raise an event when the button is clicked and then capture that event in the form.

{Control}
Private Sub Button1_Click()Handles Button1.Click
RaiseEvent ButtonClicked(args as object)
End Sub

{Form}
Private Sub UserControl1_ButtonClicked(args as object)Handles UserControl1.ButtonClicked
'Do Code here
End Sub
 
Note: You need to define the event in the UC

Public Event ButtonClicked(args as Object)
 
Well what I would do is this.

Create a third vlasslibrabry with interfaces in it.

the form implements the interface with the seterror method.

the UC uses the parentform method that does a typeoff interface to see if the form implements the correct interface and then casts it to this interface and calls the seterror method on that form.

Easy as pie.

Lesson of the day: use more interfaces.

Christiaan Baes
Belgium

My Blog
 
Be careful with that solution however, if you ever decide to change the function you will have to make sure you change it in all places.

When you use a RaiseEvent, you only need the change how the data is handled when it is passed from the UC to the form thus allowing you to put it in any function you want without having the change the original UC to conform to the syntax of the function.
 
Cool,
I was trying to make it work with delegates (which i still believe that can also work) :)

Thanks Qamgine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top