vendredi 18 novembre 2016

vb express 2015: Edit multiple controls in one sub using control.tag as identifier

In Javascript I am able to create a function that catch clicks from random elements (like buttons). then use the attribute of that control to change the behaviour of other elements.

like this

 var test = $(this).attrib("id")
 $('#' + test).something something code here

in VB however I cannot find a way to create a function that does something similar.

Trying to explain better: I have 10 checkboxes that should activate different other elements depending on which checkbox is clicked. Now I could write 10 different handlers using the same code, only changing the control names in each handler. OR I could use a Select case reading the tag from the clicked checkbox and then do the same as if I had 10 different handlers. Problem is: this is tiresome, and creates unnecessary lines of code.

I thought I'd use control.tag to determine which controls to edit next. But I am at a standstill.

Private Sub M_Click(ByVal sender As System.Object,
                        ByVal e As System.EventArgs) Handles M0.Click, M1.Click, M2.Click, MM0.Click, MM1.Click, MM2.Click

    ' SUB handles label clicks, sets tab as active and starts/stops timer
    Dim clickedBox = TryCast(sender, CheckBox)

    If clickedBox IsNot Nothing Then
        'Activate FilmClip
        Dim tjekked As Boolean
        Dim videoURL = Filer2.SelectedIndex

       Dim tjekked As Boolean
        Dim videoURL As String
        Dim imgIcon As Image = My.Resources.film

        If clickedBox.checked = False Then
            tjekked = False
            videoURL = "0"
        Else
            tjekked = True
            videoURL = AxWindowsMediaPlayer1.URL
            If Microsoft.VisualBasic.Right(videoURL, 4) = "strm" Then
                imgIcon = My.Resources.kodi
            End If
        End If

        Select Case clickedBox.tag
                Case "v1"
                    v1.Visible = tjekked
                    If tjekked = False Then
                        v1.BackColor = Color.White
                        v1.Image = My.Resources.film
                        videoURL = ""
                    End If
                    v1.Tag = videoURL
                    L1.Text = v1.Tag
                    v1.Image = imgIcon
                Case "v2"
                 ... and so on

As you can see, much of the code will be the same, no matter which checkbox is activated. The only things that changes are the control names.

You may also notice that I have created a multievent handler catching which checkbox is activated, but I am not able to use that information to avoid using the Select Case option.

I have tried CTYPE, DirectCast and TryCast. The following image shows the control with some of the functionality. enter image description here

Now clicking the checkbox should activate the movie-icon and add text to the variable on the left (numbers)

please help or advice




Aucun commentaire:

Enregistrer un commentaire