Results 1 to 10 of 10

Thread: [2005] Select CASE [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Resolved [2005] Select CASE [RESOLVED]

    Code:
    If Ctype(control, TextBox).Id.StartsWith("txtTest1") Then
      'do something
    ElseIf Ctype(control, TextBox).Id.StartsWith("txtTest2") Then
     'do something
    ElseIf Ctype(control, TextBox).Id.StartsWith("txtTest3") Then
     'do something
    ElseIf Ctype(control, TextBox).Id.StartsWith("txtTest4") Then
     'do something
     '{more if statements}
    Else If
    Now i want to recode this with using SELECT CASE.
    How do i use select case when i want that each case finds if certain control's id startswith given value?

    Thanks
    Last edited by selanec; Apr 30th, 2008 at 06:08 PM.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2005] Select CASE

    vb.net Code:
    1. Dim condition As String = CType(Control, TextBox).Id
    2.         Select Case condition
    3.             Case condition.StartsWith("txtTest1")
    4.             Case condition.StartsWith("txtTest2")
    5.             Case condition.StartsWith("txtTest3")
    6.             Case condition.StartsWith("txtTest4")
    7.         End Select

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: [2005] Select CASE

    This is not working, Sorry

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Select CASE

    What do you mean it doesn't work? There's no reason it shouldn't.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Junior Member
    Join Date
    Apr 2008
    Location
    Karachi, Sindh, Pakistan
    Posts
    23

    Re: [2005] Select CASE

    Select Case can only be used here, if the total number of characters are equal for all the cases.

    Example:

    HTML Code:
    Dim condition As String = CType(Control, TextBox).Id
    Select Case Microsoft.VisualBasic.Left(condition, 8).ToUpper()
        Case "TXTTEST1"
        Case "TXTTEST2"
        ...
    End Select

  6. #6
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2005] Select CASE

    vb.net Code:
    1. Private Sub Button2_Click( _
    2.         ByVal sender As System.Object, _
    3.         ByVal e As System.EventArgs _
    4.     ) Handles Button2.Click
    5.  
    6.         DoStuff(txtTest2)
    7.     End Sub
    8.  
    9.     Private Sub DoStuff(ByVal control As Control)
    10.         Dim condition As String = CType(control, TextBox).Name
    11.         Select Case condition
    12.             Case "txtTest1"
    13.                 MessageBox.Show("txtTest1")
    14.             Case "txtTest2"
    15.                 MessageBox.Show("txtTest2")
    16.             Case "txtTest3"
    17.                 MessageBox.Show("txtTest2")
    18.             Case "txtTest4"
    19.                 MessageBox.Show("txtTest4")
    20.         End Select
    21.     End Sub

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Select CASE

    Quote Originally Posted by sallushan
    Select Case can only be used here, if the total number of characters are equal for all the cases.

    Example:

    HTML Code:
    Dim condition As String = CType(Control, TextBox).Id
    Select Case Microsoft.VisualBasic.Left(condition, 8).ToUpper()
        Case "TXTTEST1"
        Case "TXTTEST2"
        ...
    End Select
    regarding the statement... that's a bunch of hooey. Regarding the code, because of the design of the code, the statement becomes true.... but there's no need to grab only the left 8 characters. How would you test for "txtTest10".... or "txtTest12".... or "txtTest21" ???

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Select CASE

    Quote Originally Posted by Deepak Sakpal
    vb.net Code:
    1. Dim condition As String = CType(Control, TextBox).Id
    2.         Select Case condition
    3.             Case condition.StartsWith("txtTest1")
    4.             Case condition.StartsWith("txtTest2")
    5.             Case condition.StartsWith("txtTest3")
    6.             Case condition.StartsWith("txtTest4")
    7.         End Select
    Actually... this could work, with one modification:
    Code:
            Dim condition As String = CType(Control, TextBox).Id
            Select Case True
                Case condition.StartsWith("txtTest1")
                Case condition.StartsWith("txtTest2")
                Case condition.StartsWith("txtTest3")
                Case condition.StartsWith("txtTest4")
            End Select
    It's a usage of the Select Case that I don't personally care for... but, it should work.

    @sallushan - I see what you meant now, and why only the left 8 were used... point taken. I'm the one that's dense this morning.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    Re: [2005] Select CASE

    Select Case True

    that's the winner. Thanks a lot Mr. MVP
    I realized that SrartsWith is actually boolean function so your last suggestion worked like a charm

    Thanks a lot

  10. #10
    Junior Member
    Join Date
    Apr 2008
    Location
    Karachi, Sindh, Pakistan
    Posts
    23

    Re: [2005] Select CASE [RESOLVED]

    Good one. It really increased my experience with SELECT CASE.

Posting Permissions

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



Click Here to Expand Forum to Full Width