Results 1 to 10 of 10

Thread: Select and unselect a ListBox item?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question Select and unselect a ListBox item?

    I have a ListBox in my form.

    Everytime I select (single-click) on an item in the ListBox, the selected item will be highlighted.
    At the same time, "Me.ActiveControl Is ListBox" is true.

    How can I 'unselect' a selected item, and set "Me.ActiveControl Is ListBox" is FALSE?
    So that "Me.ActiveControl Is Not ListBox"?

    Please guide. Thank you.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    For #1, set .SelectedIndex to -1

    For #2, let's see...

    Me.ActiveControl = Nothing

    appears to do it.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I'm trying to execute the following event on a ListBox:
    Click on an item in ListBox will select (highlight) an item.
    Click on a selected (highlighted) item in ListBox will deselect (unhighlight) an item.
    Double click on an item will show a new form.

    The following are the codes I wrote:

    'Handle click event
    Private Sub lstEntry_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEntry.SelectedIndexChanged
    If lstEntry.SelectedItem = strActiveControlText Then
    lstEntry.ClearSelected()
    strActiveControlText = ""
    Me.ActiveControl = Nothing
    Else
    strActiveControlText = lstEntry.SelectedItem
    End If
    End Sub

    'Handle double click event
    Private Sub lstEntry_OnDoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEntry.DoubleClick
    'Create an instance of Edit Entry form
    Dim frmEditEntry As New frmEntry("Edit Entry", splitEntryName())

    'Show Edit Entry form
    frmEditEntry.ShowDialog()

    'Retrieve Entry
    subRetrieveEntry()
    End Sub


    My problem is, when an item is already selected, if I try to double click on the selected item, lstEntry_SelectedIndexChanged will be executed before lstEntry_OnDoubleClick is execute.
    How can I make it to only execute the lstEntry_OnDoubleClick funtion?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Without looking at your code and if you can allow muliselection then all what you have to do is :
    VB Code:
    1. ListBox1.SelectionMode = SelectionMode.MultiSimple
    and then just set doubl click event to the item was doubleclicked to show it up !

  5. #5

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I tried to set ListBox1.SelectionMode = SelectionMode.MultiSimple, but the problem never solved.

    Still the same.

    When I want to "double-click" on an item which is currently being clicked (highlighted),
    the single-click event will be executed first, then only the double-click event is executed.
    It seems like it triggers the double-click's first clicking.

    How can I solve this?

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Well in point of fact your code is looking at the .SelectedIndexChanged event, which isn't quite the same as .Click, so the behavior is exactly as you describe: when you click the first time, SelectedIndexChanged fires and Me.ActiveControl = Nothing. Then the second click hits, and the DoubleClick event fires. Then, if you double-click again, SelectedIndex does not change, so only DoubleClick fires.

    I'm having trouble understanding exactly what kind of behavior you want your app to have.

  7. #7

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I want my ListBox to act like this:

    when single-click on an item, will only select the item.

    when single-click on a selected item, will deselect the item.

    when double-click on an item, will show the item's details in a new form.

    when double-click on a selected item, will also show the item's details in a new form.

    So now forgetting of my code, how will you write the code for the above behaviours?

    Please guide. Thank you.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this now ...
    Attached Files Attached Files

  9. #9

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I tried to run your program.

    And I find that your program has the same problem as I.

    Try to run the program and select any one item. eg: "item1"

    now "item1" is selected.

    try double click on "item1".

    and you get the error.

    this is what i'm asking all the while.

    how can we solve this problem?

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I understand now why it's throwing that error , try to double click under those items .I'm sure will cause an error because no value was selected . You can overcome this problem as follow :
    Just replace this code with the old one .
    VB Code:
    1. Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    2.         If ListBox1.SelectedItem = Nothing Then
    3.             Exit Sub
    4.         Else
    5.             MessageBox.Show(ListBox1.SelectedItem.ToString)
    6.         End If
    7.     End Sub

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