Solved

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

Posted on 2004-10-11
2
188 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

838 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