Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winform (vb.net) with a usercontrol (Elememthost) & I need to run the Public sub on the form (With values). After looking for what seem to be forever I am confused as some say you should not do this (Bad structure) & some say it is possible but I have tried everthing without getting anywhere. I this something I should not be doing or there a correct way.

I am new wpf & am sinking fast.

Sorry

What I have tried:

Window.GetWindow(Me)
Posted
Updated 25-Nov-19 10:08am

In general you should not mix Winforms with WPF unless you have no other options.
If you must, here's an article about it: Mixing WPF and WinForms - Simple Talk[^]

And here is a walkthrough: Walkthrough: Hosting a WPF Composite Control in Windows Forms | Microsoft Docs[^]
It's in C#, but you can convert it to VB.NET with this online converter: Convert VB.NET to/from C# online - Roslyn Code Converter[^]
 
Share this answer
 
v2
Comments
Knight school 25-Nov-19 15:24pm    
Thanks,
I have no option as my self trained skills are based around Vb.net & I need the 3d capabilities of WPF (steep learning curve & which means c# which I have learnt over time to convert but not write). As for the article I had come across this on my travels but it did not seem to point to a solution or am I misunderstanding it?
UserControls should NEVER call a method on the form that hosts it. Doing that permanently binds the control to only ever being used on that one form and that one form only.

That pretty much defeats the purpose of creating a control - re-usability.

What you should be doing is creating an event in the control that the form can subscribe to. It'll be up to the form to decide what to do with the event, including calling whatever code the form decides needs to be called.
 
Share this answer
 
Comments
Knight school 26-Nov-19 15:01pm    
Thanks.
That's a much clearer explanation of what I have been trying to understand from many days searching. The only question is how (possible some code as a pointer)?

Many Thanks
Knight school 28-Nov-19 15:16pm    
Sorry for the delay in getting back (I have been away). Thanks for link when I am back I will look into it. Have you got a working snippet you would be able pass on (It may save me time & help me understand it better (WPF is a steep learning curve from Winform))?
Thanks
Dave Kreskowiak 28-Nov-19 15:44pm    
No, I don't.
Knight school 3-Dec-19 15:32pm    
This must be beyond my understanding.

The Usercontrol needs to crete a RoutedEventArgs
So:-
Public Event CustomClick As RoutedEventHandler

With
RaiseEvent CustomClick(Me, New RoutedEventArgs())' to raise the event

And the the main form require Event to receive the event
main Form:-
Private Sub OnCustomButtonClick(ByVal sender As Object, ByVal e As Windows.RoutedEventArgs)
MsgBox("Got It")
End Sub


But nothing. Any pointers?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900