Solved

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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

635 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