Referencing VB form in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    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.Wi ndows.Forms.Tex tBox 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.Wi ndows.Forms.For m 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.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Referencing VB form in C#

    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]
    - mvp@spam.guard. caspershouse.co m




    "Steve" <sgould@convans ys.com> wrote in message
    news:uHJhwPNmDH A.2080@TK2MSFTN GP10.phx.gbl...[color=blue]
    > 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[/color]
    stuff.[color=blue]
    > 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[/color]
    pass[color=blue]
    > 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[/color]
    C#[color=blue]
    > class using
    >
    >
    >
    > Public int DoAdd(System.Wi ndows.Forms.Tex tBox 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.Wi ndows.Forms.For m 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[/color]
    a[color=blue]
    > feeling it has to do with somehow getting a reference in the C# project or
    > somehow casting frmF to frmX
    >
    >
    >
    > Any suggestions?
    >
    >
    >
    > Thanks.
    >
    >[/color]


    Comment

    • Peter Rilling

      #3
      Re: Referencing VB form in C#

      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" <sgould@convans ys.com> wrote in message
      news:uHJhwPNmDH A.2080@TK2MSFTN GP10.phx.gbl...[color=blue]
      > 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[/color]
      stuff.[color=blue]
      > 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[/color]
      pass[color=blue]
      > 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[/color]
      C#[color=blue]
      > class using
      >
      >
      >
      > Public int DoAdd(System.Wi ndows.Forms.Tex tBox 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.Wi ndows.Forms.For m 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[/color]
      a[color=blue]
      > feeling it has to do with somehow getting a reference in the C# project or
      > somehow casting frmF to frmX
      >
      >
      >
      > Any suggestions?
      >
      >
      >
      > Thanks.
      >
      >[/color]


      Comment

      Working...