Solved

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

Posted on 2004-10-11
2
184 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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now