Results 1 to 4 of 4

Thread: [RESOLVED] Listbox question

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Resolved [RESOLVED] Listbox question

    i have this code with my listbox

    Private Sub Command1_Click()

    If List1.ListIndex = -1 Then Exit Sub
    Text1.Text = "press 2945#*3101#"


    If List1.ListIndex = 0 Then Exit Sub
    Text1.Text = "press 2945#*7101#"

    If List1.ListIndex = 1 Then Exit Sub
    Text1.Text = "press 2945#*7101#"

    If List1.ListIndex = 2 Then Exit Sub
    Text1.Text = "press 2945#*2601#"

    If List1.ListIndex = 3 Then Exit Sub
    Text1.Text = "press 2945#*9701#"

    If List1.ListIndex = 4 Then Exit Sub
    Text1.Text = "press 2945#*1201#"

    If List1.ListIndex = 5 Then Exit Sub
    Text1.Text = "press 2945#*1201#"

    If List1.ListIndex = 6 Then Exit Sub
    Text1.Text = "press 2945#*3311#"

    If List1.ListIndex = 7 Then Exit Sub
    Text1.Text = "press 2945#*7101#"

    If List1.ListIndex = 8 Then Exit Sub
    Text1.Text = "press 2945#*7101#"

    End Sub
    Private Sub Form_Load()

    With List1

    .AddItem "LG C3100"

    .AddItem "LG C3380"

    .AddItem "LG F2300"

    .AddItem "LG KE260"

    .AddItem "LG KE970"

    .AddItem "LG KG120"

    End With

    End Sub
    the code is working perfect, but its badly coded because i have 50pcs of LG MODELS so the code If List1.ListIndex = X Then Exit Sub
    Text1.Text = "press bla bla" is very long,

    is there any way to make this short?
    sorry for being a noob im really a beginner

  2. #2
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Listbox question

    vb Code:
    1. Select Case List1.ListIndex
    2.  Case 0
    3.   Text1.Text = "press 2945#*3101#"
    4.  Case Is < 3, Is > 7
    5.   Text1.Text = "press 2945#*7101#"
    6.  Case 3
    7.   Text1.Text = "press 2945#*2601#"
    8.  Case 4
    9.   Text1.Text = "press 2945#*9701#"
    10.  case 5 To 6 'or 5, 6 can be used
    11.   Text1.Text = "press 2945#*1201#"
    12.  case 7
    13.   Text1.Text = "press 2945#*3311#"
    14.  case else
    15.   text1.text = "not found"
    16. End Select

    Every comma in a Case [Is] X statement is basically a logical OR.

    You can also specify ranges, such as 5 to 6, or 1 to 100, etc. Is is only required for comparisons, such as >, <, =>, <=, <>.

  3. #3
    Lively Member
    Join Date
    Jan 2009
    Posts
    93

    Re: Listbox question

    You could use a SELECT CASE statement instead of a bunch of IF's.

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Re: Listbox question

    thanks man i will try it tomorrow,

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