473,856 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to detect when items are added to Combobox/Listbox

Don
Is there any way to detect when an item has been added to the Items
collection of a combobox or listbox? I am inheriting a Combobox and want to
validate items before they are added to the combobox, but I can't find
anything that will let me do that.

- Don
Nov 21 '05 #1
9 7328
Don,
Is there any way to detect when an item has been added to the Items
collection of a combobox or listbox? I am inheriting a Combobox and want
to
validate items before they are added to the combobox, but I can't find
anything that will let me do that.


This sounds for me as a strange question, where are those Items comming
from?

Cor
Nov 21 '05 #2
Don
The combobox is populating itself in its constructor. However, the VB.NET
IDE, for some reason, thinks it's a good idea to append blank items to the
end of the combobox. It keeps adding code like this to the Windows Code
section in the form:

Me.MyCustomComb obox.Items.AddR ange(New Object() {""})

Almost every time I make a change on the form and recompile it, VB decides
to add another blank line to the combobox. And then another. And another.
I clear out the Items collection in the Properties window, but the blank
lines keep coming back. I can't figure out why it's doing this, so I'm just
trying to come up with a work around now to prevent that line of code from
working.

- Don
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...

Don,

This sounds for me as a strange question, where are those Items comming
from?

Cor
Is there any way to detect when an item has been added to the Items
collection of a combobox or listbox? I am inheriting a Combobox and want to
validate items before they are added to the combobox, but I can't find
anything that will let me do that.

Nov 21 '05 #3
Don,

Did you delete that line already, for me that never did harm at all.

When it is a two times inherited combobox look than as well in that
baseclass.

Cor
Nov 21 '05 #4
Don
I have tried deleting that line in the Form's code, but VB keeps putting it
back.

- Don
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ua******** ********@TK2MSF TNGP12.phx.gbl. ..
Don,

Did you delete that line already, for me that never did harm at all.

When it is a two times inherited combobox look than as well in that
baseclass.

Cor

Nov 21 '05 #5
Can we see the code for the custom combo box? (Or, at least, the parts
that really count)

It might be a bug in your implementation that's causing the designer to
add that line of code (but I doubt it).

I've never run into this problem before. Then again, I've never
subclassed a combo box before :)

Cheers,
-Adam.
Don wrote:
The combobox is populating itself in its constructor. However, the VB.NET
IDE, for some reason, thinks it's a good idea to append blank items to the
end of the combobox. It keeps adding code like this to the Windows Code
section in the form:

Me.MyCustomComb obox.Items.AddR ange(New Object() {""})

Almost every time I make a change on the form and recompile it, VB decides
to add another blank line to the combobox. And then another. And another.
I clear out the Items collection in the Properties window, but the blank
lines keep coming back. I can't figure out why it's doing this, so I'm just
trying to come up with a work around now to prevent that line of code from
working.

- Don
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Don,

This sounds for me as a strange question, where are those Items comming
from?

Cor

Is there any way to detect when an item has been added to the Items
collection of a combobox or listbox? I am inheriting a Combobox and
want
to
validate items before they are added to the combobox, but I can't find
anything that will let me do that.


Nov 21 '05 #6
Don
"Adam Goossens" <ad***********@ gmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..

Can we see the code for the custom combo box? (Or, at least, the parts
that really count)

It might be a bug in your implementation that's causing the designer to
add that line of code (but I doubt it).

I've never run into this problem before. Then again, I've never
subclassed a combo box before :)

Okay, here's how it works. I have a class library within which I have
created a custom combobox called EnumCombobox. It inherits from
System.Windows. Forms.Combobox. Now, this EnumCombobox is declared
MustInherit.

I have a second custom combobox class that inherits EnumCombobox. In its
constructor it calls a method of the base class (EnumCombobox) telling it to
populate the combobox.

