Vb6 how many items in a listbox




















Follow us! Get the Latest Bytes Updates. By using this site, you agree to our Privacy Policy and Terms of Use. Mar 10 '08 reply Message. Cancel Changes. Count is always 1 2 posts views Thread by ohadasor last post: by.

CollectionBase in an inherited class 2 posts views Thread by GoCoogs last post: by. General The research path of clustering reply views Thread by goatbishop last post: by.

General A tool to try open-source projects quickly reply views Thread by Trystan last post: by. End With. Close 1. At design time, you can access the property sheet for the ListBox and add items via the List property, as shown in the screen-shot below. To add multiple items to the list, you press Ctrl-Enter to move to the next line in the list just pressing Enter will close the property. You can also add to the ItemData property in this manner however, you must specify items for List before specifying items for ItemData.

Placing items in the list this way eliminates the need to use the AddItem method. You would typically add items to this list in this manner if the list is to remain static i. As mentioned in the beginning of this document, you can reference any element of the List property array or ItemData property array regardless of whether or not that item is selected with a valid index reference, i. List IndexValue.

ItemData IndexValue. A valid index reference is an integer between zero and one less than the number of items stored in the ListBox. For example, if there are 5 items in theListBox, a valid index reference is a number between 0 and 4. For example, the following code prints the contents of the List and ItemData arrays on the background area of the form:. Dim intX As Integer. ListCount — 1. List intX ,. ItemData intX. Typically, you only care about what items in the listbox were selected by the user.

The issue of whether or not the user is permitted to select multiple items from the ListBox then comes up. You determine this at design-time by setting the MultiSelect property in the property sheet of the ListBox.

By default, the MultiSelect property is set to "0 — None", meaning that only one item can be selected from the ListBox.

If the user selects one item, then clicks a different item, the item previously selected is de-selected. If you want to allow the user to select more than one item from the ListBox, you can set the MultiSelect property to either "1 — Simple", or "2 — Extended". If MultiSelect is set to None :. When you are only allowing the user to select one item from the list, there are two ways to determine which item is selected.

Suppose you have a ListBox and a command button on your form. When the user clicks the command button, you want to process the item that the user has selected from the ListBox. As an example, to store the user's selection in a string variable called strListSel, you could place this code behind the command button:. The Text property of the ListBox stores the contents of the List array item that is selected. Alternatively, you can use the ListIndex property to access the contents of the List item that is selected.

The ListIndex property is an integer value representing the index of the currently selected item. Therefore, the following statement achieves the same result as the one above:. List lstFood. To access the contents of the ItemData element corresponding to the item in the list that is selected, you must use the ListIndex property.

Suppose you wanted to store the ItemData contents corresponding to the user's selection in a long variable called lngCalCount. You could place this code behind the command button:. Pre-Selecting an Item in a ListBox :. If you load a ListBox but do not pre-select an item from the list in code, then by default, the value of ListIndex will be 0. When ListBox is first displayed on the form, the first item in the list will show the focus rectangle, but will not be highlighted.

To pre-select an item in code so that the user is presented with a default selection , use the following:. The above code highlights and pre-selects the first item in the ListBox. You could set the ListIndex to any other valid index number. If you wish to NOT pre-select an item from the ListBox and sometimes this will be the case, because you do not want to default a selection for the user , you can set the ListIndex property to —1.

A value of —1 for ListIndex indicates that no item is selected:. If you do the above, you must be careful about subsequent references to ListIndex, because if the user never selects an item from the ListBox, ListIndex will remain at —1 and therefore would not refer to a valid element of the List or ItemData arrays, and statements like the ones we looked at earlier would cause a run-time error:.

You could avoid this problem by testing ListIndex for a value of If lstFood. End If. If MultiSelect is set to Simple or Extended :. Each element of Selected stores a Boolean value of True or False, indicating whether or not the corresponding List item is selected.

In code, the Selected property is referenced as follows:. Selected IndexValue. Let's say your listbox is called ListBox1 and textbox is TextBox1. Then the code you need is TextBox1. Improve this answer. Mennan Kara M. With the ASP. Net tag, I am pretty sure this is WebForms. I would like to present an answer for the people who uses WPF.

TT7 TT7 21 8 8 bronze badges. Sign up or log in Sign up using Google. Net developer so it should be clear enough to regulars without you pointing it out. I was a VB6 developer, amongst other platforms, some 15 years ago but moved with the times to more contemporary technologies.

Nevertheless, I am well aware that there is a train of thought amongst VB6 developers that thinks it more appropriate to use the AddItem method to populate Combos and ListBoxes than to use data binding. I prefer to treat these controls as a view to a datasource rather than the datasource itself. It seems that someone at Microsoft had the same idea at some point and catered for it when designing the controls. The advantages of this approach become apparent when you move away from the old technology to the new technology.

Indeed, when you revisit the VB6 Framework after a decade working with the. Net to replace it. After all, the fundamental principle behind MVC architecture is one of separation of view from content so treating Combos and ListBoxes in much the same way is compatible with that principle. Go to the url that you posted and click on the Properties link and then the DataSource property link.

It briefly explains an alternative way of populating a ListBox control other than via the AddItem method. When you need to change a ListBox content you can change the bound data source and call the. Refresh method on the control. The OP asked how to replace an entry in the list. AddItem will add an entry. There is no way to control where in the list the new item will appear. It will not satisfy the requirements as stated by the OP.

Furthermore, neither will an approach that replaces the DataSource. Not the most desirable thing to do but would this not allow what the OP has indicated he wants? I may be wrong, but there seem to be many questions asked in this forum that generate masses of solutions with sometimes widely differing approaches, perhaps caused by personal preference, education, experience etc. How to I set it dynamically? Of course the only person who can confirm that our interpretation of the question is correct is the original poster!

So craigvbnewb can you please put us out of our misery. The OP wants to change the list based on text input. The answers given by all of: List2. In VB6 databinding was always a bit questionable except in the simplest of cases because it was so hard to get into the process. It was very clear that for single selection the correct answer should be List2. For multiple selection the answer should be the one I suggested. I prefer to leave that to folks who are still using VB6 in Folks like your good self.

Net has been around for over a decade now. Its a brilliant Framework. You would do well to discover that one day. But that was 15 years ago. Net binding is very slick and lends itself very well to ORM architectures. People gave the correct answers already and I did tell your assumption was wrong. You should stop right there. Is that a correct way to answer a VB6 question? It just shows your VB6 problem solving skill is very limited.

Why yes, there is. And yes, you can clear the List Box and then repopulate it from a separate list for example, a string array , using the AddItem method repeatedly. But why would you, when a single statement posted repeatedly in this thread will have the same effect for vastly lower cost?

You will see that the DataSource property takes a recordset. Perhaps you should read that page, which makes it clear that a List Box does not require a data source. Instead, the List Box can simply display the contents of a list of strings — in other words, a local data source.

That is a correct statement. It is read only. Please note that there is a difference between the Text property and the Item property. You can change the value of an item, because that is not read only. Michael, I assume you read this from the web as opposed to the mailing list?



0コメント

  • 1000 / 1000