JayBeeOH

Pass Data Demo

Jul 4th, 2016
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.85 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Data Passing Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   04 JUL 2016
  16. '
  17. ' Description:    Program shows various ways to pass data between Windows Forms.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/Gyjwh9R
  21. '                   App's Visual Basic .NET code is at http://pastebin.com/cCy4hhQg
  22. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  23. '-------------------------------------------------------------------------------------------
  24.  
  25. Public Class MainForm
  26.  
  27.     Private Sub PassByGobalsButton_Click(sender As Object, e As EventArgs) _
  28.         Handles PassByGobalsButton.Click
  29.  
  30.         ' Assign textbox values to global variables.
  31.         gDataOne = LineOneTextBox.Text
  32.         gDataTwo = LineTwoTextBox.Text
  33.  
  34.         Dim frm As New GlobalsForm
  35.         frm.Show()
  36.     End Sub
  37.  
  38.     Private Sub PassByConstructorButton_Click(sender As Object, e As EventArgs) _
  39.         Handles PassByConstructorButton.Click
  40.  
  41.         ' Pass textbox values in form's constructor.
  42.         Dim frm As New ConstructorForm(LineOneTextBox.Text, LineTwoTextBox.Text)
  43.         frm.Show()
  44.     End Sub
  45.  
  46.     Private Sub PassByPropertiesButton_Click(sender As Object, e As EventArgs) _
  47.         Handles PassByPropertiesButton.Click
  48.  
  49.         ' Assign textbox values to form's properties.
  50.         Dim frm As New PropertyForm
  51.         frm.DataOne = LineOneTextBox.Text
  52.         frm.DataTwo = LineTwoTextBox.Text
  53.         frm.Show()
  54.     End Sub
  55.  
  56.     Private Sub PassByDialogButton_Click(sender As Object, e As EventArgs) _
  57.         Handles PassByDialogButton.Click
  58.  
  59.         ' Assign textbox values to form's properties.
  60.         ' However, update textboxes only if OK button clicked.
  61.         Dim frm As New DialogForm
  62.         frm.DataOne = LineOneTextBox.Text
  63.         frm.DataTwo = LineTwoTextBox.Text
  64.  
  65.         If frm.ShowDialog = DialogResult.OK Then
  66.             LineOneTextBox.Text = frm.DataOne
  67.             LineTwoTextBox.Text = frm.DataTwo
  68.         End If
  69.     End Sub
  70.  
  71.     Private Sub PassByTagButton_Click(sender As Object, e As EventArgs) _
  72.         Handles PassByTagButton.Click
  73.  
  74.         ' Instantiate a new Contact and assign textbox values.
  75.         Dim aContact As New Contact With {
  76.             .FirstName = LineOneTextBox.Text,
  77.             .LastName = LineTwoTextBox.Text}
  78.  
  79.         ' Assign data to form's tag property.
  80.         Dim frm As New TagForm
  81.         frm.Tag = aContact
  82.         frm.Show()
  83.     End Sub
  84.  
  85.     Private Sub PassByTag2WayButton_Click(sender As Object, e As EventArgs) _
  86.         Handles PassByTag2WayButton.Click
  87.  
  88.         ' Instantiate a new Contact and assign textbox values.
  89.         Dim aContact As New Contact With {
  90.             .FirstName = LineOneTextBox.Text,
  91.             .LastName = LineTwoTextBox.Text}
  92.  
  93.         ' Assign data to form's tag property and show modal.
  94.         Dim frm As New TagForm
  95.         frm.Tag = aContact
  96.         frm.ShowDialog()
  97.  
  98.         ' Cast from object to contact datatype and assign
  99.         ' data to textboxes.
  100.         aContact = CType(frm.Tag, Contact)
  101.         LineOneTextBox.Text = aContact.FirstName
  102.         LineTwoTextBox.Text = aContact.LastName
  103.     End Sub
  104. End Class
  105.  
  106. '-------------------------------------------------------------------------------------------
  107. Public Class GlobalsForm
  108.     Private Sub GlobalsForm_Load(sender As Object, e As EventArgs) _
  109.         Handles MyBase.Load
  110.  
  111.         ' Load Data to TextBoxes from global variables.
  112.         LineOneTextBox.Text = gDataOne
  113.         LineTwoTextBox.Text = gDataTwo
  114.     End Sub
  115. End Class
  116.  
  117. '-------------------------------------------------------------------------------------------
  118. Module GlobalVariables
  119.     Public gDataOne As String
  120.     Public gDataTwo As String
  121. End Module
  122.  
  123. '-------------------------------------------------------------------------------------------
  124. Public Class ConstructorForm
  125.  
  126.     ' Default Constructor.
  127.     Public Sub New()
  128.  
  129.         MyBase.New      ' In vb.net if omitted, implicitly called.
  130.  
  131.         ' This call is required by the designer.
  132.         InitializeComponent()
  133.  
  134.         ' Add any initialization after the InitializeComponent() call.
  135.  
  136.     End Sub
  137.  
  138.     ' Parameterized Constructor.
  139.     Public Sub New(ByVal dataOne As String, dataTwo As String)
  140.  
  141.         MyBase.New      ' In vb.net if omitted, implicitly called.
  142.  
  143.         InitializeComponent()
  144.  
  145.         ' Assign parameter data passed to textboxes.
  146.         LineOneTextBox.Text = dataOne
  147.         LineTwoTextBox.Text = dataTwo
  148.     End Sub
  149. End Class
  150.  
  151. '-------------------------------------------------------------------------------------------
  152. Public Class PropertyForm
  153.  
  154.     Public Property DataOne As String
  155.     Public Property DataTwo As String
  156.  
  157.     Private Sub PropertyForm_Load(sender As Object, e As EventArgs) _
  158.         Handles MyBase.Load
  159.  
  160.         ' Load Data to TextBoxes.
  161.         LineOneTextBox.Text = DataOne
  162.         LineTwoTextBox.Text = DataTwo
  163.     End Sub
  164. End Class
  165.  
  166. '-------------------------------------------------------------------------------------------
  167. Public Class DialogForm
  168.  
  169.     Public Property DataOne As String
  170.     Public Property DataTwo As String
  171.  
  172.     Private Sub DialogForm_Load(sender As Object, e As EventArgs) _
  173.         Handles MyBase.Load
  174.  
  175.         ' Load Data to TextBoxes.
  176.         LineOneTextBox.Text = DataOne
  177.         LineTwoTextBox.Text = DataTwo
  178.     End Sub
  179.  
  180.     Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  181.         Handles OK_Button.Click
  182.  
  183.         ' Return Data
  184.         DataOne = LineOneTextBox.Text
  185.         DataTwo = LineTwoTextBox.Text
  186.         Me.DialogResult = System.Windows.Forms.DialogResult.OK
  187.         Me.Close()
  188.     End Sub
  189.  
  190.     Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  191.         Handles Cancel_Button.Click
  192.  
  193.         Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
  194.         Me.Close()
  195.     End Sub
  196. End Class
  197.  
  198. '-------------------------------------------------------------------------------------------
  199. Public Class TagForm
  200.  
  201.     Private Sub TagForm_Load(sender As Object, e As EventArgs) _
  202.         Handles MyBase.Load
  203.  
  204.         ' Cast from object to contact datatype.
  205.         Dim aContact As Contact = CType(Me.Tag, Contact)
  206.  
  207.         ' Load Contact class properties into textboxes.
  208.         LineOneTextBox.Text = aContact.FirstName
  209.         LineTwoTextBox.Text = aContact.LastName
  210.     End Sub
  211.  
  212.     Private Sub TagForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  213.         Handles MyBase.FormClosing
  214.  
  215.         ' Instantiate a new Contact and assign textbox values.
  216.         Dim aContact As New Contact With {
  217.             .FirstName = LineOneTextBox.Text,
  218.             .LastName = LineTwoTextBox.Text}
  219.  
  220.         'Assign data to form's tag property.
  221.         Me.Tag = aContact
  222.     End Sub
  223. End Class
  224.  
  225. '-------------------------------------------------------------------------------------------
  226. Public Class Contact
  227.     ' Auto-Implemented Properties
  228.     Public Property FirstName As String
  229.     Public Property LastName As String
  230. End Class
Advertisement
Add Comment
Please, Sign In to add comment