|
A ListBox holds a SelectedItems [^] collection that you can query;
Dim testBox As ListBox = New ListBox()
testBox.Parent = Me
testBox.Dock = DockStyle.Fill
testBox.SelectionMode = SelectionMode.MultiSimple
Me.Controls.Add(testBox)
Dim i As Integer
For i = 0 to 10
testBox.Items.Add ("test " & i)
Next i
testBox.SelectedItems.Clear()
testBox.SelectedItems.Add(testBox.Items(2))
testBox.SelectedItems.Add(testBox.Items(7))
For Each someItem in testBox.Items
If (testBox.SelectedItems.Contains(someItem)) Then
Debug.WriteLine(someItem & " was selected")
End If
Next someItem
For Each selectedItem In testBox.SelectedItems
Debug.WriteLine(selectedItem & " was selected")
Next
I are Troll
|
|
|
|
|
It's not really working, i get an error saying something about an enumerator and the list being changed (im pretty sure its not).
Also, i can't really use the SelectedItem collection because it would skip over the unselected items. I need to through ALL the items and check which ones are selected and which are not and return both results to me in the form of a messagebox, like so:
For Each someItem in testBox.Items
If testBox.SelectedItems.Contains(someItem) Then
MessageBox.Show(someItem & " is selected")
else
MessageBox.Show(someItem & " is not selected")
End If
Next someItem
[img]http://www.themightyatom.nl/stuff/tma_userbars.png[/img]
|
|
|
|
|
Hi,
ListBox.Items contains objects of arbitrary type (i.e. Object), although most often people just store strings in there.
So you can't call methods that don't exist for those items.
However you can take advantage of SelectedItems somehow. Maybe you could iterate over all items and for each of them check if they are contained in SelectedItems, the collection has a Contains() method for that. Warning: it will trick you if you happen to store value types in the ListBox as they get boxed and will not compare-equal by default.
|
|
|
|
|
Hmmm that actually makes sense, except for your warning.
Im gonna give it a try anyway.
Thanks to both of you.
Though if someone has a better/alternate solution, i'd like to hear about it.
Edit:
This is tough.
For Each Item As String In ListBox1.SelectedItems
ListBox1.SelectedItems.Contains(Item)
Next
This works but it skips over the unselected items. I need to get all the items so i can save them to a file to retrieve them later.
Hmmm, something just came up, let my try something else.
Virtual Space Shuttle Astronaut
modified on Wednesday, August 4, 2010 10:00 AM
|
|
|
|
|
Im having some troubles with this. Here's what i did:
First, i create a List of type string. This list only contains the selected items. This is done like so:
Dim SelectedItemsList As New List(Of String)
For Each SelectedItem As String In ListBox1.SelectedItems
SelectedItemsList.Add(TextBox1.Text)
Next
Now i need to compare the List with the actual listbox list. If item exists in both the list and the listbox, then this item was selected. If its not, then it was an unselected item.
Im trouble figuring that out. How do i check that?
If SelectedItemList = ListBox.Items Then
'Selected
Else
'Not selected
End If
Something like that?
Virtual Space Shuttle Astronaut
|
|
|
|
|
Hi,
1.
when you reply to your own message nobody gets notified, it is only by accident that I saw this. Didn't you get a MessageBox warning you?
2.
you don't need the List at all, this should work:
For Each item As String In ListBox1.Items
If ListBox1.SelectedItems.Contains(item) Then
' do something with the selected item (it should not change the ListBox)
Else
' do something with the unselected item (it should not change the ListBox)
Endif
Next
|
|
|
|
|
I actually do get an error saying that list has changed. It also says something about an enumerator. Does'nt make sense to me.
Here's the code:
For Each item As String In ListBox1.Items
If ListBox1.SelectedItems.Contains(item) Then
MessageBox.Show(item & "|" & True)
Else
MessageBox.Show(item & "|" & False)
End If
Next
I only get one messagebox for the first item, wether its selected or not, but then i get that error.
Virtual Space Shuttle Astronaut
|
|
|
|
|
Hi,
sorry I didn't know this, it seems one cannot iterate both Items and SelectedItems like that at the same time.
I did come up with this code, which works fine:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
Dim s As String = ""
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim item As String = ListBox1.Items(i)
If ListBox1.SelectedItems.Contains(item) Then
s = s & " +" & item
Else
s = s & " -" & item
End If
Next
Label1.Text = s
End Sub
|
|
|
|
|
|
You're welcome.
|
|
|
|
|
Hi, dear all,
I have a project created in VB.net 2005 and I will collect some user input data based on user selection for calculation.
Since the input data number and content are different based on user selection, I have problem about how to layout these input fields.
For example, the SpeedContrlType is a combobox with value 1,2 and 3, for different value, the set of input data is totall different. If user select value 1, I will show data for type 1 and hide data for other 2 types, same for type 2 and 3.
Can you give me some suggenstion how to organize the form for this case? I don't want to show all data at the same time, then if user select type 1, enable type 1 data set and disable other two sets.
Thanks a lot!
|
|
|
|
|
Andraw Tang wrote: Can you give me some suggenstion how to organize the form for this case? I don't want to show all data at the same time, then if user select type 1, enable type 1 data set and disable other two sets.
The easiest option is to put the UI for each type into Panel . Hide panels number 2 & 3 by default, and switch visibility if the user changes the type.
A better choice would be to move it to a UserControl
I are Troll
|
|
|
|
|
Eddy,
Thank you for your reply.
If I use panels to hold data, I should overlap these panels and make one of them visible according to user selection, right?
What did you mean to move it to UserControl? I am new to VB.net, can you give me more explanation? it's best if you can give me some sample.
Thanks again!
|
|
|
|
|
Andraw Tang wrote: If I use panels to hold data, I should overlap these panels and make one of them visible according to user selection, right?
Yup. It would be easier to put them next to each other in the designer, and to move them to the correct location when the form is loaded.
Andraw Tang wrote: What did you mean to move it to UserControl?
A UserControl resembles a blank panel, and is used if you need to build your own controls. Imagine that you're using a Panel over and over again to display a picture and the name of the user. You could copy/paste that code and the panel, or you could create a new control;
Public Class MyPanel
Inherits System.Windows.Forms.UserControl
Public Name As String
End Class
This new control can contain anything you like. There's a walkthrough[^] on MSDN if you want to give it a try
I are Troll
|
|
|
|
|
Eddy,
Thanks, I will take a look.
|
|
|
|
|
I'm moving from Ms Access to vb.net/Sql Server. I'm looking for a sample project that includes a vb.net front end and Sql Server back end. There is plenty of sample code out there, but I havn't found any complete applications.
|
|
|
|
|
I'm not sure if you can find complete application, but there are plenty sample codes. What is wrong with sample codes?
|
|
|
|
|
I'm very new to programming, and I'm probably not much of a natural at it, although I enjoy it thoroughly. I learned what I know about Ms Access from "reverse engineering" an application(as well as reading books of course).
I just purchased a book Murach's ado.net vb2008, it has a good amount of sample applications so hopefully it will help.
|
|
|
|
|
soulrebelpd wrote: I learned what I know about Ms Access from "reverse engineering" an application(as well as reading books of course).
I'm not sure how much you can learn from reverse engineering alone. Not to mention the amount of time you will spend in understanding the code.
soulrebelpd wrote: I just purchased a book Murach's ado.net vb2008, it has a good amount of sample applications so hopefully it will help.
Good for you. Books are good source of information. Also consider taking a class. It may accelerate your learning curve.
|
|
|
|
|
|
|
Hi Everybody,
Is there any other process to register dll other than regsvr32 process. If yes please let me know.
Thanks in advance,
Jayachandra
|
|
|
|
|
An actual COM .DLL or a .NET assembly? Are you sure it's a COM-base .DLL? Library .DLL's cannot be registered.
The only other way to do it would be to poke the registry values in by manually, but that's not a good idea at all.
|
|
|
|
|
maybe ...
google this:
programmatically register activex dll
|
|
|
|
|
hello
I'm inheriting a legacy VB COM component - I tried register the component:
regsvr32 SomeMatlabUtil.dll
(The folder contains SomeMatlabUtil.tbl as well in same folder)
But I ran into the error:
SomeMatlabUtil.dll was loaded, but the DllRegisterServer entry point was not found.
We're deploying this on 64 bit WIN2003 machine. Need some help on this...
Thanks
dev
|
|
|
|