ListView : Fetch data from selected item - vb.net

I have created a listview and populated it using datatable..
All worked fine... Now I added checkbox to all items..
Now I want like when the last column's value of an item is "true" then the checkbox of the same item is checked.
I tried the following code...
If LstViewHelp.Items.Count <> 0 Then
For Each item As ListViewItem In LstViewHelp.Items
If LstViewHelp.FocusedItem.SubItems(10).Text = "True" Then
LstViewHelp.FocusedItem.Checked = True
End If
Next
End If
I am getting the following error object reference is not set to an instance.
Tried many links but no proper solution found...!

You can get all selected index like ListView.SelectedIndexCollection using SelectedIndices . Add ForEach loop on selected indexes and edit your SubItem.
Dim indexes As ListView.SelectedIndexCollection = Me.ListViewHelp.SelectedIndices
For Each index In indexes
If Me.ListViewHelp.Items(index).SubItems(10).Text = "True" Then
LstViewHelp.Items(index).Checked = True
End If
Next
And if you want to check all items you can use for loop
For i = 0 To Me.ListViewHelp.Items.Count - 1
If Me.ListViewHelp.Items(i).SubItems(10).Text = "True" Then
LstViewHelp.Items(i).Checked = True
End If
Next

Related

CheckBox in DataGridView Won't Evaluate if Cell Has Focus

The first column in a DataGridView (dgv1) has checkboxes. Interestingly, when you click on the last checkbox desired, as soon a you loop through the rows for that column to evaluate checked items, the last cell checked (with a cursor on it) does not evaluate to true. The syntax for looping through the checkboxes in column 0 is below:
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next
Is there a way to set the focus to false if the cell containing the last checkbox checked is in edit mode?
BTW, refreshing the edit status (below) did not help before looping:
dgv1.RefreshEdit()
Per #JohnG's comment, the suggestion worked as follows:
dgv1.EndEdit()
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next

Referencing value from Excel Listbox item in .Match function in VBA

I am hoping to use the string value of a selected Listbox item in a .Match function within VBA - I need the the value '1' to be entered into the row where the value of the selection matches a value in column "A:A", on a specific column.
What I thought I would be able to do is to use a .value argument for the selected ListBox item, however this seems to either error out or give me a Boolean response, which isn't what I am after (I am after the actual string value of the item).
I have already looped through all items to set the Selected argument to True, and then I am looping through the list one by one to add '1' to the correct range.
Here is the code I thought would work (but doesn't, it throws an error of "Run-time error '13': Type mismatch" which is presumably down to the .Value not being a String.
For x = 0 To Me.CreditsEmployeesListBox.ListCount - 1
Me.CreditsEmployeesListBox.Selected(x) = True
Next
For i = 0 To Me.CreditsEmployeesListBox.ListCount - 1
If Me.CreditsEmployeesListBox.Selected(i) = True Then
employeeRow = WorksheetFunction.Match(Me.CreditsEmployeesListBox(i).Value, IndexSheet.Range("A:A"), 0)
IndexSheet.Range(Cells(employeeRow, showCodeColumn).Address).Value = 1
End If
Next
It errors out on the 'employeeRow = ...' line. Here, I am essentially trying to ask it:
employeeRow = WorksheetFunction.Match(<value of the currently referenced ListBox item>,IndexSheet.Range("A:A"),0)
Is this possible with VBA or am I going about this the wrong way?
Thanks
Matt
As an "hybrid" answer (as there is more than one problem) try this:
For x = 0 To Me.CreditsEmployeesListBox.ListCount - 1
Me.CreditsEmployeesListBox.Selected(x) = True
Next
Dim employeeRow As Variant
For i = 0 To Me.CreditsEmployeesListBox.ListCount - 1
If Me.CreditsEmployeesListBox.Selected(i) = True Then
employeeRow = Application.Match(Me.CreditsEmployeesListBox.List(i), IndexSheet.Columns(1), 0)
If IsNumeric(employeeRow) Then IndexSheet.Cells(employeeRow, showCodeColumn).Value = 1
End If
Next
This also should avoid VBA-errors.
If any questions are left, just ask :)

Unselecting Items in a Pivot Table with an IF THEN

