473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Referencing VB form in C#

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.
Nov 20 '05 #1
2 2884
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/eaaCatal...Interface.html

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

Hope this helps
Jay

"Steve" <sg****@convansys.com> wrote in message
news:e0**************@tk2msftngp13.phx.gbl...
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.

Nov 20 '05 #2
* "Steve" <sg****@convansys.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a main form with a continuous subform. On the main form I have a text box that references a field on the subform. What I'd like it to do is show the value...
6
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if...
4
by: mplogue | last post by:
I have a form (frmMain) with a subform (frmSub), each with enumerated fields of the same name (txt1, txt2, etc). I'm trying to make a function that will take the values for each field in frmMain,...
17
by: Paul Helmuth | last post by:
All, (here's an easy one)... This is probably a stupid question - please bare with me as I am new to dotNet. How does one reference objects on a form from a module? In 6.0 you could simply...
2
by: Axel | last post by:
Hi, a question about something that seems very simple at first glance: is it possible to reference other controls of a subform in a query window without referencing through the parent form? I...
9
by: Alan | last post by:
Hmmm, I'm not too good with the syntax of referencing a subreport. I have frmInvoice which has the invoice details (e.g. ProductCode, ProductCost etc) in the subform frmInvoiceDetails. I'm trying...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
2
by: ccsnavy | last post by:
For some reason referencing an unbound control on an active form from a query has ceased to work correctly. While other previously existing references to unbound controls in the same form seem to...
11
by: ozTinker | last post by:
I'm sure this shouldn't be too difficult, but I lack familiarity with the MS object model. Suppose I have a table "Purchase_Orders" and a form "TEMP" which I am using to look up a customer's...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.