Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Form 2 is this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim newdate As String
       newdate = TextBox1.Text
       MessageBox.Show("Date will be applied " & newdate & ".txt")
       Me.Hide()
       Form1.Show()


My form 1 is this

Dim Findstring = IO.File.ReadAllText("C:\Users\JBP-Admin\Desktop\Pugad\Mobile Support\Redmi01-JCO0451-WX_20191025.txt")
Dim Lookfor As String = Format(Now, "MM/dd/yyyy HH:mm:ss")


I just want to get the value of textbox1 in form2 to be used in this part
20191025.txt


What I have tried:

I tried adding this
Form2.TextBox1.Text = TextBox1.Text
Posted
Updated 27-Jan-20 17:37pm

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
VB
Dim mf As MyForm = New MyForm()
mf.Show()
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

The code is in C#, but it's pretty simple - and if you really can't work it out then online converters like Code Converter C# to VB and VB to C# – Telerik[^] do a pretty good job of converting between the two languages.
 
Share this answer
 
I am going to assume from reading this, that you have created both forms and control them with .show and .hide. Try this simple solution. Reverse your show and hide on form 2 and make the assignment in-between. It appears your assignment is also reversed.
to get form2.textbox1 to form1, should be
Form1.TextBox1.Text = TextBox1.Text
Yours form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim newdate As String
       newdate = TextBox1.Text
       MessageBox.Show("Date will be applied " & newdate & ".txt")
       Me.Hide()
       Form1.Show()
Try: form2
<pre>Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim newdate As String
       newdate = TextBox1.Text
       MessageBox.Show("Date will be applied " & newdate & ".txt")
       Form1.Show()
       Form1.TextBox1.Text = TextBox1.Text
       Me.Hide()


I agree with the comment on the issue with this. This was based on the supplied code. A better way would be to declare a public variable in a module. On the form2 closing event set the variable to the textbox value. Then on the form1 load event, set the textbox value to the variable.
 
Share this answer
 
v2
Comments
CHill60 28-Jan-20 3:45am    
That would imply that Form1.TextBox1 has been declared Public - bad practice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900