Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Data Passing Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 04 JUL 2016
- '
- ' Description: Program shows various ways to pass data between Windows Forms.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/Gyjwh9R
- ' App's Visual Basic .NET code is at http://pastebin.com/cCy4hhQg
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Public Class MainForm
- Private Sub PassByGobalsButton_Click(sender As Object, e As EventArgs) _
- Handles PassByGobalsButton.Click
- ' Assign textbox values to global variables.
- gDataOne = LineOneTextBox.Text
- gDataTwo = LineTwoTextBox.Text
- Dim frm As New GlobalsForm
- frm.Show()
- End Sub
- Private Sub PassByConstructorButton_Click(sender As Object, e As EventArgs) _
- Handles PassByConstructorButton.Click
- ' Pass textbox values in form's constructor.
- Dim frm As New ConstructorForm(LineOneTextBox.Text, LineTwoTextBox.Text)
- frm.Show()
- End Sub
- Private Sub PassByPropertiesButton_Click(sender As Object, e As EventArgs) _
- Handles PassByPropertiesButton.Click
- ' Assign textbox values to form's properties.
- Dim frm As New PropertyForm
- frm.DataOne = LineOneTextBox.Text
- frm.DataTwo = LineTwoTextBox.Text
- frm.Show()
- End Sub
- Private Sub PassByDialogButton_Click(sender As Object, e As EventArgs) _
- Handles PassByDialogButton.Click
- ' Assign textbox values to form's properties.
- ' However, update textboxes only if OK button clicked.
- Dim frm As New DialogForm
- frm.DataOne = LineOneTextBox.Text
- frm.DataTwo = LineTwoTextBox.Text
- If frm.ShowDialog = DialogResult.OK Then
- LineOneTextBox.Text = frm.DataOne
- LineTwoTextBox.Text = frm.DataTwo
- End If
- End Sub
- Private Sub PassByTagButton_Click(sender As Object, e As EventArgs) _
- Handles PassByTagButton.Click
- ' Instantiate a new Contact and assign textbox values.
- Dim aContact As New Contact With {
- .FirstName = LineOneTextBox.Text,
- .LastName = LineTwoTextBox.Text}
- ' Assign data to form's tag property.
- Dim frm As New TagForm
- frm.Tag = aContact
- frm.Show()
- End Sub
- Private Sub PassByTag2WayButton_Click(sender As Object, e As EventArgs) _
- Handles PassByTag2WayButton.Click
- ' Instantiate a new Contact and assign textbox values.
- Dim aContact As New Contact With {
- .FirstName = LineOneTextBox.Text,
- .LastName = LineTwoTextBox.Text}
- ' Assign data to form's tag property and show modal.
- Dim frm As New TagForm
- frm.Tag = aContact
- frm.ShowDialog()
- ' Cast from object to contact datatype and assign
- ' data to textboxes.
- aContact = CType(frm.Tag, Contact)
- LineOneTextBox.Text = aContact.FirstName
- LineTwoTextBox.Text = aContact.LastName
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- Public Class GlobalsForm
- Private Sub GlobalsForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Load Data to TextBoxes from global variables.
- LineOneTextBox.Text = gDataOne
- LineTwoTextBox.Text = gDataTwo
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- Module GlobalVariables
- Public gDataOne As String
- Public gDataTwo As String
- End Module
- '-------------------------------------------------------------------------------------------
- Public Class ConstructorForm
- ' Default Constructor.
- Public Sub New()
- MyBase.New ' In vb.net if omitted, implicitly called.
- ' This call is required by the designer.
- InitializeComponent()
- ' Add any initialization after the InitializeComponent() call.
- End Sub
- ' Parameterized Constructor.
- Public Sub New(ByVal dataOne As String, dataTwo As String)
- MyBase.New ' In vb.net if omitted, implicitly called.
- InitializeComponent()
- ' Assign parameter data passed to textboxes.
- LineOneTextBox.Text = dataOne
- LineTwoTextBox.Text = dataTwo
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- Public Class PropertyForm
- Public Property DataOne As String
- Public Property DataTwo As String
- Private Sub PropertyForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Load Data to TextBoxes.
- LineOneTextBox.Text = DataOne
- LineTwoTextBox.Text = DataTwo
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- Public Class DialogForm
- Public Property DataOne As String
- Public Property DataTwo As String
- Private Sub DialogForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Load Data to TextBoxes.
- LineOneTextBox.Text = DataOne
- LineTwoTextBox.Text = DataTwo
- End Sub
- Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles OK_Button.Click
- ' Return Data
- DataOne = LineOneTextBox.Text
- DataTwo = LineTwoTextBox.Text
- Me.DialogResult = System.Windows.Forms.DialogResult.OK
- Me.Close()
- End Sub
- Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Cancel_Button.Click
- Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
- Me.Close()
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- Public Class TagForm
- Private Sub TagForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Cast from object to contact datatype.
- Dim aContact As Contact = CType(Me.Tag, Contact)
- ' Load Contact class properties into textboxes.
- LineOneTextBox.Text = aContact.FirstName
- LineTwoTextBox.Text = aContact.LastName
- End Sub
- Private Sub TagForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles MyBase.FormClosing
- ' Instantiate a new Contact and assign textbox values.
- Dim aContact As New Contact With {
- .FirstName = LineOneTextBox.Text,
- .LastName = LineTwoTextBox.Text}
- 'Assign data to form's tag property.
- Me.Tag = aContact
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- Public Class Contact
- ' Auto-Implemented Properties
- Public Property FirstName As String
- Public Property LastName As String
- End Class
Advertisement
Add Comment
Please, Sign In to add comment