The point of this arrangement was to create multiple custom comboboxes, one
for each of several custom Enums I have made for my project. The comboboxes
actually display the Enum names with spaces inserted before each capital
letter, or something like that, and have an option to display a blank item.

Here is how one of the second custom comboboxes looks. The only code I
added was the call to Me.Populate() in the constructor. The second
argument, a boolean, tells the Populate() method to insert a blank line. :
--- CODE START ---
Public Class ProcedureToothT ypeComboBox
Inherits EnumComboBox

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call
Me.Populate("Ve gaBizObj.Enumer ations+Procedur eToothType", True)

End Sub

'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Component Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
components = New System.Componen tModel.Containe r
End Sub

#End Region
End Class
--- CODE END ---
The Populate() from the base class looks like this. The code calls a
FillList() method which actually puts items in the list. The GetList()
method just retrieves a collection of objects representing the various
members of the Enum:
--- CODE START ---
Friend Sub Populate(ByVal enumName As String, ByVal includeBlankIte m As
Boolean)
Dim selectedIndex As Integer = -1
Try

' Remember enum name
_EnumerationNam e = enumName
_IncludeBlankIt em = includeBlankIte m

' Prevent control from visually updating
Me.SuspendLayou t()

' If an enum name has been specified...
If _EnumerationNam e.Length > 0 Then

' Clear the list
Me.Items.Clear( )

' Fill in the control with enum values
FillList(GetLis t(_EnumerationN ame))

End If

Catch ex As Exception

' Log the error
LogError(ex)

Finally

' Allow control to visually update
Me.ResumeLayout ()

End Try

End Sub
--- CODE END ---
This is what FillList() looks like:
--- CODE START ---
Friend Sub FillList(ByVal nameValueCollec tion As Collection)
Dim item As NameValue
' If we are to include a blank item in the list, then do so.
If _IncludeBlankIt em Then Me.Items.Insert (0, String.Empty)

' If the collection we were given has something in it...
If Not IsNothing(nameV alueCollection) Then

' Loop thru the collection
For Each item In nameValueCollec tion

' Add the item to the list
Me.Items.Add(it em)

Next

End If
End Sub
--- CODE END ---
In the method above is the only line of code that adds a blank item to the
combobox. I'm guessing, maybe, when VB creates the objects on the form it
calls the constructor, which calls Populate(), which calls FillList()? And
maybe the GetList() is returning Nothing because the information it
retrieves is not available at that time, so it just adds the blank line and
that's it? I don't think this is the case because I've commented out that
line and it still does it. I've event commented out the call to the
Populate() method in the combobox that inherits EnumCombobox and I still get
extra blank items added after every compile.

There is also nothing special in the constructor for EnumCombobox. I
haven't modified it at all. I am simply at a loss here.

- Don

Nov 21 '05 #7
Don
I've figured out what is causing the appearance of a blank item in the
combobox, but I cannot figure out a way to prevent it.

When I compile my code, the constructor for the class is executed. This
means the following method is executed:
-- CODE START --
Friend Sub FillList(ByVal nameValueCollec tion As Collection)
Dim item As NameValue
' If we are to include a blank item in the list, then do so.
If _IncludeBlankIt em Then Me.Items.Insert (0, String.Empty)

' If the collection we were given has something in it...
If Not IsNothing(nameV alueCollection) Then

' Loop thru the collection
For Each item In nameValueCollec tion

' Add the item to the list
Me.Items.Add(it em)

Next

End If
End Sub
-- CODE END --
Since I set _IncludeBlankIt em to true before this gets called, VB executes
the Me.Items.Insert (0, String.Empty) code when it compiles the form. The
strange thing is that this is all the list contains. I've modified the
method so that the code to add the blank line is placed after the loop and
so that the blank line is only added if the list (Me.List) contains at least
one item, but the blank item is still always added even though it's the only
item in the list.

