Solved

Need listbox event to determine when an item has been added or removed

Posted on 2004-10-11
2
189 Views
Last Modified: 2010-05-18
Not sure why I can't find this in the docs or online - maybe I'm just looking in the wrong places. Anyway, here's the deal -

I'm building a wizard-type application. On one page, a listbox is presented with small buttons to add and delete directories from the listbox. Directories might also be added to this box from other portions of the wizard.

When the user first enters this page, I make a check to see if entries exist in the listbox, and if not, the NEXT button is disabled. The user can now add/delete directories - whenever anything is added or delete, I need to check and see if I should enable the NEXT button to allow them to continue.

Now I suppose I could code these checks into the add, delete and other buttons, but that's seems redundent, hard to trace, and error prone. What I want to do is have an event fire every time an item is added or removed from the listbox.

The SelectedItemChanged event does not work, because in most cases (in this app anyway) nothing is selected to begin with, so adding or removing an item doesn't cause the event to fire until you select something - not what I want.

Is there an event that fires when I do this?
myListBox.Add("Fire Something")
0
Comment
Question by:toddhd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 50 total points
ID: 12282328
You can use this:

Public Class MyListBox
    Inherits System.Windows.Forms.ListBox

#Region " Código generado por el Diseñador de componentes "

Public Sub New(Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Requerido para la compatibilidad con el Diseñador de composiciones de clases Windows.Forms
        Container.Add(me)
End Sub
Public Sub New()
        MyBase.New()

        'El Diseñador de componentes requiere esta llamada.
        InitializeComponent()

        'Agregar cualquier inicialización después de la llamada a InitializeComponent()

End Sub

    'Component reemplaza a Dispose para limpiar la lista de componentes.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Requerido por el Diseñador de componentes
    Private components As System.ComponentModel.IContainer

    'NOTA: el Diseñador de componentes requiere el siguiente procedimiento
    'Se puede modificar utilizando el Diseñador de componentes.
    'No lo modifique con el editor de código.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region


    Private pItemsCount As Integer
    Public ReadOnly Property ItemsCount() As Integer
        Get
            Return Me.pItemsCount
        End Get
    End Property

    Private WriteOnly Property NewItemsCount() As Integer
        Set(ByVal Value As Integer)
            Me.pItemsCount = Value
            RaiseEvent ItemsCountChanged()
        End Set
    End Property

    Public Sub RemoveAt(ByVal index As Integer)
        Try
            Me.Items.RemoveAt(index)
            Me.NewItemsCount = Me.Items.Count
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

    Public Event ItemsCountChanged()
End Class
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12282342
You must use the RemoveAt(index as integer) method of the custom listBox to remove your items, and you can catch the event in the place you use this listBox
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question