• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Sharing Text Box across forms

I have found several examples to pass either a datavalue from a grid to another form or a textbox on form A to textbox on form B including the friends with statement and declaring it in a module; I have just not been able to get any of these examples to work for me.

Could someone share code for the best practices way of doing this ?

Thank you for your help
  • 5
  • 5
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net?

Do you only have ONE instance of each form?  or many?

Are you using standard forms?  Or an MDI model?
sny23vpbAuthor Commented:
standard forms. only one instance; a customer lookup form needs to pass information to a sales entry form. Visual Studio 2003. Thanks
Mike TomlinsonMiddle School Assistant TeacherCommented:
Conisder using a Singleton approach to access your forms.

See my discussion and examples here:

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

sny23vpbAuthor Commented:
Thank you.
sny23vpbAuthor Commented:
I tried the singleton approach and that works well; but is there anyway to do it without opening another instance of the form ?
For example: My application is a sales order form; its is open; then it opens a customer entry form; the customer entry form needs to pass information back to the sales entry form before the cust form is closed. But the sales order form is already open and I can't really open it again.

Thanks for your help.
Mike TomlinsonMiddle School Assistant TeacherCommented:
The whole point of the singleton approach is that you only have one instance of the form open and you can access it from anywhere...

Did you use the singleton approach with only the customer entry form?  Make the sales order form singleton as well and then you will be able to access it from the customer entry form without making another instance.
sny23vpbAuthor Commented:
ok. thanks.
I actually only put the primary code in my form called enterorders - and I used the form5 terminology for my examples also for simplicity (from http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21845440.html) except where I had to change the form name.

Under EnterOrders: I have
Public Shared ReadOnly Property Instance() As enterorders
            If enterorders.Form5Instance Is Nothing Then
                enterorders.Form5Instance = New enterorders
            End If
            Return enterorders.Form5Instance
        End Get
    End Property

    Private Shared Sub Form5Instance_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form5Instance.Closed
        enterorders.Form5Instance = Nothing
    End Sub

This form opens assigns order number 1716 and you click find customer which opens the customer form search on your customer click accept and the code under the accept button is

    Private Sub btnOK_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnOK.Click
        Dim f5 As enterorders = enterorders.Instance

        '        MsgBox(gcustlookup.Item(gcustlookup.CurrentRowIndex, 0))
        f5.customerID.Text = gcustlookup.Item(gcustlookup.CurrentRowIndex, 0)


    End Sub
However; the code above which is in the accept button simply opens another enterorders form incrementing the order number to 1717 - but it does correctly bring over the customerID from the customer form.

The only part I had difficulty implementing and perhaps this is the problem is the following code in that example

Public Class Form5
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Private Sub New()                           ' <----------------     Notice I change this from "Public" to "Private"!!!!

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        ' ... rest of code ...
I could not find a region section under this particular form; I have it in my project on another form. Perhaps I need to figure out how to add this part but I see it should be windows generated code.

Thanks for your help !
I'm increasing the points since your spending so much time assisting me.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Changing the New() constructor from Public to Private isn't that crucial.  It just prevents you from making another instance like this:

    Dim f As New FormXXX

With it set to Private it ensures you can only access it thru the Shared singleton instance.
sny23vpbAuthor Commented:
Anything I can try to prevent the additional instance of the form from being created ?
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you're using VB.Net 2003 then you should have the "Windows Form Designer generated code" Region.

(VB.Net 2005 puts this auto generated stuff in another file)

You said:

    "I could not find a region section under this particular form; I have it in my project on another form. Perhaps I need to figure out how to add this part but I see it should be windows generated code."

So you have two forms in the same file?  Make each form its own file so that the IDE can deal with each properly.  You may have to add a new form to your project and then copy your exising code over to it...

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now