none
What form a function was called from RRS feed

  • Question

  • I have a function stored in a shared file that is to be used by multiple forms.  How do I let the function know WHICH form called if??
    Saturday, July 22, 2017 4:17 PM

Answers

  • I was initially thinking like Cor and Acamar, that I needed a header similar to

    public shared sub myFunction(sender as object)

    but a friend put me onto an alternative.

    '  call to shared function
    functionFile.clearAllDateFields(Me.Controls)
    
    '  body of shared function
        Public Shared Sub clearAllDateFields(ByRef formControlList As ControlCollection)
            For Each Control In formControlList
                If TypeOf Control Is DateTimePicker Then
                    DirectCast(Control, DateTimePicker).CustomFormat = " "
                    DirectCast(Control, DateTimePicker).Format = DateTimePickerFormat.Custom
                End If
            Next
        End Sub

    This function receives a reference to the controls on the calling form and then cycles through each control until it finds a dateTimePicker and sets each to a blank value (custom format)...essentially resetting.  Just don't forget to set the format when the user selects a new date.

    Thanx for the suggestions.  Sorry if my description of what I was trying to do was too vague. 

    • Marked as answer by DarrMik Sunday, July 23, 2017 1:46 AM
    Sunday, July 23, 2017 1:46 AM

All replies

  • If you look at the normal windows forms methods, then you see that the normal passing for this kind of methods is. 

    Function x (sender as object)

    You should tell the method who is sending. There is no way to know Sorcerer's apprentice which tells somewhere what is the caller.

    (Otherwise they would have never created that pattern) 


    Success
    Cor

    Saturday, July 22, 2017 4:53 PM
  • Here is a simple method that will work.

    Module Module1
        Public Sub Demo(ByVal sender As Form)
            If sender Is Form1 Then
                Console.WriteLine("Form1")
            ElseIf sender Is Form2 Then
                Console.WriteLine("Form2")
            End If
        End Sub
    End Module
    


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, July 22, 2017 8:33 PM
    Moderator
  • Yes...it is a shared class (sorry.)  I am looking to get a reference to the FORM that the call was made from.

    End result, this function will cycle through all the controls on the specified form and reset all the controls of a specified type (textBox.)

    Saturday, July 22, 2017 9:46 PM
  • How do I let the function know WHICH form called if??

    Which form, or which form instance?

    In either case, the answer is to add an additional parameter to the function signature, and have the caller pass the required data in the function call.  That data might be an identifier for the form (the name is one option) or it might be a reference to an object (eg, a reference to the form itself).  It depends on why your function needs to know.

    Saturday, July 22, 2017 10:59 PM
  • I was initially thinking like Cor and Acamar, that I needed a header similar to

    public shared sub myFunction(sender as object)

    but a friend put me onto an alternative.

    '  call to shared function
    functionFile.clearAllDateFields(Me.Controls)
    
    '  body of shared function
        Public Shared Sub clearAllDateFields(ByRef formControlList As ControlCollection)
            For Each Control In formControlList
                If TypeOf Control Is DateTimePicker Then
                    DirectCast(Control, DateTimePicker).CustomFormat = " "
                    DirectCast(Control, DateTimePicker).Format = DateTimePickerFormat.Custom
                End If
            Next
        End Sub

    This function receives a reference to the controls on the calling form and then cycles through each control until it finds a dateTimePicker and sets each to a blank value (custom format)...essentially resetting.  Just don't forget to set the format when the user selects a new date.

    Thanx for the suggestions.  Sorry if my description of what I was trying to do was too vague. 

    • Marked as answer by DarrMik Sunday, July 23, 2017 1:46 AM
    Sunday, July 23, 2017 1:46 AM
  • This function receives a reference to the controls on the calling form and then cycles through each control until it finds a dateTimePicker and sets each to a blank value (custom format)...essentially resetting.

    That's not a particularly safe solution.  If someday someone decides that the form needs another DTP for some other purpose, they will be surprised to find that it is getting automatically updated from somewhere that they never even considered in their modification.   A better solution is to create a List(Of date time pickers, populate it with only the DTP that you want to be updated, and pass that list to your function.  That also eliminates the casts and the need to cycle over all controls.

    Sunday, July 23, 2017 2:52 AM
  • Hadn't considered sending a ListOf dtp's into my function....would have saved a lot of headaches and research (all of which is good for this new programmer,) but as this function will only be getting called from Reset buttons on the different forms, I think I will stick with the current solution, but will also keep your's in mind for future applications.

    Thanx again

    Sunday, July 23, 2017 12:00 PM
  • Mostly OPs don't mark there own replies as answers especially not as they are so obvious wrong. 

    It solves maybe something you did wrong, but does not what you ask. 

    You only put persons who have the same kind of questions in the wrong direction. 

    Unlucky enough we have no moderators in this forum who correct this. 


    Success
    Cor



    Sunday, July 23, 2017 2:01 PM