0

My first time asking a question here so apologies upfront if I'm not doing this right. I could not find anything with the search.

I inherited a VB.NET legacy Windows Application project. Any and all new additions as far as possible I would like to code in C#, including new Windows Forms. So, I have a new Windows form in a C# assembly (not sure if this is the best way) being referenced by the VB.NET Windows application. On initialising the C# form from the referenced assembly I pass it all the necessary parameters from which I will build up my grid columns and records in the C# Windows Form. No problems here so far. On the cell-double click event of the C# grid, I would like to show an existing VB.NET Winform from the application referencing the C# assembly. My question is: How do I get a handle on the VB.NET Windows Form to show it, or do anything else with it for that matter? Circular reference is obviously not allowed so how can I achieve this if at all possible, C# or otherwise?

Crono
  • 10,211
  • 6
  • 43
  • 75
  • "Get a handle" is rather unspecific. In general you just add a reference to the assembly in your project. Your compiler doesn't care in what language it was written. As long as the VB.NET project declared it *public*, you can simply use the *new* operator to create the instance and its Show() method to display it. – Hans Passant Apr 08 '14 at 12:35

2 Answers2

0

One way would be to pass a generic form creation handler to your C# Form, given that it is instantiated from your VB.NET code. This way the form wouldn't have to know what exact type of form it must create and show; all it would know is that it's a Form, which is good enough if all you have to do is showing it.

You may also consider exposing your C# Form cell double click event as a public event that can be handled by your VB.NET code.

If it makes sense in your design, a third option would be to use a third assembly that acts as a bridge. This assembly could be referred by both your VB.NET and C# assemblies and hold interface types implemented / used in these.

Crono
  • 10,211
  • 6
  • 43
  • 75
0

You should be able to add an event handler to the object.

http://msdn.microsoft.com/en-us/library/6yyk8z93%28v=vs.90%29.aspx

Sam Makin
  • 1,526
  • 8
  • 23
  • Thanks! That's effectively what I ended up doing by writing a class with a custom event triggering whenever property 'ShowForm' is true. I assign the event handler to the custom event from whichever VB.NET form I am initialising the C# assembly form. This way I can neatly manage any and all c# referenced assembly Windows forms, as well as add various other events, should I need to. – user3510816 Apr 14 '14 at 07:01