I am trying to remove some fields from a Pivot Table. The Table changes but the list of items I am trying to remove stay the same.
It works if I just use the deselect code like this:
Set pf =ActiveSheet.PivotTables("PivotTable1").PivotFields("ServiceName")
pf.PivotItems("Disk").Visible = False
pf.PivotItems("SNMP").Visible = False
pf.PivotItems("POP").Visible = False..... and so on for about 140 Items
My problem comes when I have a Pivot table that does not contain the PivotItem. Run time error: "Unable to get the PivotItems property of the PivotField class
So I thought I would us a simple IF THEN:
If pf = ("POP") Then pf.PivotItems("POP").Visible = False
But it doesn't work. It doesn't do anything, no errors, no changes to the field. It just blinks and its done.
What am I missing?
You could iterate over the PivotItems, therefore you modify only existing items, like so (see MSDN for a description):
Dim dict As New Scripting.Dictionary
' add the items to be removed
' dictionaries take key and value, but we are
' only interested in the key here. So the
' value could really be anything.
c.Add "name1" 1
c.Add "name2" 1
Worksheets("sheet4").Activate
With Worksheets("sheet3").PivotTables(1)
For i = 1 To .PivotFields.Count
For j = 1 To .PivotFields(i).PivotItems.Count
If c.Exists(.PivotFields(i).PivotItems(j).Name) then
.PivotFields(i).PivotItems(j).Visible = False
End If
Next
Next
End With

max number checkbox columns datagridview

I am working in VB.NET and have a datagridview with multiple columns, the first being a checkboxcolumn. I want to add the functionality to restrict the amount of rows the user can select. For example, I may have 10 rows of entries in the datagridview but want to only allow for 5 checkboxes to be checked. Does anyone have any ideas on how to go about this?
You can use the readonly property of datagridview to prevent the edit.and use a for loop to limit which all rows to be disabled.
for i=5 to datagridview1.rows.count-1
dataGridView1.Rows[i].Cells[columnindex].ReadOnly = true;
next
Specify the index of column to disable at column index.this code will disable editing of 6th to last row of datagridview.
You can Maintain States of CheckBoxes in ViewState and Then Check Length of Array List which Gives You No of Items Checked
Dim CheckBoxArray As ArrayList
If ViewState("CheckBoxArray") IsNot Nothing Then
CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
Else
CheckBoxArray = New ArrayList()
End If
Add or Remove items From ViewState
If chkAll.Checked Then
If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then
CheckBoxArray.Add(checkAllIndex)
End If
Else
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
CheckBoxArray.Remove(checkAllIndex)
CheckAllWasChecked = True
End If
End If

Multi Select List Box

I have a list box on a form and it works fine for what I want to do.
I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.
My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.
PLease can someone give me some pointers.
I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.
Any help wil be much appreciated.
My Code:
Private Sub SelectItemSizes(ByVal itemID As Integer)
Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
If (itemSizes.Count > 0) Then
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
End If
End Sub
Did you set the selectionmode to multi?
You need to specify that in order to allow multiple selections.
Then you can do:
Dim i as Integer=0
For i=0 To Me.listBox.SelectedItems.Count -1
'display the listbox value
next i
Here is a screen shot:
After you set the property on the listbox then call setselected based on the values you want selected.
me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)
Here you can add 20 numbers and only select the even.
Dim i As Integer
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
'now use setselected
'assume only even are selected
For i = 0 To 20
If i Mod 2 = 0 Then
Me.ListBox1.SetSelected(i, True)
End If
Next
3rd edit
Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:
Dim i As Integer
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
Dim lCount As Integer = 0
For lCount = 0 To l.Count - 1
For i = 0 To 20
If i = l.Item(lCount) Then
Me.ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
In the code my l is a list of just 3 items: 2, 6, and 20.
I add these items to l which is just a list object.
So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.
Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.
After running my code here is the result
You have to change the ListBox.SelectionMode property in order to enable multiple-selection.
The possible values are given by the SelectionMode enum, as follows:
None: No items can be selected
One: Only one item can be selected
MultiSimple: Multiple items can be selected
MultiExtended: Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections
So, you simply need to add the following line to the code you already have:
' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;
' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)
Alternatively, you can set the SelectionMode property at design time, instead of doing it with code.
According to MSDN, SetSelected() can be used to select multiple items. Simply repeat the call for each item that needs to be selected. This is the example they use:
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
For reference, this is the MSDN article.
Because my code had the following loops:
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
The first loop loops through the available sizes and the second loop is used to compare the item sizes.
Having the following code:
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Meant that even if item i became selected, it could also be deselected.
SOLUTION:
Remove Me.lstItemSizes.SetSelected(i, False) OR Include Exit For