Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 24450
  • Last Modified:

'Select Case' in vb.net

I have the following script which is using 'Select Case':

        Dim ctl As ToolStripButton = CType(sender, ToolStripButton)
        Dim MoveUp As Boolean = False
        Dim thisLV As ListView = AccountsLV

        Select Case ctl.Name
            Case "UpBtn1"
                MoveUp = True
                thisLV = ListView1
            Case "DownBtn1"
                MoveUp = False
                thisLV = ListView1
            Case "UpBtn2"
                MoveUp = True
                thisLV = ListView2
            Case "DownBtn2"
                MoveUp = False
                thisLV = ListView2
            Case "UpBtn3"
                MoveUp = True
                thisLV = ListView3
            Case "DownBtn3"
                MoveUp = False
                thisLV = ListView3
        End Select

As you can see, if the Name of the ToolStripButton begins with "Up", then I want 'MoveUp = True'. If not, then I want 'MoveUp = False'. Then, I want the number at the end of the toolstripbutton name to correspond with the ListView (i.e. DownBtn1 => 'thisLV = ListView1' or UpBtn3 => 'thisLV = ListView3')

Is there anyway to condense this? I am planning on having over 20 cases like this and don't want the code to get to lengthy.
0
tiehaze
Asked:
tiehaze
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure...try something like:

    Dim ctl As ToolStripButton = CType(sender, ToolStripButton)
    Dim MoveUp As Boolean = ctl.Name.StartsWith("Up")
    Dim thisLV As ListView = Me.Controls.Find("ListView" & ctl.Name.Substring(ctl.Name.IndexOf("Btn") + 3), True)(0)
0
 
smcdrcCommented:
if ctl.Name.Substring(0, 2).ToUpper.Equals("UP") then
MoveUp = True
else
MoveUp = False
end if

To shorten the 'thisLV', you could take off the last number and use a select case with that.
0
 
tiehazeAuthor Commented:
Can you explain the logic of:

Dim thisLV As ListView = Me.Controls.Find("ListView" & ctl.Name.Substring(ctl.Name.IndexOf("Btn") + 3), True)(0)

If you could explain it, then I might be able to use it in other instances
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Working from the inner to the outer we start with:

    "ListView" & ctl.Name.Substring(ctl.Name.IndexOf("Btn") + 3)

This extracts just the number portion of the Control name by grabbing everything past the "Btn" in the controls name.  We then append this number to "ListView" giving us:

    "UpBtn1" --> "ListView1"
    "DownBtn1" --> "ListView1"
    "UpBtn2" --> "ListView2"
    "DownBtn2" --> "ListView2"
    etc...

The Me.Controls.Find() method searches a container for a control that has a matching name.  The second parameter of True/False tells Find() whether to seach child containers.  This is necessary if the ListViews are actually NOT contained directly by the form (such as in a Panel for instance).  The Controls.Find() method returns an ARRAY of Controls that match so the "(0)" part on the end is so that we only use the first match found.

A more verbose version might be:

        Dim ctl As ToolStripButton = CType(sender, ToolStripButton)
        Dim MoveUp As Boolean = ctl.Name.StartsWith("Up")
        Dim thisLV As ListView = AccountsLV
        Dim btnPos As Integer = ctl.Name.IndexOf("Btn")
        If btnPos > -1 Then
            Dim ListViewName As String = "ListView" & ctl.Name.Substring(btnPos + 3)
            Dim matches() As Control = Me.Controls.Find(ListViewName, True)
            If matches.Length > 0 Then
                thislv = matches(0)
            Else
                MessageBox.Show(ListViewName, "Unable to find Corresponding ListView")
            End If
        Else
            MessageBox.Show(ctl.Name, "Unexpected Control Name")
        End If
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now