0

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?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
ranadev
  • 23
  • 1
  • 7
  • `Dim c as form1 = new form1` creates a ***new*** instance as the keyword implies. it wont be the same form instance which might be showing – Ňɏssa Pøngjǣrdenlarp Aug 02 '17 at 16:27
  • you mean like this c.a and c.b ?? @Plutonix – ranadev Aug 02 '17 at 16:28
  • you asked `whats the problem` the problem is that you created a ***new*** form1 instance and those variables are likely to be 0 in a new form instance. After you create the new form if you did `c.Show` you would see a new copy of your form display – Ňɏssa Pøngjǣrdenlarp Aug 02 '17 at 16:33
  • ohh yeah, ok so how can I get the data from form1 to form 2?? @Plutonix – ranadev Aug 02 '17 at 16:35
  • 1
    This has been asked thousands of times. Type your title into Google, maybe add "VB" to the search box (over 6000 posts), then do common ordinary research to work out which method is best for your case. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Aug 02 '17 at 16:40
  • Make them `Public Shared` or just `Shared`. – RBarryYoung Aug 02 '17 at 17:03
  • Possible duplicate of [Is there way how to pass values between forms in vb.net? Without using public variable](https://stackoverflow.com/questions/18966080/is-there-way-how-to-pass-values-between-forms-in-vb-net-without-using-public-va) this has been asked here many times as @Plutonix already has mentioned. – Trevor Aug 02 '17 at 19:29
  • Possible duplicate of [How to pass value from Form1 to Form2](https://stackoverflow.com/questions/42903216/how-to-pass-value-from-form1-to-form2) – Bugs Aug 04 '17 at 10:46
  • That's two dupes for you to look at now :) – Bugs Aug 04 '17 at 10:46

3 Answers3

1

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.

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • That's a great idea! I may use something like this if I ever need multiple open forms to communicate to the original parent. – pianocomposer Oct 06 '21 at 17:51
0

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!

Thromet
  • 35
  • 8
0

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.

RicRev
  • 29
  • 1
  • 1
  • 8