|
-
Jun 23rd, 2004, 09:57 AM
#1
Thread Starter
Addicted Member
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:
Private Sub btnWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhile.Click
Dim sbMessage As New System.Text.StringBuilder
Dim liTemp As ListViewItem
If lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do While loop and you chose the following items:").Append(vbCrLf)
For Each liTemp In lstParts.Items
If liTemp.Selected = True Then sbMessage.Append(liTemp.Text).Append(vbCrLf)
Next
txtLoop.Text = (sbMessage.ToString)
Else
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
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???
-
Jun 23rd, 2004, 10:13 AM
#2
Hyperactive Member
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:
Private Sub btnWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhile.Click
Dim sbMessage As New System.Text.StringBuilder
Dim Indx As Int32 = 0
If lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do While loop and you chose the following items:").Append(vbCrLf)
Do While Indx <= Me.ListView1.SelectedIndices.Count - 1
sbMessage.Append(Me.ListView1.SelectedItems(Indx).Text).Append(vbCrLf)
Indx += 1
Loop
txtLoop.Text = (sbMessage.ToString)
Else
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
-
Jun 23rd, 2004, 11:51 AM
#3
Thread Starter
Addicted Member
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!
-
Jun 23rd, 2004, 12:00 PM
#4
Thread Starter
Addicted Member
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?
-
Jun 23rd, 2004, 12:18 PM
#5
Hyperactive Member
yes, that should work, let me see what you have there
-
Jun 23rd, 2004, 12:34 PM
#6
Thread Starter
Addicted Member
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:
Private Sub btnUntil_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUntil.Click
Dim sbMessage As New System.Text.StringBuilder
Dim Indx As Int32 = 0
If lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do Until loop and you chose the following items:").Append(vbCrLf)
Do Until Indx >= Me.lstParts.SelectedIndices.Count - 1
sbMessage.Append(Me.lstParts.SelectedItems(Indx).Text).Append(vbCrLf)
Indx += 1
Loop
txtLoop.Text = (sbMessage.ToString)
Else
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
-
Jun 23rd, 2004, 12:45 PM
#7
Hyperactive Member
just get rid of the "="
VB Code:
Private Sub btnUntil_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUntil.Click
Dim sbMessage As New System.Text.StringBuilder
Dim Indx As Int32 = 0
If lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do Until loop and you chose the following items:").Append(vbCrLf)
Do Until Indx > Me.lstParts.SelectedIndices.Count - 1
sbMessage.Append(Me.lstParts.SelectedItems(Indx).Text).Append(vbCrLf)
Indx += 1
Loop
txtLoop.Text = (sbMessage.ToString)
Else
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
-
Jun 23rd, 2004, 12:52 PM
#8
Thread Starter
Addicted Member
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.
-
Jun 23rd, 2004, 02:21 PM
#9
Hyperactive Member
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:
Private Sub btnUntil_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUntil.Click
Dim sbMessage As New System.Text.StringBuilder
Dim Indx As Int32 = 0
If Me.lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do Until loop and you chose the following items: ")
Do Until Indx > Me.lstParts.SelectedIndices.Count - 1
If Indx = 0 Then
sbMessage.Append(Me.lstParts.SelectedItems(Indx).Text)
ElseIf Indx = Me.lstParts.SelectedIndices.Count - 1 Then
sbMessage.Append(" and ").Append(Me.lstParts.SelectedItems(Indx).Text)
Else
sbMessage.Append(", ").Append(Me.lstParts.SelectedItems(Indx).Text)
End If
Indx += 1
Loop
txtLoop.Text = (sbMessage.ToString)
Else
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
-
Jun 24th, 2004, 06:54 AM
#10
Thread Starter
Addicted Member
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:
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
' Declare variables
Dim sbMessage As New System.Text.StringBuilder
Dim liTemp As ListViewItem
Dim Indx As Int32 = 0
' If then statement with For...Next loop
If lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do Next loop and you selected:")
For Each liTemp In lstParts.Items
If liTemp.Selected = True Then
sbMessage.Append(liTemp.Text)
ElseIf Indx = Me.lstParts.SelectedIndices.Count - 1 Then
sbMessage.Append(" and ").Append(Me.lstParts.SelectedItems(Indx).Text)
Else
sbMessage.Append(", ").Append(Me.lstParts.SelectedItems(Indx).Text)
End If
Next
txtLoop.Text = (sbMessage.ToString)
Else
' If no items are selected show error message
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
-
Jun 24th, 2004, 07:07 AM
#11
Hyperactive Member
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:
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
' Declare variables
Dim sbMessage As New System.Text.StringBuilder
Dim liTemp As ListViewItem
If Me.lstParts.SelectedItems.Count > 0 Then
sbMessage.Append("This is a Do Until loop and you chose the following items: ")
For Each liTemp In Me.lstParts.Items
If liTemp.Selected = True Then
If liTemp.Index = Me.lstParts.SelectedItems(0).Index Then
sbMessage.Append(liTemp.Text)
ElseIf liTemp.Index = Me.lstParts.SelectedItems(Me.lstParts.SelectedIndices.Count - 1).Index Then
sbMessage.Append(" and ").Append(liTemp.Text)
Else
sbMessage.Append(", ").Append(liTemp.Text)
End If
End If
Next
txtLoop.Text = (sbMessage.ToString)
Else
MessageBox.Show("You did not select any items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
Last edited by CyberHawke; Jun 24th, 2004 at 09:21 AM.
-
Jun 24th, 2004, 07:57 AM
#12
Thread Starter
Addicted Member
Didn't work b/c of ListView1?? what should thatbe?
-
Jun 24th, 2004, 08:20 AM
#13
Hyperactive Member
I edited the code, it should work now, but I left it in my last code post
-
Jun 24th, 2004, 08:59 AM
#14
Thread Starter
Addicted Member
I tried that myself thinking it would fix it but I get:
lstParts is not a member of System.Windows.Forms.Listview
???
-
Jun 24th, 2004, 09:22 AM
#15
Hyperactive Member
Fixed, refer back to my code again...
That's what I get for trying to convert my code in this window
-
Jun 24th, 2004, 09:28 AM
#16
Thread Starter
Addicted Member
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.
-
Jun 24th, 2004, 11:33 AM
#17
I wonder how many charact
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)
-
Jun 24th, 2004, 11:45 AM
#18
Thread Starter
Addicted Member
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?
-
Jun 24th, 2004, 12:40 PM
#19
Frenzied Member
When you select one item, store it's value in a variable. Then do the same for the other item. Then do the calculation.
-
Jun 24th, 2004, 12:45 PM
#20
Hyperactive Member
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
-
Jun 24th, 2004, 01:18 PM
#21
Frenzied Member
Well, you can always click the wrong item, then reclick. How does Ctrl-Click prevent clicking the wrong item?
-
Jun 24th, 2004, 04:13 PM
#22
Thread Starter
Addicted Member
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??
-
Jun 25th, 2004, 07:52 AM
#23
Hyperactive Member
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:
Private Cities(2) As String
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
If Me.ListView1.SelectedItems.Count > 0 Then
Cities(0) = Me.ListView1.SelectedItems(0).Text
End If
End Sub
Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
If Me.ListView1.SelectedItems.Count > 0 Then
Cities(1) = Me.ListView2.SelectedItems(0).Text
End If
End Sub
-
Jun 25th, 2004, 09:04 AM
#24
Thread Starter
Addicted Member
Then I would do this for my message boxes to show?
VB Code:
If Cities(0) Then
MessageBox. Show("You need to select a Destination City", "Error")
Else
If Cities(1) Then
MessageBox.Show("You need to select a Departure City", "Error")
Is this right or am I way off track??
-
Jun 25th, 2004, 09:09 AM
#25
Hyperactive Member
close, but should be like this:
VB Code:
If Cities(0).Length = 0 Then
MessageBox.Show("You need to select a Destination City", "Error")
ElseIf Cities(1).Length = 0 Then
MessageBox.Show("You need to select a Departure City", "Error")
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|