none
VB.Net: How to access a WinForm Template button click events RRS feed

  • Question

  • Hi,

    I created a Windows Form template that I instantiate for other forms that share the same controls in the template.  Now, how do I access the button click events for each new instance of the form?

    For example, WinFormTemplate was instantiated for the following in Form1 (the main form):

      Dim ConForm = New WinFormTemplate()
      Dim EquipForm = New WinFormTemplate()
      Dim FertForm = New WinFormTemplate()

    The WinFormTemplate has New, Edit, Copy, Delete, and Save Buttons.  When I click any of the buttons, how do I let the Click Event know which form it came from (whether from ConForm, EquipForm, FertForm)?

    Appreciate any help.


    Marilyn Gambone

    Friday, December 13, 2019 4:04 PM

Answers

  • One idea is to have a Enum to determine type of operations.

    Public Enum WorkType
        ConSomething
        EquipSomething
        FetSomething
    End Enum

    Example

    Public Class WinFormTemplate
        Private WorkType As WorkType
        Public Sub New(workType As WorkType)
            InitializeComponent()
            workType = workType
        End Sub
    
        Private Sub NewButton_Click(sender As Object, e As EventArgs) Handles NewButton.Click
            Select Case WorkType
                Case WorkType.ConSomething
                    '
                Case WorkType.EquipSomething
                    '
                Case WorkType.FetSomething
                    '
            End Select
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by deskcheck1 Monday, December 16, 2019 3:24 PM
    Friday, December 13, 2019 4:18 PM
    Moderator
  • Hi

    Another way to do it. In this case, using the .Tag Property.

    Example Form1  code

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim ConForm As New WinFormTemplate() With {.Tag = "ConForm"}
    		Dim EquipForm As New WinFormTemplate() With {.Tag = "EquipForm"}
    		Dim FertForm As New WinFormTemplate() With {.Tag = "FertForm"}
    
    		ConForm.Show()
    		EquipForm.Show()
    		FertForm.Show()
    
    	End Sub
    End Class

    Example WinFormTemplate code

    Option Strict On
    Option Explicit On
    Public Class WinFormTemplate
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim b As Button = TryCast(sender, Button)
        MessageBox.Show("This message comes to you from Form " & b.Parent.Tag.ToString)
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    Friday, December 13, 2019 4:25 PM

All replies

  • One idea is to have a Enum to determine type of operations.

    Public Enum WorkType
        ConSomething
        EquipSomething
        FetSomething
    End Enum

    Example

    Public Class WinFormTemplate
        Private WorkType As WorkType
        Public Sub New(workType As WorkType)
            InitializeComponent()
            workType = workType
        End Sub
    
        Private Sub NewButton_Click(sender As Object, e As EventArgs) Handles NewButton.Click
            Select Case WorkType
                Case WorkType.ConSomething
                    '
                Case WorkType.EquipSomething
                    '
                Case WorkType.FetSomething
                    '
            End Select
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by deskcheck1 Monday, December 16, 2019 3:24 PM
    Friday, December 13, 2019 4:18 PM
    Moderator
  • Hi

    Another way to do it. In this case, using the .Tag Property.

    Example Form1  code

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim ConForm As New WinFormTemplate() With {.Tag = "ConForm"}
    		Dim EquipForm As New WinFormTemplate() With {.Tag = "EquipForm"}
    		Dim FertForm As New WinFormTemplate() With {.Tag = "FertForm"}
    
    		ConForm.Show()
    		EquipForm.Show()
    		FertForm.Show()
    
    	End Sub
    End Class

    Example WinFormTemplate code

    Option Strict On
    Option Explicit On
    Public Class WinFormTemplate
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim b As Button = TryCast(sender, Button)
        MessageBox.Show("This message comes to you from Form " & b.Parent.Tag.ToString)
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    Friday, December 13, 2019 4:25 PM