-
May 22nd, 2015, 05:00 PM
#1
Thread Starter
Lively Member
[RESOLVED] how to manualy set index or reset it
how do i manualy set an index of my items or reseting the index in my listview cause .items.clear not seems too work when you have selected items cause it still haves the old values
-
May 22nd, 2015, 05:27 PM
#2
Re: how to manualy set index or reset it
Use the SelectedIndices collection:
Code:
ListView1.SelectedIndices.Clear()
to set index 0 selected:
Code:
ListView1.SelectedIndices.Add(0)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2015, 05:36 PM
#3
Thread Starter
Lively Member
Re: how to manualy set index or reset it
so i should use SelectedIndice instead of .Items.Item(0).Selected() = True
this is how the code looks and is calling the code 2 times
Code:
If ListView1.Items.Item(0).Selected() = True Then
ListView1.Items.Clear()
'Add items to ListView
Dim arr As String() = New String(2) {}
Dim itm As ListViewItem
'Add first item
arr(0) = "Alfa Romeo"
arr(1) = "4x98 4x108 5x98"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.Items(0).ImageIndex = 0
End If
Last edited by kewin; May 22nd, 2015 at 05:40 PM.
-
May 22nd, 2015, 05:40 PM
#4
Re: how to manualy set index or reset it
Try it and you'll see how it works
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2015, 05:45 PM
#5
Thread Starter
Lively Member
Re: how to manualy set index or reset it
still getting this error Additional information: Invalid Argument = Value '2' is invalid for 'index'. when i click on
Code:
If ListView1.Items.Item(1).Selected() = True Then
ListView1.SelectedIndices.Add(1)
mærke.Text = "Aston Martin"
Model.Text = "ALLE"
Krydsnålb.Text = "ALLE"
ListView1.Items.Clear()
'Add items to ListView
Dim arr As String() = New String(2) {}
Dim itm As ListViewItem
'Add first item
arr(0) = "Aston Martin DB9"
arr(1) = "5x114,3"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.Items(0).ImageIndex = 1
-
May 22nd, 2015, 05:48 PM
#6
Thread Starter
Lively Member
Re: how to manualy set index or reset it
this is the error i had been strugling with for a couple weeks now and i guess is due too index dont exists
start up items
Code:
Private Sub Krydsmål_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.SmallImageList = ImageList1
ListView1.Items.Clear()
'Add items to ListView
Dim arr As String() = New String(2) {}
Dim itm As ListViewItem
'Add first item
arr(0) = "Alfa Romeo"
arr(1) = "4x98 4x108 5x98 5x108 5x110"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.SelectedIndices.Add(0)
ListView1.Items(0).ImageIndex = 0
'Add second item
arr(0) = "Aston Martin"
arr(1) = "5x114,3"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.SelectedIndices.Add(1)
ListView1.Items(1).ImageIndex = 1
'Add third item
arr(0) = "Audi"
arr(1) = "4x100 4x108 5x100 5x112"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.SelectedIndices.Add(2)
ListView1.Items(2).ImageIndex = 2
first item (working)
Code:
'Declaration
Public Event ItemActivate As EventHandler
Private Sub ListView1_ItemActivate(sender As Object, e As EventArgs) Handles ListView1.MouseClick
If ListView1.Items.Item(0).Selected() = True Then
ListView1.Items.Clear()
'Add items to ListView
Dim arr As String() = New String(2) {}
Dim itm As ListViewItem
'Add first item
arr(0) = "Alfa Romeo 145"
arr(1) = "4x98"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.Items(0).ImageIndex = 0
End if
2nd (error)
Code:
If ListView1.Items.Item(1).Selected() = True Then
ListView1.Items.Clear()
'Add items to ListView
Dim arr As String() = New String(2) {}
Dim itm As ListViewItem
'Add first item
arr(0) = "Aston Martin DB9"
arr(1) = "5x114,3"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.Items(0).ImageIndex = 1
End if
3rd item(working but the error refareting to it)
Code:
If ListView1.Items.Item(2).Selected() = True Then
ListView1.Items.Clear()
'Add items to ListView
Dim arr As String() = New String(2) {}
Dim itm As ListViewItem
'Add first item
arr(0) = "Audi 100"
arr(1) = "4x108 5x112"
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
ListView1.Items(0).ImageIndex = 2
End if
Last edited by kewin; May 22nd, 2015 at 06:00 PM.
-
May 22nd, 2015, 06:01 PM
#7
Re: how to manualy set index or reset it
Why are you clearing the items and re-adding them?
The IndexOutOfRangeException means that you are referring to an index that doesn't exist
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2015, 06:04 PM
#8
Thread Starter
Lively Member
Re: how to manualy set index or reset it
cause i want too when i example click on alfa romeo it will only come up with all the alfa romeo models same with when i click on aston martin and audi are there any other ways to do that?
-
May 22nd, 2015, 06:06 PM
#9
Re: how to manualy set index or reset it
If you clear the items, then add one item, any index other than 0 doesn't exist
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2015, 06:11 PM
#10
Thread Starter
Lively Member
Re: how to manualy set index or reset it
so is not possible to do what i am doing such as adding multiple items too all the other start up items here like i have for my first start up item here
Last edited by kewin; May 22nd, 2015 at 06:18 PM.
-
May 22nd, 2015, 06:35 PM
#11
Re: how to manualy set index or reset it
You could use something like this. I used a resetListview method that returns the listview to showing a list of makers. When you doubleclick a maker it lists all of their models. There's a button for returning to the first makers view:
Code:
Public Class cars
Public maker As String
Public models() As ListViewItem
End Class
Code:
Public Class Form1
Dim allCars As New List(Of cars)
Dim isMain As Boolean = True
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
allCars.Add(New cars With {.maker = "Alfa Romeo", .models = New ListViewItem() {New ListViewItem("Model 1"), New ListViewItem("Model 2"), New ListViewItem("Model 3")}})
allCars.Add(New cars With {.maker = "Aston Martin", .models = New ListViewItem() {New ListViewItem("Model 4"), New ListViewItem("Model 5"), New ListViewItem("Model 6")}})
allCars.Add(New cars With {.maker = "Audi", .models = New ListViewItem() {New ListViewItem("Model 7"), New ListViewItem("Model 8"), New ListViewItem("Model 9")}})
resetListview()
End Sub
Private Sub resetListview()
ListView1.Items.Clear()
ListView1.Items.AddRange(Array.ConvertAll(allCars.ToArray, Function(c) New ListViewItem(c.maker)))
isMain = True
End Sub
Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
If Not isMain Then Return
Dim index As Integer = ListView1.HitTest(e.Location).Item.Index
If index <> -1 Then
Dim maker As String = ListView1.Items(index).Text
ListView1.Items.Clear()
For Each c As cars In allCars
If c.maker = maker Then
ListView1.Items.AddRange(c.models)
End If
Next
isMain = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
resetListview()
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2015, 06:52 PM
#12
Thread Starter
Lively Member
Re: how to manualy set index or reset it
awesome is working as i want it too ty so much but how do i add the icons to it now cause i getting Additional information: Invalid Argument = Value '0' is not valid for the 'index'.
Code:
ListView1.SmallImageList = ImageList1
ListView1.Items(0).ImageIndex = 0
with this now
Last edited by kewin; May 22nd, 2015 at 07:08 PM.
-
May 22nd, 2015, 07:27 PM
#13
Re: how to manualy set index or reset it
Either index 0 doesn't exist in the imagelist, or index 0 doesn't exist in the listview items, at the point that you're attempting to set the imageindex. If the image exists at index 0, then make sure you set the imageindex after adding the listviewitems
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 22nd, 2015, 07:32 PM
#14
Thread Starter
Lively Member
Re: how to manualy set index or reset it
arh i see had too place it after the resetListview()
-
May 22nd, 2015, 07:55 PM
#15
Thread Starter
Lively Member
Re: [RESOLVED] how to manualy set index or reset it
and how do i add the sub items? ex "4x98 4x108 5x98 5x108 5x110" out for maker alfa romeo and "4x98" out for model 1 and also i can't get it too add the icon for the models only the maker
Last edited by kewin; May 22nd, 2015 at 08:03 PM.
-
May 22nd, 2015, 08:04 PM
#16
Re: [RESOLVED] how to manualy set index or reset it
Change the listviewitems array:
New ListViewItem(){New ListViewItem(New String() {"text", "first subitem", "second subitem"}), New ListViewItem(New String() {"text", "first subitem", "second subitem"})}
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 23rd, 2015, 04:25 AM
#17
Thread Starter
Lively Member
Re: [RESOLVED] how to manualy set index or reset it
it works for models
Code:
allcars.Add(New cars With {.marker = "Alfa Romeo", .models = New ListViewItem() {New ListViewItem(New String() {"text", "first subitem", "second subitem"})}})
but when i put
Code:
allcars.Add(New cars With {.marker = New ListViewItem(){New ListViewItem(New String() {"text", "first subitem", "second subitem"}), .models = New ListViewItem() {New ListViewItem(New String() {"text", "first subitem", "second subitem"})}})
i getting oppretor '=' is not defined for types '1 array and 1- dimmensional array
and also how to make another click event for the models too come up with the cars year?
Last edited by kewin; May 23rd, 2015 at 04:40 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|