Results 1 to 7 of 7

Thread: How to enable a button when an item is selected in a listBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    13

    How to enable a button when an item is selected in a listBox

    I'm stuck on a part of my assignment and wondering if someone could point me in the right direction.

    Here's my problem, I have set my listBox button_up to disabled, but I need it to enable when there is at least 1 selected item in the listBox, here's my code for the remove button:

    VB Code:
    1. Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
    2.  
    3.       If listBox.SelectedItems ' need selectedItem to = something??? Then
    4.  
    5.             Me.btnRemove.Enabled = True
    6.         End If
    7.  
    8.         Dim item As String = listBox.SelectedItem
    9.         Dim index As Integer = listBox.SelectedIndex
    10.         listBox.Items.RemoveAt(index)
    11.         listBox.Items.Insert(index - 1, item)
    12.         listBox.SelectedIndex = index - 1
    13.  
    14. end sub

    Thanks for any help.
    Last edited by JENKINS; Dec 9th, 2006 at 05:05 PM.

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: How to enable a button when an item is selected in a listBox

    If nothing is selected, the SelectedIndex property of the listbox is -1.
    VB Code:
    1. If ListBox1.SelectedIndex <> -1 Then
    2.             Btn.Enabled = True
    3.         End If
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: How to enable a button when an item is selected in a listBox

    Thanks for the quick response, bur the code you gave doesn't enable the button??

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: How to enable a button when an item is selected in a listBox

    You have to replace "Btn" with the name of your correct button. Do you want to enable button_up or btnRemove from inside btnRemove_click (the latter seems kind of impossible).
    VB 2005, Win Xp Pro sp2

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: How to enable a button when an item is selected in a listBox

    sorry about the mistake, I want to enable button_up, here's my code again:

    VB Code:
    1. Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
    2.  
    3.         Dim item As String = listBox.SelectedItem
    4.         Dim index As Integer = listBox.SelectedIndex
    5.         listBox.Items.RemoveAt(index)
    6.         listBox.Items.Insert(index - 1, item)
    7.         listBox.SelectedIndex = index - 1
    8.         If listBox.SelectedIndex <> -1 Then
    9.             Me.btnUp.Enabled = True
    10.         End If

    It still doesn't work?

  6. #6
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: How to enable a button when an item is selected in a listBox

    This approach is bound to throw exceptions but you fill face them as you go on(and learn to avoid them). The reason it is not working is because you are enabling the button if some condition is met when the very same button is clicked. How can you enable a button if it has to be clicked first in order to be enabled?

    Put the code from your last post in the click event of some other button. I think in your design it should be the btnRemove's click event.
    VB 2005, Win Xp Pro sp2

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: How to enable a button when an item is selected in a listBox

    Yeah, that does sound a lot better, will give it a try, thanks for your help half.

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