Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a listbox on my form and I add entries to it throughout the program. Normally the listbox Visible property is set to FALSE. When it is FALSE the SELECTITEDITEM only returns the first entry. When I change it to TRUE I am able in my program to select the last entry that was added. The VISABLE status seems to be the only difference.

Within my program can I select an item, even if the listbox is set to NOT Visable?

What I have tried:

Within References or Control documentation I have not been able to find any qualifications on a VISABLE function on a LISTBOX.
Posted
Comments
Ralf Meier 20-Nov-23 15:21pm    
what is thge sense of using an unvisible Listbox ?
and ... do you allways want to get the last Item of the Entries inside yor Listbox ?
In fact you could get any Item of your Listbox at any time - independant of the Listbox.Visible-State
Andre Oosthuizen 20-Nov-23 15:22pm    
Soooo, what code do you use to select items in your listbpx when it is NOT visible, this will help a lot to figure out what you are trying to do...
Dave Kreskowiak 20-Nov-23 20:34pm    
It sounds like your "data model" is not a model at all and you're using UI controls to store data. That's a really bad idea.

There is no point at all, even for "debugging purposes" to use hidden UI controls to store and "select data".
Richard MacCutchan 21-Nov-23 6:14am    
My previous comment has been removed. However, I ran a quick test, and the ListBox behaves exactly the same whether it is visible or not. And in previous applications that I have created I know that a Windows control's general behaviour is unaffected bu iys visibilitty. That is, of course apart from the fact that you may not be able to see it.

If you use data binding with the ListBox to a List, you don't need to access the ListBox to access the collection. The ListBox becomes a 'view' to the data. This means that you can access the collection of data directly.

You can read more on how to do this here:
* Bind ComboBox or ListBox Control to Data - Windows Forms .NET Framework | Microsoft Learn[^]
* Best Practice for Binding WinForms List Controls - Code Project[^]
 
Share this answer
 
you haven't answered my question - but independant here is an answer for you :
VB
Dim l As Integer = ListBox1.Items.Count
Dim myEntry as string = ListBox1.Items(l-1)

this gives you the last Entry from your Listbox.
to get any else you should realize that the Entry-List is Zero-based - that means that the first entry stays a Index=0 and so on ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900