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.
Related
I am hoping someone can help me out because I don't often code in Excel VBA. I have two Listbox and a CommandButton on a UserForm, which adds selected records from the first ListBox to the second. The range in the first ListBox is kind of long and so users of the form will generally select a couple records, hit the Add button, and then scroll down the first ListBox to select more records to add. The problem is, only the ones that have been added last will actually be recognized and used to generate reports even though all of the selected records appear in the second ListBox. Here is my current code for the button that adds the records to the second ListBox:
Private Sub AddButton_Click()
For i = 0 To Listbox1.ListCount - 1
If Listbox1.Selected(i) = True Then Listbox2.AddItem Listbox2.List(i)
Next i
End Sub
How can I populate the second ListBox so that it will include every record selected no matter how many times the add button is used?
Private Sub comboBoxStudentID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxStudentID.SelectedIndexChanged
For Each dr As DataGridViewRow In Me.dataGridViewStudent.Rows
If dr.Cells(0).Value.ToString.Contains(comboBoxStudentID.Text) Then dr.Visible = True Else dr.Visible = False
Next
End Sub
I've created this method to check and display a row that contains the same Student ID as selected from the comboBoxStudentID the problem is it only checks the first row of the DataGridView. How can I make it check all of the rows if there is match of Student ID?
Without knowing how your data is organized, you could do this by binding the DataGridView and the ComboBox to the same data source. Since this is not specified and looking at the snippet of code (which appears to work) it appears to loop through the rows and if the currently selected combo box text is contained in the first column cell, then make that row visible. Otherwise make the row invisible.
The posted code appears to work however there is one scenario you may want to consider. If the combo box contains the values that are in column one of the DataGridView, THEN, when the user selects a value in the combo box, the DataGridView will display ONLY those values that match the current combo box selection. This appears to be what you are trying to do.
The problem is how do you re-display all the rows?
Once the combo box selection has changed to some value, the user can delete the selection but all the rows will not display unless you handle another event. Also this strategy is not very user intuitive. One possible solution (below) simply adds a top item of “Reset” to the top of the combo boxes items. Then when the user selects “Reset” all row will become visible. Hope this helps.
To reiterate: you should consider setting this up with a DataSource and possibly a BindingSource where this is done for you automatically. Just a thought.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex > 0 Then
For Each dr As DataGridViewRow In DataGridView1.Rows
If (Not dr.IsNewRow) Then
If dr.Cells(0).Value.ToString.Contains(ComboBox1.Text) Then
dr.Visible = True
Else
dr.Visible = False
End If
End If
Next
Else
For Each row As DataGridViewRow In DataGridView1.Rows
row.Visible = True
Next
End If
End Sub
In Visual basic i'm trying to make a field required by preventing the user to leave the active cell if it's empty.
The grid consists out of two columns. The first one is the ID which is automaticly filled in on adding a new row, the second one is the name.
On form load there are 4 items loaded into the grid. When the form is loaded the first cell that is selected passes the check with no problem. It is the same for the three cells that are filled on form load.
Unforunately it does not seem to work when i try to add a new row to the grid, enter a value and try to leave the cell. I've set a break point and checked the values. The value is equal to nothing... This is not the case with any of the already loaded values.
How does it not get the value i enter?
Thanks in advance.
Ivan.f
Private Sub ValidateCellValue(sender As Object, e As
DataGridViewCellValidatingEventArgs)
Handles grdResult.CellValidating
Dim catNaam = grdResult.Rows(e.RowIndex).Cells(1).Value
If IsNothing(catNaam) Then
MessageBox.Show("Veld mag niet leeg zijn")
e.Cancel = True
Exit Sub
End If
End Sub
Currently I have a program that is able to write to a ListView with column named : number, time, description . This listview is not bound to anything data, I'm just basically writing into it using the code.
What I want to do is to have a TextBox, whereby if the user wants to look at particular number i.e. 2, when they type into the textbox, then I want the listview to only show data with number = 2. When there's nothing in the textbox, I want the listview to show all the data.
I have being looking around on the internet and I didn't seem to find a filter method. Does it even exist and if so how would I go about implementing this.
All help is appreciated.
While I recommend using a DataGridView with DataSource but in cases that you need to use ListView, you can use this solution.
You can filter your list view this way:
Define a member field as backup storage of items:
In form Load after adding items to list view, store each item in that member field
Put a TextBox and a Button on form and handle Click event of the Button and in the handler, first clear all items of ListView then each item from that backup storage that matches with criteria.
Member Field for Backup of Items
Private ItemsBackup As New List(Of ListViewItem)
Fill backup after loading items in ListView in the form Load event
For Each item As ListViewItem In ListView1.Items
ItemsBackup.Add(item)
Next
Code of Filter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.ListView1.BeginUpdate()
Me.ListView1.Items.Clear()
For Each item As ListViewItem In ItemsBackup
If (item.Text = Me.TextBox1.Text Or String.IsNullOrEmpty(Me.TextBox1.Text)) Then
Me.ListView1.Items.Add(item)
End If
Next
Me.ListView1.EndUpdate()
End Sub
You can also use above code for TextChanged event of the TextBox.
Here is a test data:
For i = 1 To 30
Dim item As New ListViewItem(i.ToString())
item.SubItems.Add(DateTime.Now.AddHours(i))
Me.ListView1.Items.Add(item)
Next
A normal .NET ListView can't do this without a considerable amount of work. So, you could use ObjectListView -- an open source wrapper around a standard .NET ListView -- which has already done all the work for you.
It has built-in filtering (and highlighting)
I am making a personal application in VB.NET that uses a CheckedListBox to store items. I have three buttons on my form, with which I would like to change the selected item's color with (to green, orange, and red.)
I have tried numerous approaches to this issue and have had no such luck. Could someone lend a helping hand?
Use a ListView instead. It has support for checkboxes and selected item colors.
There is a very similar answer here:
For each <item> in CheckedListBox. <item> returns as Object and not as Control
Basically, this control won't do what you want it to (at least not without much complexity). You need to upgrade your control to a ListView.
You can also use TreeView that looks and acts like a checked list box:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.TreeView1.ShowLines = False
Me.TreeView1.CheckBoxes = True
Dim MyColors() As Color = {Color.Black, Color.Blue, Color.Red, Color.Green, Color.Aqua}
For x As Integer = 0 To 4
Dim NewNode As TreeNode = Me.TreeView1.Nodes.Add("Node" & x)
NewNode.ForeColor = MyColors(x)
Next
End Sub
Took the advice of using a ListView. Looked and worked great on my Windows 7 and Vista boxes but on XP, the ListView did not display properly (items were crunched overtop of one another, checkboxes didn't always display). Checked to make sure it was not a framework version issue and that it was not a screen resolution issue. Ended up retreating back to the CheckedListBox implementation which did NOT have the inconsistency.
Found this solution that accomplished the removal of the blue highlight in the CheckedListBox scenario for me. Using it however you have to keep track of the selection in another manner (global variable, looking at the checked item(s), etc.).
I simply clear the selected item(s) after processing the "..._SelectedIndexChanged". The first thing I do in the "..._SelectedIndexChange" is test for no Selection and do nothing if that is the change. The result is that the currently selected item appears unselected (and actually is unselected, i.e. no blue highlight) however the CheckBox remains checked indicating the most recent selection for the user.
Example ==>
Private Sub ModelCheckedListBox_SelectedIndexChanged(ByVal sender As System.Object,...
Dim x As Short = ModelCheckedListBox.SelectedIndex
If x >= 0 Then
'Something I always do since the Selection Mode = "One" doesn't bother to clear
'the checks itself
ModelCheckedListBox.SetItemChecked(x, True)
If ModelCheckedListBox.CheckedItems.Count > 1 Then
For Each item In ModelCheckedListBox.CheckedIndices
If item <> x Then
ModelCheckedListBox.SetItemChecked(item, False)
End If
Next
End If
ModelCheckedListBox.Refresh()
'More of your code
ModelCheckedListBox.ClearSelected()
End If
End Sub