473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

programmatically set the SelectedItems for a ListBox

Hello all,

I need a way to read a previously saved list of customers and
programatically "Select" these items in an existing listbox. Something
to the effect of ListBox.SelectedItems = myCollection. Of course
SelectedItems is readonly.

I'm reading in from an XML file and I have that portion of the issue
resolved. Where I run into problems is how to get them all selected.

Any suggestions?

Jun 29 '07 #1
6 12997
"tommcd24" <to******@gmail.comschrieb
Hello all,

I need a way to read a previously saved list of customers and
programatically "Select" these items in an existing listbox.
Something to the effect of ListBox.SelectedItems = myCollection. Of
course SelectedItems is readonly.

I'm reading in from an XML file and I have that portion of the issue
resolved. Where I run into problems is how to get them all selected.

Any suggestions?

lbx.SetSelected(.., True)

Armin
Jun 29 '07 #2
Thanks Armin,

SetSelected wants an index and I'm difficulty translating the values
from my xml file which correspond to the ValueMember property of the
listbox items. I've looked into ListBox.FindString() and
ListBox.IndexOf() but both return -1. I'm guessing they must be
searching the DisplayMember property.

Any thoughts on how to translate from the ValueMember values to Index?
Jun 29 '07 #3
Does anyone have suggestions or sample code to do this? I need to be
able to Select listbox items based on the item value.

Thanks in advance,

Jul 3 '07 #4

"tommcd24" <to******@gmail.comwrote in message
news:11**********************@k29g2000hsd.googlegr oups.com...
Does anyone have suggestions or sample code to do this? I need to be
able to Select listbox items based on the item value.
I don't know if you're looking for a routine below or not to set the combox
to the display is display value based based on its item value or not, but it
might help you, possibly.
Sub SelectCBOBbyItem(ctrl as Combobox, someval as whatevertype)

dim i as Integer
For i = 0 To ComboBox1.Items.Count - 1

ComboBox1.SelectedIndex = i

If ComboBox1.SelectedValue = someval Then exit sub

Next

end sub

Jul 3 '07 #5

"Mr. Arnold" <MR. Ar****@Arnold.comwrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...
>
"tommcd24" <to******@gmail.comwrote in message
news:11**********************@k29g2000hsd.googlegr oups.com...
>Does anyone have suggestions or sample code to do this? I need to be
able to Select listbox items based on the item value.

Oops, you would be using the ctrl

call SelectCBOBbyItem(Combox1, value)

Sub SelectCBOBbyItem(ctrl as Combobox, someval as whatevertype)

dim i as Integer
For i = 0 To ctrl.Items.Count - 1

ctrl.SelectedIndex = i

If ctrl.SelectedValue = someval Then exit sub

Next

end sub
>
Jul 3 '07 #6
I was overcomplicating this issue. I was assuming that
ListBox.SelectedValue could only hold 1 value. On a lark I tried
repeated calls using that property and achieved the result I needed of
selecting multiple items based on the ValueMember. I'm including my
code below for others who may have similar situations.

Using xReader As XmlReader = XmlReader.Create(strFilePath & "\"
& strFileName)
xReader.ReadStartElement("Customers")
While xReader.Read()
If (xReader.IsStartElement()) Then
If (xReader.IsEmptyElement()) Then
Me.lstCustomers.SelectedValue = xReader(0)
End If
End If
End While
End Using

Jul 5 '07 #7

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

Similar topics

0
by: Josef Meile | last post by:
Hi I wanted to programmatically disable events like SelectedIndexChanged without using a boolean flag, which indeed I find a quick and dirty solution. So, I found a piece of code that do this...
11
by: Robin Tucker | last post by:
SelectedItems and SelectedIndices appear to be read-only, so I cannot remove a selected item. I need to do this to synchronise the selection in my list with the selection in other views (ie....
2
by: Mat | last post by:
in my program options, users can reorder DB grid columns( in listbox) Then, i want to programmatically simulate col move event and reorder the columns. Help plz
2
by: Mike | last post by:
Ok I'm having a problem with a listview. I have a listview on a WinForm called ProgramInformationFrm. I call this from from 2 other forms. One is ProgramSearch Frm, the other is OpenNCFrm. The...
0
by: Chris | last post by:
Hi, Can I add a new row to gridview programmatically, similar to ADD method of listbox ? Thanks a lot in advance.
2
by: ohadasor | last post by:
Even if I select several items, and I can see that several items are colored in blue. I can't get a list of all selected items. I also have listBox.SelectionMode = SelectionMode.MultiExtended; ...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance, it is GREATLY appreciated. I'm wondering if there is a way to duplicate a function in regedit of find and find next for keywords programmatically,...
2
by: Colin Williams | last post by:
Hi I am trying to pass the selected items from a multiselect listbox to a string array. Can any body give me a start. Thanks Colin Williams
13
ADezii
by: ADezii | last post by:
Recently, there have been several questions and much confusion concerning the Topic of Hyperlinks. Specifically, Users wanted to know how to retrieve a File Name from a FileDialog Box, copy the Name...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.