Results 1 to 25 of 25

Thread: Do While Statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    Question Do While Statement

    I want users to select items from a ListView I've created containing: arm, hand, foot, etc.
    I have 3 buttons being used: Do...While, Do...Next, and For...Next

    I have finished the code for the For...Next button, but need to do the same for the Do...While button using a Do...While statement.

    Does anyone know how I would edit my code to reflect a Do...While statement while still returning the same information??

    VB Code:
    1. Private Sub btnWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhile.Click
    2.         Dim sbMessage As New System.Text.StringBuilder
    3.         Dim liTemp As ListViewItem
    4.         If lstParts.SelectedItems.Count > 0 Then
    5.             sbMessage.Append("This is a Do While loop and you chose the following items:").Append(vbCrLf)
    6.             For Each liTemp In lstParts.Items
    7.                 If liTemp.Selected = True Then sbMessage.Append(liTemp.Text).Append(vbCrLf)
    8.             Next
    9.             txtLoop.Text = (sbMessage.ToString)
    10.         Else
    11.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    12.             Exit Sub
    13.         End If
    14.  
    15.     End Sub
    It works fine like this and returns the information I want, but instead of using a For..Next statement I need to use a Do...While statement. Any ideas???
    Twisted

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    This code looks suspiciously familiar
    If you want people to help you, don't claim their efforts as your own.

    Now for the answer to your question...

    VB Code:
    1. Private Sub btnWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhile.Click
    2.         Dim sbMessage As New System.Text.StringBuilder
    3.         Dim Indx As Int32 = 0
    4.         If lstParts.SelectedItems.Count > 0 Then
    5.             sbMessage.Append("This is a Do While loop and you chose the following items:").Append(vbCrLf)
    6.             Do While Indx <= Me.ListView1.SelectedIndices.Count - 1
    7.                 sbMessage.Append(Me.ListView1.SelectedItems(Indx).Text).Append(vbCrLf)
    8.                 Indx += 1
    9.             Loop
    10.             txtLoop.Text = (sbMessage.ToString)
    11.         Else
    12.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    13.             Exit Sub
    14.         End If
    15.     End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    I meant my code as (the code I'm using). You and I both know I'm to VB illterate to create code such as this. Thanks for helping!
    Twisted

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Now I tried to edit what you posted to a Do...Until statement but can't get it to work. Shouldn't it be the same except this <= should be >=??? Is that right?
    Twisted

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    yes, that should work, let me see what you have there

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    It works but not exactly correct. If I select one item it doesn't display it, if I select two it displays one, if I select three it displays two. Here's the code:
    VB Code:
    1. Private Sub btnUntil_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUntil.Click
    2.         Dim sbMessage As New System.Text.StringBuilder
    3.         Dim Indx As Int32 = 0
    4.         If lstParts.SelectedItems.Count > 0 Then
    5.             sbMessage.Append("This is a Do Until loop and you chose the following items:").Append(vbCrLf)
    6.             Do Until Indx >= Me.lstParts.SelectedIndices.Count - 1
    7.                 sbMessage.Append(Me.lstParts.SelectedItems(Indx).Text).Append(vbCrLf)
    8.                 Indx += 1
    9.             Loop
    10.             txtLoop.Text = (sbMessage.ToString)
    11.         Else
    12.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    13.             Exit Sub
    14.         End If
    15.     End Sub
    Twisted

  7. #7
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    just get rid of the "="

    VB Code:
    1. Private Sub btnUntil_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUntil.Click
    2.         Dim sbMessage As New System.Text.StringBuilder
    3.         Dim Indx As Int32 = 0
    4.         If lstParts.SelectedItems.Count > 0 Then
    5.             sbMessage.Append("This is a Do Until loop and you chose the following items:").Append(vbCrLf)
    6.             Do Until Indx > Me.lstParts.SelectedIndices.Count - 1
    7.                 sbMessage.Append(Me.lstParts.SelectedItems(Indx).Text).Append(vbCrLf)
    8.                 Indx += 1
    9.             Loop
    10.             txtLoop.Text = (sbMessage.ToString)
    11.         Else
    12.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    13.             Exit Sub
    14.         End If
    15.     End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Awesome! Now instead of listing the items like this:

    This is a Do Until loop and you chose the following items:
    ankle
    finger

    How can I get them to list in the textbox like this:

    This is a Do Until loop and you chose the following items:
    ankle and finger

    And if I have more then two like this:

    This is a Do Until loop and you chose the following items:
    ankle, finger, arm, and hand

    Mainly because if I select to many items they won't display b/c my texbox isn't that big.
    Twisted

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Ok, this will work, and before you ask to have it altered for each button on your form, why not give it a go on your own first

    VB Code:
    1. Private Sub btnUntil_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUntil.Click
    2.         Dim sbMessage As New System.Text.StringBuilder
    3.         Dim Indx As Int32 = 0
    4.         If Me.lstParts.SelectedItems.Count > 0 Then
    5.             sbMessage.Append("This is a Do Until loop and you chose the following items: ")
    6.             Do Until Indx > Me.lstParts.SelectedIndices.Count - 1
    7.                 If Indx = 0 Then
    8.                     sbMessage.Append(Me.lstParts.SelectedItems(Indx).Text)
    9.                 ElseIf Indx = Me.lstParts.SelectedIndices.Count - 1 Then
    10.                     sbMessage.Append(" and ").Append(Me.lstParts.SelectedItems(Indx).Text)
    11.                 Else
    12.                     sbMessage.Append(", ").Append(Me.lstParts.SelectedItems(Indx).Text)
    13.                 End If
    14.                 Indx += 1
    15.             Loop
    16.             txtLoop.Text = (sbMessage.ToString)
    17.         Else
    18.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    19.             Exit Sub
    20.         End If
    21.     End Sub

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Well I got the Do...While button to do it. That was easy b/c their wasn't much to change I've been working on trying to figure my Next button but can't get it to work right. It just displays one item selected and repeats it.

    This is a For...Next loop and you selected: arm and arm and arm and arm and arm and arm etc.

    VB Code:
    1. Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
    2.         ' Declare variables
    3.         Dim sbMessage As New System.Text.StringBuilder
    4.         Dim liTemp As ListViewItem
    5.         Dim Indx As Int32 = 0
    6.         ' If then statement with For...Next loop
    7.         If lstParts.SelectedItems.Count > 0 Then
    8.             sbMessage.Append("This is a Do Next loop and you selected:")
    9.             For Each liTemp In lstParts.Items
    10.                 If liTemp.Selected = True Then
    11.                     sbMessage.Append(liTemp.Text)
    12.                 ElseIf Indx = Me.lstParts.SelectedIndices.Count - 1 Then
    13.                     sbMessage.Append(" and ").Append(Me.lstParts.SelectedItems(Indx).Text)
    14.                 Else
    15.                     sbMessage.Append(", ").Append(Me.lstParts.SelectedItems(Indx).Text)
    16.                 End If
    17.             Next
    18.             txtLoop.Text = (sbMessage.ToString)
    19.         Else
    20.             ' If no items are selected show error message
    21.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    22.             Exit Sub
    23.         End If
    24.     End Sub
    Twisted

  11. #11
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    ok, I give you credit for trying it on your own, and you weren't far off, but this will work for you:
    VB Code:
    1. Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
    2.         ' Declare variables
    3.         Dim sbMessage As New System.Text.StringBuilder
    4.         Dim liTemp As ListViewItem
    5.         If Me.lstParts.SelectedItems.Count > 0 Then
    6.             sbMessage.Append("This is a Do Until loop and you chose the following items: ")
    7.             For Each liTemp In Me.lstParts.Items
    8.                 If liTemp.Selected = True Then
    9.                     If liTemp.Index = Me.lstParts.SelectedItems(0).Index Then
    10.                         sbMessage.Append(liTemp.Text)
    11.                     ElseIf liTemp.Index = Me.lstParts.SelectedItems(Me.lstParts.SelectedIndices.Count - 1).Index Then
    12.                         sbMessage.Append(" and ").Append(liTemp.Text)
    13.                     Else
    14.                         sbMessage.Append(", ").Append(liTemp.Text)
    15.                     End If
    16.                 End If
    17.             Next
    18.             txtLoop.Text = (sbMessage.ToString)
    19.         Else
    20.             MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    21.             Exit Sub
    22.         End If
    23.     End Sub
    Last edited by CyberHawke; Jun 24th, 2004 at 09:21 AM.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Didn't work b/c of ListView1?? what should thatbe?
    Twisted

  13. #13
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I edited the code, it should work now, but I left it in my last code post

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    I tried that myself thinking it would fix it but I get:

    lstParts is not a member of System.Windows.Forms.Listview

    ???
    Twisted

  15. #15
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Fixed, refer back to my code again...

    That's what I get for trying to convert my code in this window

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Awesome works perfect now. One more question though:

    Can I select more then one item from a ListView without having to hold CTRL??? Right now I have to hold CTRL to select more then one item.
    Twisted

  17. #17
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by twisted

    Can I select more then one item from a ListView without having to hold CTRL??? Right now I have to hold CTRL to select more then one item.
    You will notice the same holds true for any Windows app.

    Selecting multiples items either involves a Listview or ListBox and use of the CTRL key (OS dictates this, no simple way around it)

    OR

    Implement two listviews, one that contains all available items, one that contains the selected items (much like adding people to an email message in OutLook Express by clicking the => button)

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    OK thanks! Not a big problem if you can't select two items without holding down CTRL, but one problem I'm having without another Application is I have 2 seperate ListView boxes and I need to select one item from each. Why can I only select one from either? I need to pick and item from one lstDeparture and one from lstDestination so it will calculate the mileage to get there. Any clues?
    Twisted

  19. #19
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    When you select one item, store it's value in a variable. Then do the same for the other item. Then do the calculation.

  20. #20
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Not a good solution salvelinus what happens if you click the wrong element by mistake, does that mean you have to click it again to remove it, and your code works that way and you accidentally click the same element twice you could lose your selection.

    The only way to appropriately do this is the way that microsoft has provided by using the ctrl + click methodology

  21. #21
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Well, you can always click the wrong item, then reclick. How does Ctrl-Click prevent clicking the wrong item?

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Well guys! I have written code that states the user must select an item from each box or a message is displayed:

    You haven't selected any items and
    Please select a destination city or Please select a departure city

    So right now my array won't even work because I don't know how to select an item from each box. I can only select one item and that's one total. I need one from each list box. How??
    Twisted

  23. #23
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    salvelinus,

    No, ctrl + click does not prevent you from accidentally clicking on the same element twice, but it does give you a visual indicator that you have deselected an item in the list. While code could be written to update the listview's selecteditems collection after each click, in the end is it really worth that work, just to circumvent the built-in functionality already provided?

    twisted,

    if your array is a two element array, the best way to populate it is on the SelectedIndexChanged event of each of the two listviews.

    VB Code:
    1. Private Cities(2) As String
    2.     Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    3.         If Me.ListView1.SelectedItems.Count > 0 Then
    4.             Cities(0) = Me.ListView1.SelectedItems(0).Text
    5.         End If
    6.     End Sub
    7.     Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
    8.         If Me.ListView1.SelectedItems.Count > 0 Then
    9.             Cities(1) = Me.ListView2.SelectedItems(0).Text
    10.         End If
    11.     End Sub

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Then I would do this for my message boxes to show?
    VB Code:
    1. If Cities(0) Then
    2. MessageBox. Show("You need to select a Destination City", "Error")
    3. Else
    4. If Cities(1) Then
    5. MessageBox.Show("You need to select a Departure City", "Error")
    Is this right or am I way off track??
    Twisted

  25. #25
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    close, but should be like this:

    VB Code:
    1. If Cities(0).Length = 0 Then
    2.     MessageBox.Show("You need to select a Destination City", "Error")
    3. ElseIf Cities(1).Length = 0 Then
    4.     MessageBox.Show("You need to select a Departure City", "Error")
    5. End If

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