PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Send String as Address in AddHandler-VBForums
Results 1 to 3 of 3

Thread: Send String as Address in AddHandler

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Send String as Address in AddHandler

    Hello..
    I am using an PrintDocument to print various formats depending on the subject.
    To do so i am using the following code

    vb Code:
    1. Select Case Subject
    2.             Case "Subject1"
    3.                 AddHandler Me.PrintDocument1.PrintPage, AddressOf Subject1Format
    4.             Case "Subject2"
    5.                 AddHandler Me.PrintDocument1.PrintPage, AddressOf Subject2RegularFormat
    6.             Case "Subject3"
    7.                 AddHandler Me.PrintDocument1.PrintPage, AddressOf FullPageFormat
    8.    ..so on
    9.  End Select

    Currently there are 8 subjects, and many more subject are being added.

    so is there any possibility something like

    vb Code:
    1. Dim FuncName as String = Me.GetFuncNameFromDB(SubjectName)
    2. AddHandler Me.PrintDocument1.PrintPage, AddressOf FuncName

    TIA

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,916

    Re: Send String as Address in AddHandler

    Can we assume from the fact that your second code snippet calls a method named 'GetFuncNameFromDB' that you have records in a database that contain the pairs "Subject1" and "Subject1Format", "Subject2" and "Subject2RegularFormat" as well as "Subject3" and "FullPageFormat"? Is it possible that those database values or relationships would change without the code changing? I ask because if you have to change the code every time you add a new formatting method anyway, I don't see the value in storing the values and relationships in the database. I would tend to just create a Dictionary with the relationships and then do away with your Select Case in that case.

    If you do want to go down the road of using Strings then it is possible using Reflection. You can call the Type.GetMethod method to get a MethodInfo object by name, i.e. using a String. You can then call CreateDelegate on that MethodInfo to get a PrintPageEventHandler delegate that can be used in an AddHandler or RemoveHandler statement.
    Last edited by jmcilhinney; Feb 6th, 2019 at 07:49 PM.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,916

    Re: Send String as Address in AddHandler

    I decided to post an example because it's a little bit fiddly and may be confusing if you're not familiar with Reflection. I created a test project with two RadioButtons and two Buttons. I changed the names of the RadioButtons to 'helloRadioButton' and 'goodbyeRadioButton' and the Text properties to "Hello" and "Goodbye" respectively. I then added the following code:
    vb.net Code:
    1. Imports System.Reflection
    2.  
    3. Public Class Form1
    4.  
    5.     Private handler As EventHandler
    6.  
    7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.         If handler IsNot Nothing Then
    9.             RemoveHandler Button2.Click, handler
    10.         End If
    11.  
    12.         Dim handlerName = "Say" & {helloRadioButton, goodbyeRadioButton}.Single(Function(rb) rb.Checked).Text
    13.         Dim handlerMethod = Me.GetType().GetMethod(handlerName, BindingFlags.NonPublic Or BindingFlags.Instance)
    14.         Dim handlerDelegate = handlerMethod.CreateDelegate(GetType(EventHandler), Me)
    15.  
    16.         handler = DirectCast(handlerDelegate, EventHandler)
    17.  
    18.         AddHandler Button2.Click, handler
    19.     End Sub
    20.  
    21.     Private Sub SayHello(sender As Object, e As EventArgs)
    22.         MessageBox.Show("Hello")
    23.     End Sub
    24.  
    25.     Private Sub SayGoodbye(sender As Object, e As EventArgs)
    26.         MessageBox.Show("Goodbye")
    27.     End Sub
    28.  
    29. End Class
    You can then click Button1 to set the Click event handler of Button2 based on the Text of the RadioButton that is currently checked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width