I have 2 forms shown below.
Form1 has a button that opens Form2.
Form2 has a button that (I wish) puts text in a textbox on Form1

This is about as bare bones as it gets. No one I've talked to knows what to do. It appears that everytime you click the button on Form2 it creates another form. This can't be that difficult, I must be missing something simple. I can do this in VB6.

'==============
' FORM 1
'==============
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.ShowDialog()

End Sub
End Class
'==============
' FORM 2
'==============

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm1 As New Form1
frm1.TextBox1.Text = " TEST "
End Sub
End Class

Recommended Answers

All 6 Replies

I have 2 forms shown below.
Form1 has a button that opens Form2.
Form2 has a button that (I wish) puts text in a textbox on Form1

This is about as bare bones as it gets. No one I've talked to knows what to do. It appears that everytime you click the button on Form2 it creates another form. This can't be that difficult, I must be missing something simple. I can do this in VB6.

'==============
' FORM 1
'==============
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.ShowDialog()

End Sub
End Class
'==============
' FORM 2
'==============

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm1 As New Form1
frm1.TextBox1.Text = " TEST "
End Sub
End Class

It looks to me the reason your getting a new form is you are creating a new on, in your dim statement, frm1 as new form1. should it not be

form1.testbox1.text = "test"

as the only on_click statement? You shouldnt have to dim the form it is already active, and your are just supplying a value to a control with in a live form. Just my thoughts.

Hi,


'==============
' FORM 1
'==============
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.ShowForm(me)

End Sub
End Class
'==============
' FORM 2
'==============

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
Dim temForm1 as Form1
Public function showform(frm as form1)
temForm1=frm
me.show()
end function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
temForm1.textbox1.text=textbox1.text
End Sub

Hope this works for you!.
Bye
Jeeva


You can do this by

I have 2 forms shown below.
Form1 has a button that opens Form2.
Form2 has a button that (I wish) puts text in a textbox on Form1

This is about as bare bones as it gets. No one I've talked to knows what to do. It appears that everytime you click the button on Form2 it creates another form. This can't be that difficult, I must be missing something simple. I can do this in VB6.

'==============
' FORM 1
'==============
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.ShowDialog()

End Sub
End Class
'==============
' FORM 2
'==============

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm1 As New Form1
frm1.TextBox1.Text = " TEST "
End Sub
End Class

i think it's the "new" word in your code seems to be the problem..
because using "new" create a new form of that you specify..
maybe just use :

----_click() (in frm1)

frm2.show
frm1.hide


then in frm2 click event you can add code to transfer text from frm2 to frm1 like:

-----_clik()

frm1.txt1.text=frm2txt1.text

pls try it and seee if it works...

Hi.

I would like to know how to pass variable to form before creation of this form. I want to pass object which represents important data and then to perform some actions on form with this object. So I want to have form for user and for visual purpose no for variables and performing actions. For this purpose I have mentioned object.

I dont know how to pass variable to form which is Startup form. I want to have SDI application and this form must be as Startu form. One solution is to create second from, set it as Startup form and call my first form.

In C# it is simple - there is Program.cs and Application.Run so I can call Application.Run(new frmC_Main());

But VB? There are Application.myapp and Application.Designer.vb files, but how to call something similar to C#?

Thx in advance.

...MUDO...

what nikie said is correct, in vb you do not need to declare forms, they are already recognized by the program.

Under Form 1 - Button 1.Click
Form2.show()

Under Form 2 - Button 1.Click
Form1.TextBox1.Text = "Test"

I figured this out in MS Visual Basic Express 2008 the code shouldn't be too different and upgrading isn't hard. I know this post is old, but I used a few hints here to work this out as well. I wanted to be able to send data from one form to another and have it so I can repurpose it elsewhere in may application. So it had to be universal. This is based on string text but with a little modification you can send any type of data this way. If you use similar code to what is below then it should work for you.

FORM 1 (This form has a text box and a button on it the button opens up FORM 2 and passes a reference to the textbox when clicked. You technically don't need "txtCalledControl" but I use it so I can also pass additional information to FORM 2 with using multiple variables.)
==============
Public Class frmMainMenu
Public txtCalledControl As TextBox

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frmKeyboard.Show()
frmKeyboard.txtRefControl = txtCalledControl
End Sub
.
.
.
End Class

FORM 2 (This form has several controls on it in my own personal version, but the most important for this example is another textbox and a button. When the button is clicked it takes the text inside of the textbox and sets it as the text for the textbox on FORM 1.)
=============
Public Class frmKeyboard
Public txtRefControl As TextBox

Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
txtRefControl.Text = txtWrite.Text
Me.Close()
End Sub
.
.
.
End Class


The two public variables I have are the bonds that link these two forms together. They have to be set to public or neither will know either of them exist. You can create multiple forms that call the same keyboard form and will not care which form called it to open up. This is because when I pass it the textbox control that is stored in the variable "txtRefControl" it knows which form it also belongs too as well.

The other thing you have make sure of is after the second form is opened you tell it which textbox control you want to deal with. So it goes hand-in-hand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.