none
calling sub from other class with CallByName RRS feed

  • Question

  • Hi Guys,

    my Form is _3_Reports and i'm trying to call a sub from another class once the name of the checkbox contains specific characters (for this example i change it to = "Jan_payed").

    the problem is that in GreenLine Class it's the line 

     If _3_Reports.Jan_Payed.Checked"

    always False although the checkbox is checked.. how is it possible??

    Public Class _3_Reports
    Private Sub _3_Reports_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            For Each ct As Control In Me.Controls
                If (TypeOf ct Is CheckBox) Then
                    AddHandler ct.Click, AddressOf CheckBox_Update
    
                End If
            Next
    
    End Sub
    
    Private Sub CheckBox_Update(ByVal Sender As Object, ByVal e As EventArgs)
    
            If Sender.name = "Jan_Payed" Then
    
                Dim cls As New GreenLine
                CallByName(cls, Sender.name, CallType.Method, Nothing)
            End If
    End Sub
    
    End Class
    
    Class GreenLine
    
    
    
        Sub Jan_Payed()
            Dim result As Integer = 0
            If _3_Reports.Jan_Payed.Checked = True Then
               
    
                   _3_Reports.Panel_Jan.Visible = True
                   _3_Reports.Jan_DocReceived.BackColor = Color.LightGreen
                   _3_Reports.Jan_DocTyped.BackColor = Color.LightGreen
                   _3_Reports.Label_Jan.BackColor = Color.LightGreen
                   _3_Reports.Button_jan.BackColor = Color.LightGreen
                   _3_Reports.Jan_Payed.BackColor = Color.LightGreen
            Else
             
                _3_Reports.Panel_Jan.Visible = False
                _3_Reports.Jan_DocReceived.BackColor = Color.AliceBlue
                _3_Reports.Jan_DocTyped.BackColor = Color.AliceBlue
                _3_Reports.Label_Jan.BackColor = Color.AliceBlue
                _3_Reports.Button_jan.BackColor = Color.AliceBlue
                _3_Reports.Jan_Payed.BackColor = Color.AliceBlue
            End If
        End sub
    end class
    


    Tuesday, January 8, 2019 11:18 AM

