I want to have a button that will check all checkboxes in my listview. I have:
Dim I as Integer
If listViewAccounts.CheckedItems.Count > 0 then
(>>My Problem here<<)
End if
What to do next?
for i = 0 to listViewAccounts.Items.Count -1
listViewAccounts.Items(i).Checked = true
next
Or just use Linq
listView1.Items.OfType(Of ListViewItem).All(Function(c)
c.Checked = True
Return True
End Function)
I used:
For Each lvi As ListViewItem In listViewAccounts.Items
lvi.Checked = True
Next
Related
I have been searching for a method to group multiple objects to change a common value but have not been successful. I have been forced to do things like this:
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
Label13.Visible = True
Label14.Visible = True
RectangleShape8.Visible = True
RectangleShape9.Visible = True
RectangleShape10.Visible = True
RectangleShape11.Visible = True
Ect, Is there a method to group, or declare multiple objects to refer to all of them at the same time? I have attempted to declare but i was unsuccessful. Thanks for your help in advanced.
You can use the following:
Dim RectangleShapeGroup() As String = {"RectangleShape8", "RectangleShape9", "RectangleShape10", "RectangleShape11"}
Dim LabelGroup() As String = {"Label10", "Label11", "Label12", "Label13", "Label14"}
For Each ctrl As Control In Me.Controls
If Array.IndexOf(RectangleShapeGroup, ctrl.Name) > -1 Or Array.IndexOf(LabelGroup, ctrl.Name) > -1 Then
ctrl.Visible = True
End If
Next
In case you want to show all Label controls you can use the following:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
ctrl.Visible = True
End If
Next
... or all controls with names starting with Label or RectangleShape:
For Each ctrl As Control In Me.Controls
If ctrl.Name.StartsWith("Label") Or ctrl.Name.StartsWith("RectangleShape") Then
ctrl.Visible = True
End If
Next
Just add your controls to List(Of ControlType) then loop through to change any properties. This code is for Windows Forms. If this is a different type of application plead indicate that in your question.
Private lstLabels As New List(Of Label) From {Label10, Label11, Label12, Label13, Label14}
Private Sub MakeLabelsVisible()
For Each l In lstLabels
l.Visible = True
Next
End Sub
I'm using the below code to try and loop through all ListBox controls in a panel on a WinForm. I want to check if any of them have a SelectedIndex above 0. If they do, I want to set a boolean value in an array to True, else set it to False:
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
i = i + 1
If cntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next
The issue I am having is that cntrl.selectedindex is not valid as .selectedindex is being picked up that it is not a member of Windows.Forms.Control
How do I get this to see it as a ListBox?
Try converting the cntrl to listbox first like this
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
Dim TmpCntrl As ListBox = TryCast(cntrl, ListBox)
i = i + 1
If TmpCntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next
I have some code that reads the value of a Listview on a button click. However, what it does is select all the entries to add to another Listview. How do I adjust my code to allow for single items selection. Thanks
lvSelectRequestItems.Items.Clear()
While dr.Read()
Dim LVS As New ListViewItem
'LVS.SubItems.Clear()
With (LVS)
.UseItemStyleForSubItems = False
.Text = dr("Box").ToString()
.SubItems.Add(dr("CustRef").ToString())
End With
lvSelectedItems.Items.Add(LVS)
End While
If ListViewObj.SelectedItems.Count >= 1 Then
While ListViewObj.SelectedItems.Count >= 1
ListViewObj.SelectedItems(0).Selected = False
End While
ListViewObj.Items(ListViewObj.FocusedItem.Index).Selected = True
End If
I have about 50 Maskedtextboxes, only few of them are visible. What I need is to only check the visible ones if they are empty.
I used this code to check all Maskedtextboxes:
Dim empty = TabLABOR.Controls.OfType(Of MaskedTextBox)().Where(Function(txt) txt.Text.Length = 0)
If empty.Any Then
MessageBox.Show(String.Format("Please fill all fields",
String.Join(",", empty.Select(Function(txt) txt.Name))))
Else
TabControlBlockD.SelectTab(TabMATERIALS)
End If
End Sub
you should use a for each like the following
dim myfrm as MyCurrentForm()
then
for Each item As System.Windows.Forms.Control In myfrm.Controls
If item.GetType Is GetType(System.Windows.Forms.MaskedTexbox) Then
For Each mboxes As MaskedTexbox In item.Controls
If MaskedTexbox.text = "" AND maskedTextbox.visible = true Then
//Make deltu king of the world
End If
Next
End If
Next
That should work.
edit:
I am having problems with highlighting the found listview item using the code below:
If lstMaster.View = View.Details AndAlso lstMaster.Items.Count > 0 Then
Dim lvi As ListViewItem = lstMaster.FindItemWithText(txtSearchSR.Text, True, 0)
If lvi IsNot Nothing Then
MsgBox("found")
lvi.ListView.Items(0).Selected = True 'Does not seem to work...
End If
End If
How do i highliht the found column?
David
Try setting Subitems
lvi.Items[0].UseItemStyleForSubItems = false
lvi.Items[0].SubItems[0].BackColor = Color.Black
lvi.Items[0].SubItems[0].ForeColor = Color.White
if that doesn't work try
lvi.UseItemStyleForSubItems = false
lvi.SubItems.Add(new ListViewItem.ListViewSubItem(lvi,"subitem", Color.Black, Color.White, lvi.Font ))
You need
lvi.Selected = True
From your snippet,
lvi.ListView.Items(0) will always return the first ListViewItem in the listview.
Got it! :o)
lstMaster.Items(lvi.Index).Selected = True
lstMaster.Select()
lstMaster.SelectedItems.Item(0).EnsureVisible()