• We just launched and are currently in beta. Join us as we build and grow the community.

Pass values between Windows Forms

jlnelmes

Play-to-Earn Innovator
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
69
Likes
64
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In my tutorial “How to Pass Value from One Form to another Form” I discussed how the other form received a value by using a Public Variable.

There are so many discussions on the Internet that argues whether Public Variable is a good programming practice compare to Property. Well, IMHO public variable is an option for simplicity. You do not need to waste line of code just to get the same result.

However, there are some certain areas that we need to consider like OOP and Security. Public Variables as the name implies is subject to vulnerabilities. Arjay_nacion has also commented to my tutorial which breaks the encapsulation principle of OOP.

Without further ado let’s get on the code.

Add two forms in your project called “Form1” and “Form2”. Add the necessary controls as shown in the following screenshot.

[inline:Pass_Value.jpg=Pass values between Windows Forms]

In Form1 add the following code:

  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. Dim

    Frm2 As

    Form2

  3. Frm2 =

    New

    Form2

  4. Frm2.

    GetFolioNumber

    =

    txtFolioNumber.

    Text

  5. Frm2.

    Show

    (

    )
  6. End

    Sub

In Form2 add the following code:

  1. Dim

    FolioNumber As

    String

  2. Public

    WriteOnly

    Property

    GetFolioNumber(

    )
  3. Set

    (

    ByVal

    value)
  4. FolioNumber =

    value
  5. End

    Set
  6. End

    Property

  7. Private

    Sub

    Form2_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  8. txtFolioNumber.

    Text

    =

    FolioNumber
  9. End

    Sub

I hope this helps.

 

452,292

323,526

323,535

Top