-1

I'm new to VB.NET and I was experimenting with trying to call a Sub from another. I have the first Sub that handles a button then executes some code. I then want to call the other Sub I have that handles a rectangle being tapped. I'm having trouble call the second Sub from the first Sub. I think it might be the EventArgs. The first Sub is RoutedEventsArgs and the second is TappedRoutedEventArgs. I can't seem to find any documentation about this particular instance. I only see if it as the same e.

Public Class MainPage

    Private Sub Show_Message(sender As Object, e As RoutedEventArgs) Handles Button_1.Click
        If Message.Visibility = Visibility.Collapsed Then
            Message.Visibility = Visibility.Visible
        Else
            Message.Visibility = Visibility.Collapsed
        End If

        ChangeColorItem_Click(sender, e)
    End Sub

    Private Sub ChangeColorItem_Click(sender As Object, e As TappedRoutedEventArgs) Handles Rectangle.Tapped

        'Change fill color to red to blue and back 
        If rectangleFill.Color = (Windows.UI.Colors.Red) Then
            rectangleFill.Color = Windows.UI.Colors.Blue
        Else
            rectangleFill.Color = Windows.UI.Colors.Red
        End If
    End Sub
End Class

I want it so that if Show_Message is run also run the ChangeColorItem_Click.

0

RoutedEventArgs is a square peg, TappedRoutedEventArgs is a round hole... you're trying to put a square peg in a round hole... and you don't need to.

Unless there's a need for the event args, and I don't see the event args being used, you're better off putting the code into their own sub and simply calling it.

Public Class MainPage

    Private Sub Show_Message(sender As Object, e As RoutedEventArgs) Handles Button_1.Click
        ShowMessage()
    End Sub

    Private Sub ChangeColorItem_Click(sender As Object, e As TappedRoutedEventArgs) Handles Rectangle.Tapped

        ChangeColorItem()
    End Sub

    Private Sub ShowMessage()
        If Message.Visibility = Visibility.Collapsed Then
            Message.Visibility = Visibility.Visible
        Else
            Message.Visibility = Visibility.Collapsed
        End If

        ChangeColorItem()
    End Sub

    Private Sub ChangeColorItem()
        'Change fill color to red to blue and back 
        If rectangleFill.Color = (Windows.UI.Colors.Red) Then
            rectangleFill.Color = Windows.UI.Colors.Blue
        Else
            rectangleFill.Color = Windows.UI.Colors.Red
        End If
    End Sub
End Class
  • Ok, I will keep that in mind in the future. I am just learning VB.Net, I come from a VBA background. My work has tasked me with making a new program from the ground up. I wanted to see how to make the two subs work together to reduce duplicate code for future code. – nmhammer Aug 13 at 15:13
  • I did get to work the way I described by using the ChangeColorItem_Click(Nothing, Nothing) that some else suggested by deleting the Handles Button_1.Click – nmhammer Aug 13 at 15:44
  • Those do work, as does creating a New TappedRoutedEventArggs and passing it in... but many wold consider all of those options poor coding practices and would fail most code reviews. – TechGnome Aug 13 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.