Answers

  • still not working for me :(

        Private Sub CheckBox_Update(ByVal Sender As Object, ByVal e As EventArgs)
    Dim CurrentCheckBox = CType(Sender, CheckBox)
            If CurrentCheckBox.Name = "Jan_Payed" Then
    
                Dim cls As New GreenLine
                CallByName(cls, CurrentCheckBox.Name, CallType.Method, CurrentCheckBox)
            End If
    
    End Sub
    
    
    Public Class GreenLine
    
    
    
        Sub Jan_Payed(sender As CheckBox)
            Dim result As Integer = 0
            If sender.Checked = True Then
    
                _3_Reports.Panel_Jan.Visible = True
                _3_Reports.Jan_DocReceived.BackColor = Color.LightGreen
                _3_Reports.Jan_DocTyped.BackColor = Color.LightGreen
                _3_Reports.Label_Jan.BackColor = Color.LightGreen
                _3_Reports.Button_jan.BackColor = Color.LightGreen
                _3_Reports.Jan_Payed.BackColor = Color.LightGreen
            Else
    
                _3_Reports.Panel_Jan.Visible = False
                _3_Reports.Jan_DocReceived.BackColor = Color.AliceBlue
                _3_Reports.Jan_DocTyped.BackColor = Color.AliceBlue
                _3_Reports.Label_Jan.BackColor = Color.AliceBlue
                _3_Reports.Button_jan.BackColor = Color.AliceBlue
                _3_Reports.Jan_Payed.BackColor = Color.AliceBlue
            End If
        End Sub
    
    End Class

    Here is a working code sample

    https://1drv.ms/u/s!AtGAgKKpqdWjjHsgUe3CKmXD6l6k


    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

    • Marked as answer by noampro8 Wednesday, January 9, 2019 5:32 AM
    Tuesday, January 8, 2019 12:58 PM
    Moderator

All replies

  • Hello,

    First off seems like you have Option Strict Off because you never cast sender in the update method and in this case sender is known as an Object not a CheckBox.

    Conceptually speaking the following shows how this might be done (there are variations on this too but I believe you will get the point).

    Public Class Form1
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            For Each ct As Control In Me.Controls
                If (TypeOf ct Is CheckBox) Then
                    AddHandler ct.Click, AddressOf CheckBox_Update
                End If
            Next
        End Sub
        Private Sub CheckBox_Update(Sender As Object, e As EventArgs)
            Dim currentCheckBox = CType(Sender, CheckBox)
            If currentCheckBox.Name = "Jan_Payed" Then
                Dim cls As New GreenLine
                CallByName(cls, currentCheckBox.Name, CallType.Method, currentCheckBox)
            End If
        End Sub
    End Class
    Public Class GreenLine
        Public Sub Jan_Payed(sender As CheckBox)
            Console.WriteLine("{0} is {1}", sender.Name, sender.Checked)
        End Sub
    End Class

    Output from toggling jan checkbox


    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

    Tuesday, January 8, 2019 12:30 PM
    Moderator
  • still not working for me :(

        Private Sub CheckBox_Update(ByVal Sender As Object, ByVal e As EventArgs)
    Dim CurrentCheckBox = CType(Sender, CheckBox)
            If CurrentCheckBox.Name = "Jan_Payed" Then
    
                Dim cls As New GreenLine
                CallByName(cls, CurrentCheckBox.Name, CallType.Method, CurrentCheckBox)
            End If
    
    End Sub
    
    
    Public Class GreenLine
    
    
    
        Sub Jan_Payed(sender As CheckBox)
            Dim result As Integer = 0
            If sender.Checked = True Then
    
                _3_Reports.Panel_Jan.Visible = True
                _3_Reports.Jan_DocReceived.BackColor = Color.LightGreen
                _3_Reports.Jan_DocTyped.BackColor = Color.LightGreen
                _3_Reports.Label_Jan.BackColor = Color.LightGreen
                _3_Reports.Button_jan.BackColor = Color.LightGreen
                _3_Reports.Jan_Payed.BackColor = Color.LightGreen
            Else
    
                _3_Reports.Panel_Jan.Visible = False
                _3_Reports.Jan_DocReceived.BackColor = Color.AliceBlue
                _3_Reports.Jan_DocTyped.BackColor = Color.AliceBlue
                _3_Reports.Label_Jan.BackColor = Color.AliceBlue
                _3_Reports.Button_jan.BackColor = Color.AliceBlue
                _3_Reports.Jan_Payed.BackColor = Color.AliceBlue
            End If
        End Sub
    
    End Class

    Tuesday, January 8, 2019 12:52 PM
  • still not working for me :(

        Private Sub CheckBox_Update(ByVal Sender As Object, ByVal e As EventArgs)
    Dim CurrentCheckBox = CType(Sender, CheckBox)
            If CurrentCheckBox.Name = "Jan_Payed" Then
    
                Dim cls As New GreenLine
                CallByName(cls, CurrentCheckBox.Name, CallType.Method, CurrentCheckBox)
            End If
    
    End Sub
    
    
    Public Class GreenLine
    
    
    
        Sub Jan_Payed(sender As CheckBox)
            Dim result As Integer = 0
            If sender.Checked = True Then
    
                _3_Reports.Panel_Jan.Visible = True
                _3_Reports.Jan_DocReceived.BackColor = Color.LightGreen
                _3_Reports.Jan_DocTyped.BackColor = Color.LightGreen
                _3_Reports.Label_Jan.BackColor = Color.LightGreen
                _3_Reports.Button_jan.BackColor = Color.LightGreen
                _3_Reports.Jan_Payed.BackColor = Color.LightGreen
            Else
    
                _3_Reports.Panel_Jan.Visible = False
                _3_Reports.Jan_DocReceived.BackColor = Color.AliceBlue
                _3_Reports.Jan_DocTyped.BackColor = Color.AliceBlue
                _3_Reports.Label_Jan.BackColor = Color.AliceBlue
                _3_Reports.Button_jan.BackColor = Color.AliceBlue
                _3_Reports.Jan_Payed.BackColor = Color.AliceBlue
            End If
        End Sub
    
    End Class

    Here is a working code sample

    https://1drv.ms/u/s!AtGAgKKpqdWjjHsgUe3CKmXD6l6k


    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

    • Marked as answer by noampro8 Wednesday, January 9, 2019 5:32 AM
    Tuesday, January 8, 2019 12:58 PM
    Moderator