ranadev
ranadev

Reputation: 23

send data from form 1 to form 2

I have made 2 variables:

Public a as Integer
Public b as Integer

and I have a text field where 2 values can be inserted like this: "1 - 10" so I split the value and saved them in those 2 variables a and b.

but I need those 2 values in different forms but all I'm getting is 0.

I also created Dim c as form1 = new form1 in the form2.

What's the problem?

Upvotes: 0

Views: 895

Answers (3)

Thromet
Thromet

Reputation: 35

Well, i am not sure what it exactly is you want but here are some suggestions:

if your form1 contains two variables like so:

Public a As Integer = 0
Public b As Integer = 0

and you want to retrieve the value of one of those variables from another form, lets say from Form2, then all you need to do is that:

(Put this code in either a function, or an event of your second Form (for example Form2)):

dim current_a As Integer = 0
current_a = Form1.a

dim current_b As Integer = 0
current_b = Form1.b

If you enter the text (for example: "1 - 10“), into a textbox, but want to get both values (in this case 1, and 10), you only need to grab them by for example creating a loop, to search for these values, like so:

dim textbox_str As String = ""
textbox_str = TextBox1.Text

( in this example, the TextBox1.Text would contain "1 - 10")

dim final_str1 As String = ""
dim final_str2 As String = ""

for each s_ As String In textbox_str
      If Not s_ = " " Then
          final_str1 = final_str1 & s_
      Else
          Exit For
      End If
Next

(after this, final_str1 would contain "1") (now extract the second number, by doing the following:)

final_str2 = textbox_str.Replace(final_str1 & " - ", "")

(and now, final_str2 would contain "10") (so you would have both numbers extracted from that text)

As i said, i do not exactly know what you want, but hopefully this helped you, let me know if it is what you were looking for!

Upvotes: 0

RicRev
RicRev

Reputation: 29

You could create a module and define the variable as puclic there, that way they'll always be accessible from anywhere on the application.

Right click the solution, Add New, Module

And inside that module add

Public a, b as Integer

To split the textbox input and store them in these vars use

Dim s as String
Dim sp() as String
s = TextBox1.Text
sp = s.Split("-")

This stores the values in the textbox separated by '-' on sp(0) and sp(1) respectively.

You can then stote them into you public variables.

a = sp(0)
b = sp(1)

And call then from any form you need.

Upvotes: 0

Oak_3260548
Oak_3260548

Reputation: 2010

Depending on a relation between Form1 and Form2, you can use this kind of communication (this is for a parent-child relation):

Form 2:

Public ParentFrm as Form1
Public a as Int16

Form 1 - in it's running instance:

Public b as Int16
Dim NewInstanceOfForm2 as New Form2
NewInstanceOfForm2.ParentFrm = Me    ' this is to ensure you can talk back to correct instance of parent form
NewInstanceOfForm2.a = 12345
NewInstanceOfForm2.BackColor = colors.Pink
NewInstanceOfForm2.TextBox1.Text = "Hello World!!!"

Since we set the ParentFrm in the Form2, you can communicate the same way back and set things in Form1:

ParentFrm.b = 789
ParentFrm.TextBox3 = "Hi there!!!"
ParentFrm.UpdateForm1FromDatabasePublicFunction()

Siblings can communicate through a common parent. But in all the cases, you need to get to understand instances of the forms. Remember, that you can communicate only with objects (TextBox, Button, DataGridView,...) and Public variables of the form.

Upvotes: 1

Related Questions