Results 1 to 17 of 17

Thread: ListView [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    Question ListView [RESOLVED]

    I have a listview box with 10 items in it. How do I create a statement saying if you select no items then show message, if you select items then message show, the items that were selected. Thanks! I know the command for show message, but can't figure out the if parts.

    For emaple:

    If lstParts.SelectedItems = None selected then display an Error Message

    If lstParts.SelectedItems are selected then display a Message with those selected items.
    Last edited by twisted; Jun 23rd, 2004 at 09:41 AM.
    Twisted

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

    VB Code:
    1. Dim sbMessage As System.Text.StringBuilder
    2. Dim liTemp As ListViewItem
    3. If lstParts.SelectedItems.Count > 0 Then
    4.     sbMessage.Append("You chose the following items").Append(vbCrLf)
    5.     For Each liTemp In lstParts.Items
    6.          If liTemp.Selected = True Then sbMessage.Append(liTemp.Text).Append(vbCrLf)
    7.     Next
    8.     MessageBox.Show(sbMessage.ToString)
    9. Else
    10.     MessageBox.Show("Show some other message")
    11. End If

    Hope this helps
    Last edited by CyberHawke; Jun 21st, 2004 at 11:03 AM.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    You can test the condition in this way:

    Code:
     If Me.LswScadenze.SelectedItems.Count > 0 Then
    
    else  
          
     End If
    If Count > 0 at least one item is selected.

    This solve your problem?
    Good job
    Live long and prosper (Mr. Spock)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    First part works great! But how do I get the second message (when items are selected) to show the items that have been selected. Say my items are arm, leg, and hand. And when I select arm and hand I want the message to say You have selected arm and hand.
    Twisted

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I updated my original message, check it out

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by CyberHawke
    simple,

    VB Code:
    1. Dim sbMessage As System.Text.StringBuilder
    2. Dim liTemp As ListViewItem
    3. If lstParts.SelectedItems.Count > 0 Then
    4.     sbMessage.Append("You chose the following items").Append(vbCrLf)
    5.     For Each liTemp In lstParts.Items
    6.          If liTemp.Selected = True Then sbMessage.Append(liTemp.Text).Append(vbCrLf)
    7.     Next
    8.     MessageBox.Show(sbMessage.ToString)
    9. Else
    10.     MessageBox.Show("Show some other message")
    11. End If

    Hope this helps
    Didn't work out right! The first part did, but when I select items I end up with an error.

    An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

    Additional information: Object reference not set to an instance of an object.
    Twisted

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Twisted

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Ok this is what I got so far, but need the items in my ListView that are selected to be displayed in my text box. Here's my code..

    VB Code:
    1. If lstParts.SelectedItems.Count = 0 Then
    2.             MessageBox.Show("You did not select any items", "Error")
    3.             Exit Sub
    4.         End If
    5.         If lstParts.SelectedItems.Count > 0 Then
    6.             txtLoop.Text = "You have selected: [COLOR=RED]THE ITEMS THAT WERE SELECTED[/COLOR]."
    7.         End If
    Twisted

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Sorry, I left out a key word in one of the declarations
    The word new is required when declaring an instance of the stringbuilder class, or any other for that matter...

    VB Code:
    1. Dim sbMessage As New System.Text.StringBuilder
    2. Dim liTemp As ListViewItem
    3. If lstParts.SelectedItems.Count > 0 Then
    4.     sbMessage.Append("You chose the following items").Append(vbCrLf)
    5.     For Each liTemp In lstParts.Items
    6.          If liTemp.Selected = True Then sbMessage.Append(liTemp.Text).Append(vbCrLf)
    7.     Next
    8.     MessageBox.Show(sbMessage.ToString)
    9. Else
    10.     MessageBox.Show("Show some other message")
    11. End If

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by CyberHawke
    Sorry, I left out a key word in one of the declarations
    The word new is required when declaring an instance of the stringbuilder class, or any other for that matter...

    VB Code:
    1. Dim sbMessage As New System.Text.StringBuilder
    2. Dim liTemp As ListViewItem
    3. If lstParts.SelectedItems.Count > 0 Then
    4.     sbMessage.Append("You chose the following items").Append(vbCrLf)
    5.     For Each liTemp In lstParts.Items
    6.          If liTemp.Selected = True Then sbMessage.Append(liTemp.Text).Append(vbCrLf)
    7.     Next
    8.     MessageBox.Show(sbMessage.ToString)
    9. Else
    10.     MessageBox.Show("Show some other message")
    11. End If
    Now it works perfect! The only problem I have is I want to keep the "Show some other message", but not the "You chose the following items". Instead of a Message Box for this one I want it to show up in a text box. Saying "You have selected: arm, hand, and foot." If that's what they select. Would I just change the one line:
    MessageBox.Show(sbMessage.ToString)??

    EDIT: I went ahead and put txtLoop.Text = (sbMessage.ToString)
    Now that it works I don't think I can use it. I believe I have to return these values using Do While, Do Until, and For Next statements.
    It works great! One other question is DOES THIS use a text box for the loop results, and if so, how did they make it that size, b/c mine won't let me change the height??
    Last edited by twisted; Jun 22nd, 2004 at 06:59 AM.
    Twisted

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Another problem I found:

    If I select any parts then my Message works, but after I select parts then go back and don't select parts my Message doesn't come up. It's like it is storing the times Parts have been selected and after the first time it's greater then 0 so it doesn't work.
    Twisted

  12. #12
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    one word here "CODE" as in I need to see some...

    If you are attempting to not have any items in the list selected then I suggest you try clearing the selecteditems collection.

    VB Code:
    1. lstParts.SelectedItems.Clear

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    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

    Right now, how do I make this a Do...While statement while mainting the same stuff? Also, how do I get my text box for the Loop Results to look like this? Is that even a text box??
    Twisted

  14. #14
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    yeah, it's a TextBox inside a GroupBox with the enabled property set to false and the backcolor property set to Control

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Well I got the GroupBox and Textbox inside it but I can change the height of the Textbox it's just default right now, but the width is perfect and let me adjust it fine.
    Twisted

  16. #16
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Set the multiline property to true

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    HELL YEAH!! Thanks man!
    Last edited by twisted; Jun 23rd, 2004 at 09:41 AM.
    Twisted

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