473,405 Members | 2,445 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,405 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 15 '05 #1
2 1559
Steve,

The reason this doesn't work is that you can not have circular
references between projects (nor should you). To get around this, you are
going to have to create an interface that represents the form and the
properties you want to expose on the form. This would have to be defined in
another assembly, and referenced by both the VB and C# assemblies. The VB
project would need it to know what to implement, and the C# project would
need it to know what to be passed (instead of the form instance, you get the
implementation of the interface). Then, the C# class would take this
interface as a parameter.

Also, one would question the design of passing the form instance to the
C# object. If you are going to pass specific custom UI elements (the custom
form) to an engine for processing, then you might as well include all of the
code in one project.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Steve" <sg****@convansys.com> wrote in message
news:uH**************@TK2MSFTNGP10.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 15 '05 #2
By default, form controls are not public. Check the visibility for the
controls and make sure they are publicly accessible. Controls on a form are
no different then any other method or property on any other class.

"Steve" <sg****@convansys.com> wrote in message
news:uH**************@TK2MSFTNGP10.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 15 '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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.