Any one help me,
I have to DateTimePicker i need loop condition fill listview two DateTimePicker. and save them database using Selected Checkbox (Listview checkbox) and one textbox.
http://i.stack.imgur.com/YTsQW.jpg
Thanks
Try This Logic...
For count = 0 To DataGridView1.RowCount - 1
If DataGridView1.Rows(count).Cells(0).Value = True Then
'SQL INSERT COMMAND HERE
End If
Next
Since it is a ListView, you need to iterate the CheckedItems collection:
For Each lvi As ListViewItem in myLv.CheckedItems
' SQL operation using lvi.SubItems for the values
Next
This will work on all the items checked (as per the question) not just the one singled out in the image. you could also iterate CheckedIndices as an index into Items().
Related
I'm populating a VBA ComboBox with some data from a Workshet. As it is populated depending on the choice of the user, sometimes some levels are should not be populated, because they do not have data. When such a thing happens, I want to populate the items of a ComboBox with the same of another. How can a do that?
I think if somebody teaches me how to loop through the items of a ComboBox, I will the able to do what I want.
Here is how you loop through a combobox.
Dim intComboItem As Integer
For intComboItem = 0 To Me.ComboBox1.ListCount - 1
MsgBox Me.ComboBox1.List(intComboItem)
Next
I'm working with a listbox in vb.net and am trying to remove all items from the listbox that don't contain specific text at the click of a button. Here's my code:
Dim i As Integer
For i = 0 To ListBoxPrePublish.Items.Count - 1
If InStr(ListBoxPrePublish.Items(i), "-8-") > 0 = False Then
ListBoxPrePublish.Items.RemoveAt(i)
Exit For
End If
Next
This only removes 1 item at a time though. How can I tweak this to remove all items that don't contain "-8-" at once?
EDIT: in case anyone asks, the listbox items list is growing rather large so I'm adding a sort feature so users can widdle down their options if they want to. That's why I'm not filtering anything before adding to the listbox
Here is the complete code for backward loop I mentioned in the comments - it should work:
For i as Integer = ListBoxPrePublish.Items.Count - 1 To 0 Step -1
If Not ListBoxPrePublish.Items(i).Contains("-8-") Then
ListBoxPrePublish.Items.RemoveAt(i)
End If
Next
No, I do not know of any RemoveRange type functionality. And be advised that you will need to loop through the listbox Items collection backwards as you remove items or you will get index exceptions, because once you remove something it will mess up the index values of all the remaining items in the iterator.
I've tried searching on stackoverflow and implementing code that I found but to no avail. As the text changes I want to use the current text in the textbox to filter through the listview items (so the items that are not closed to being matched get removed) and it only leaves me with whatever is contained in the columns.
Here's an example of what I mean:
Search: "Georges"
|1|Anderson Silva|Anderson silva is the champion in the ...|
|2|Georges St-Pierre| Georges is the champion in the ...|
|3|Georges Sotoropolis| Georges Sotoropolis is a fighter in the lightweight division|
With this search, only rows 2 and three would be returned. The first row would be omitted and not displayed. Once I erase the terms, then it would get displayed.
Here is the code that I currently have:
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
It seems to be working but I cannot see the listview items once I enter something in the tbSearch. The scrollbar gets smaller / larger depending if there are more / less items due to the search being executed. My problem seems that they are not visible
Thank you!
Listview.Clear wipes out the items, columns, and groups. It appears you only want to wipe out the items, so call lwArticles.Items.Clear instead of lwArticles.Clear
I would suggest another approach - first create a DataTable based on your original items. Then create a DataView and assign that as DataSource of your ListView. Now you can change DataView.RowFilter, and your list will update automatically, so only items matching the filter will show up. Using this approach you don't need to recreate everything from scratch every time, and your TextChanged becomes very simple and maintainable - it just changes RowFilter, if a corresponding DataView has already been created.
Here's the final answer of my question:
Private originalListItems As New List(Of ListViewItem) 'this gets populated on form load
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Items.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Items.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
Credits to Dan-o for explaining that lwArticles.Clear() will clear everything. He notified me that I needed lwArticles.Items.Clear() to clear only the items in the listview.
I have 8 comboboxes on my form that will all hold the same values - Yes and No.
Is there a quicker way than having to do combox1.items.add("Yes") etc?
I have the following but I cant seem to find anything to do with adding the items.
Dim cmb As Control
For Each cmb In Panel1.Controls
If TypeOf cmb Is ComboBox Then
'cmb. isnt beinging anything up for adding items?
End If
Next
Cheers
You can use Enumerable.OfType:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.Items.Add("Yes")
Next
I would create a DataSource containing {Yes,No} values, for example as a List and then just do this:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.DataSource = myYesNoDataSource
Next
Later if you need to accept Y and N in place of Yes and No, you can convert to Dictionary and set ValueMember and DisplayMember accordingly. Plus your list of available values is only initialized once. So your solution becomes flexible.
This Code will be useful, if you didnt use panel.
If you have 5 comboboxes give count 1 to 5 and name of those comboboxes like ComboBox1, ComboBox2, comboBox3 etc.
For count = 1 To 5
Dim combobox = DirectCast(Me.Controls("ComboBox" & n & ""), ComboBox)
combobox.Items.Add("Ok")
Next
Hope this code also helps you.
I'm trying to use listview control in vb.net or vb2008 express edition to display records from my database after it is being queried.I have two column headers in my listview control which are named Account# and Authorized Users.
I was able to display the records from the database into the listview control but i don't know how to arrange it in a proper column where it should be displayed cause what happen is all records are being combined in one column which other records should be aligned to the other column.What I want is to display the account numbers in aligned with the Account# column header and the names should be aligned in Authorized Users column header.
Here's the codes I used:
*****************************************************************
Private Sub Button1_Click(-------------) Handles Button1.Click
Dim db As New memrecDataContext
Dim au = From ah In db.Table1s _
Where ah.Account <> " "
For Each ah In au
With Form9.ListView1.Items
Form9.ListView1.Items.Add(ah.Account)
Form9.ListView1.Items.Add(ah.Name)
Form9.Show()
End With
Next
End Sub
*********************************************************
I would greatly appreciate if you could correctly debug the codes for me?
In listview, details mode, both columns are in a single listview item. The first column is item.text, and the rest are in item.subitems, something like this:
dim item as ListViewItem
ListView1.Columns.Add("Account")
ListView1.Columns.Add("Name")
For Each ah In au
item = New ListViewItem
item.text = ah.Account
item.subitems.add(ah.Name)
form9.listview1.items.add(item)
next ah
A couple of notes: You should not show the form every time you add an item to the listview. You're better off showing the form only once. Second, the With statement is used to shortcut writing form9.listview.items. Inside the With block you can use just a period (.) instead of writing out form9.listview.items.