Referencing VB form in C#

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I have a .NET windows program that has a VB.NET component and a C#
component. The VB component contains the form and handles all the UI stuff.
The C# component drives the engine that actually does all the work.



The VB component has one form called frmX. On frmX are three text boxes
(txtA, txtB, txtC) and a button (cmdAdd). When the button is clicked, the
program is supposed to add the values in txtA and txtB and display it in
txtC.



The cmdAdd_Click event instantiates a C# object called Add. I want to pass
in frmX and have the C# object extract the values in txtA and txtB and do
the calculation.



If I pass in just the text boxes then I can declare the parameters in the C#
class using



Public int DoAdd(System.Windows.Forms.TextBox txtA,

System.Windows.Forms.TextBox txtB)



But that is not what I want to do. Obviously this is only a test case and
the actually program needs to access all the elements on the form.



The question I have is, how do I pass in frmX and have access to all the
elements (text boxes, drop downs, etc.)? If I declare the parameter in C#
as



Public int DoAdd(System.Windows.Forms.Form frmF)



I have access to all the properties and method of a form, but not the
specific elements in frmX. The C# method has no clue about frmX. I have a
feeling it has to do with somehow getting a reference in the C# project or
somehow casting frmF to frmX



Any suggestions?



Thanks.
 
Steve,
If I follow you. You have a project VB that has a form that creates a class
from project C, where the class from project C needs access to the form in
project VB?

Which is clearly a circular project problem. VS.NET does not allow circular
project references.

The easiest way to avoid this is to apply the Separated Interface Pattern.

In project C I would define an interface that the Form in Project VB needs
to implement. This interface would have read-only properties for the values
needed from the form. The Form would then implement the interface. The
DoAdd method's parameter would be this interface. Project C is then
standalone, while Project VB needs to reference Project C.

Something like:

// in C#
public interface IFormX
{
// these are string to avoid coupling, if you need full TextBoxes,
// you can define them as TextBox.
string TextA { get; }
string TextB { get; }
string TextC { get; }
}

public class Class1
{
public int DoAdd(IFormX formX)
{
}
}

' in VB.NET
Public Class XForm
Inherits Form
Implements IFormX

' designer code omitted

Public Readonly Property TextA() As String Implements IFormX.TextA
Get
Return txtA.Text
End Get
End Property

...

End Class

For details on the Separated Interface Pattern see:
http://www.martinfowler.com/eaaCatalog/separatedInterface.html

For the complete explanation you need Martin Fowler's book "Patterns of
Enterprise Application Architecture" from Addison Wesley.

Hope this helps
Jay
 
* "Steve said:
I have a .NET windows program that has a VB.NET component and a C#
component. The VB component contains the form and handles all the UI stuff.
The C# component drives the engine that actually does all the work.

The VB component has one form called frmX. On frmX are three text boxes
(txtA, txtB, txtC) and a button (cmdAdd). When the button is clicked, the
program is supposed to add the values in txtA and txtB and display it in
txtC.

The cmdAdd_Click event instantiates a C# object called Add. I want to pass
in frmX and have the C# object extract the values in txtA and txtB and do
the calculation.

If I pass in just the text boxes then I can declare the parameters in the C#
class using

Public int DoAdd(System.Windows.Forms.TextBox txtA,

System.Windows.Forms.TextBox txtB)

But that is not what I want to do. Obviously this is only a test case and
the actually program needs to access all the elements on the form.

The question I have is, how do I pass in frmX and have access to all the
elements (text boxes, drop downs, etc.)? If I declare the parameter in C#
as

Public int DoAdd(System.Windows.Forms.Form frmF)

I have access to all the properties and method of a form, but not the
specific elements in frmX. The C# method has no clue about frmX. I have a
feeling it has to do with somehow getting a reference in the C# project or
somehow casting frmF to frmX

You will have to reference the VB.NET class library from the C#ä project
and declare the prameter 'frmF' as 'MyNamespace.frmX', assuming
'MyNamespace' is the root namespace of the VB.NET class library that
contains the definition of the form class.
 
Back
Top