So, it looks like I have no choice but to remove the call to the method that
populates the combobox from the constructor and just make it public so that
the form can manually call it during its Load event or something. Unless
someone knows of a way to determine when code is being executed because of a
compile as opposed to normal execution, or if someone knows of a way to
prevent adding new Items to the list.

- Don
Nov 21 '05 #8
Don,

Are you sure that your "nameValueColle ction" is nothing and not is
"nameValuCollec tion.length (or count) = 0"

When you instance it with "new" than it is always something.

Cor
Nov 21 '05 #9
Don
Actually, I've discovered that, during the compile, the nameValueCollec tion
contains items in it, and they are added to the combobox's Item collection
along with the blank line. However, strangely enough, only the blank line
remains after the build; the others are discarded(?).

I've tested this by inserting Me.Items.Count instead of a blank line. Sure
enough, after a build, there is a line appended to the combobox with the
number of items the nameValueCollec tion is expected to have. I'm completely
at a loss to explain this strange behavior.

- Don
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:ed******** ******@TK2MSFTN GP12.phx.gbl...
Don,

Are you sure that your "nameValueColle ction" is nothing and not is
"nameValuCollec tion.length (or count) = 0"

When you instance it with "new" than it is always something.

Cor

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1781
by: Wally | last post by:
I need to browse all ITEMS of a ListBox and all ITEMS of a ComboBox. I wrote 2 distinct Sub that are almost identical. The difference is only the object type. How could I browsing ITEMS using only one Sub with object as parameter? Here, my two Subs. Thanks Private Sub AnalyzeListBox()
11
18385
by: Timo Kunze | last post by:
Hi! If you move the mouse over an item that's part of a treeview and wider than the treeview, a tooltip showing the full item text will be displayed. I try to do this for ListBoxes and ComboBoxes. For ListBoxes I got it working, but not for the ListBox part of ComboBoxes. Do you know any sample code? Thanks in advance! Timo
0
1698
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing the listbox with no luck and, because it is already bound, I can't just delete the item directly from the listbox without an error (because it already has a datasource, you are not allowed to delete/add anything to the listbox directly). The...
14
18774
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe is that as more and more are added, population of the list box takes longer and longer. ie the first 10th of the item set are added much much quicker than the last 10th. THis occurs with about 40,000 listbox items. My worry is the listbox may...
1
4726
by: Gian Paolo | last post by:
hi all i'm looking for a way to add 2 items in a combobox in the same line and hide one. i'm working on the code above but it returns me only the last value i entered and i do not know why.... *********** CLASS using System; using System.Collections.Generic;
0
1678
by: inandout | last post by:
Hi, I was wondering if anyone can help me with this. I have a listview that is specified to a size of: 348W x 308H When items are added to it, no problems. However when items continue to be added such that the list gets too large to be displayed, the scrollbars appear. Once this occurs, the last item though selected and focused (highlighted blue), will not appear within the viewable area since the listview requires a scrolldown. This...
11
14282
by: kimiraikkonen | last post by:
Hi, Vb.net 2005 express i've been working, the problem is: I use openfiledialog to browse files, i created some code to execute file with OK button, no problem. But if i press "cancel" button as the result of openfiledialog box, for example my filename's are added into listbox or my audio file is being played from the beginning. To explain better: openfileplaylist.ShowDialog()
3
4954
by: ags5406 | last post by:
I've a listbox that I add items to at different points in the code. It's height is such that only about 6 items can be displayed vertically. The first item added shows up in the top line, then the second item in the second line, etc. After 6 items have been added, the vertical scroll bar appears and new items continue to be added, but until the user actually moves the scrollbar down the box displays only the first 6 items. What I'd...
5
18803
by: Kevinp | last post by:
I've spent the last four hours Google searching for a way to disable items in a Combobox. I found one example in C++ which I can't get to work and another in C# that I couldn't get to work either. Does anyone have some good code for an owner-drawn combobox?
0
9916
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9762
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11056
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9531
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7093
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.