Solved

Add text to Second form

Posted on 2004-10-15
3
304 Views
Last Modified: 2010-04-23
How can I add text to another form without re-showing it?
I can do it if I use the following code


Dim t As New Form2
        t.TextBox1.Text = "test"
t.show


But the form is already open so I don’t want to reshow it

I have tried

Dim t As New Form2
        t.TextBox1.Text = "test"
me.close

With no success
0
Comment
Question by:davidrichardson
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
gdexter earned 125 total points
ID: 12322903
You can not do this by creating a new instance of Form2. You need to pass a reference to the current instance of Form2 to the other form through a property.

Does Form2 open the active form that needs to adjust the text?

If you are not using MDI you can do something like this:

Public Class OtherForm
   'declare a reference variable
   private _Form2 as Form2
 
   Public WriteOnly Property MyForm2() as Form2
      Set(ByVal Value as Form2)
         Me._Form2 = Value
      End Set
   End Property

   Public Sub WriteToForm()
      Me._Form2.TextBox1.text = "test"
   End Sub

End Class

'When you open the OtherForm object from Form2 you pass a refence to itself
'In Form2

Public Sub LoadOtherForm
   
   Dim frm as New OtherForm
   frm.MyForm2 = Me
   frm.Show

End Sub

If you are using MDI this functionality is provided through the 'MDIParent' Property


 


   
 






0
 
LVL 7

Author Comment

by:davidrichardson
ID: 12323162
form 2 does open the active form, when i run WriteToForm() i get the following error

Object reference not set to an instance of an object

On line Me.frmscaninput.TextBox1.Text = "test"

Copy of full code used below

    Private frmscaninput As frmscaninput

    Public WriteOnly Property MyForm2() As frmscaninput
        Set(ByVal Value As frmscaninput)
            Me.frmscaninput = Value
        End Set
    End Property

    Public Sub WriteToForm()
        Me.frmscaninput.TextBox1.Text = "test"
    End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        WriteToForm()

    End Sub
0
 
LVL 7

Author Comment

by:davidrichardson
ID: 12323300
my mistake i didnt as the load form cade properly

Thanks

Dave
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question