VB.NET ListView Questions - vb.net

I have Questions?
If I enter a data in a textbox,
I want my listview to select the same data entered in the textbox,
example,
I have a StudentNumber column in my listview and it has data on it(ex. 123456)
I will enter 123456 in the textbox.
The ListView must select 123456 ?
Please Help
THANK YOU,

I think this will do what you want. It will search the first column of the ListView for the text in the TextBox.
Setup the listview:
With ListView1
.MultiSelect = False 'Ensure only one item selected at a time
.HideSelection = False 'Shows the selection when the textbox changes
'Add some items for testing
.Items.Add("1234")
.Items.Add("1122")
.Items.Add("1133")
End With
Then in the textbox TextChanged changed event:
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
ListView1.SelectedItems.Clear()
Dim foundItem As ListViewItem = ListView1.FindItemWithText(TextBox1.Text, False, 0, False)
If (foundItem IsNot Nothing) Then foundItem.Selected = True
End Sub
Alternatively if you want to specify which column of your ListView to search for the text then this function should do the trick:
Private Sub SelectListViewItem(ByRef listviewSource As ListView, ByVal textToFind As String, ByVal column As Integer)
Dim foundItem As ListViewItem = Nothing
Dim startIndex As Integer = 0
listviewSource.SelectedItems.Clear()
Do Until Not foundItem Is Nothing AndAlso foundItem.SubItems(column).Text = TextBox2.Text
If foundItem Is Nothing Then startIndex = 0 Else startIndex = foundItem.Index + 1
If startIndex > listviewSource.Items.Count - 1 Then Exit Sub 'We have reached end of the listview
foundItem = listviewSource.FindItemWithText(textToFind, True, startIndex)
If foundItem Is Nothing Then Exit Sub
Loop
If (foundItem IsNot Nothing) Then foundItem.Selected = True
End Sub
Usage:
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
SelectListViewItem(ListView1, TextBox2.Text, 1)
End Sub
Warning - In both cases, this may cause your application to perform poorly if you have a lot of items in your listview, in that case you could consider moving the code into a background worker

Related

vb.net DexExpress winform GridControl click column header select cell

I use GridControl with Gridview to display the data. I realized I click the column header is sorting the data instead of select all cell under the column header. May I know how to handle this? I have a sample code (below) but it use BandedGridview.
Private Sub bandedGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles bandedGridView1.MouseDown
If Control.ModifierKeys <> (Keys.Shift Or Keys.Control) Then
Return
End If
Dim view As BandedGridView = CType(sender, BandedGridView)
Dim hInfo As BandedGridHitInfo = view.CalcHitInfo(e.Location)
If hInfo.InColumn Then
view.ClearSelection()
SelectCells(hInfo.Column)
ElseIf hInfo.InBandPanel AndAlso hInfo.Band IsNot Nothing Then
view.ClearSelection()
SelectCells(hInfo.Band)
Else
Return
End If
CType(e, DXMouseEventArgs).Handled = True
End Sub
Private Sub SelectCells(ByVal column As BandedGridColumn)
For i As Integer = 0 To column.View.RowCount - 1
column.View.SelectCell(i, column)
Next i
End Sub
Private Sub SelectCells(ByVal band As GridBand)
For Each column As BandedGridColumn In band.Columns
SelectCells(column)
Next column
End Sub
I need GridView only, anyone can help?
If you want to use this code for GridView then you can just remove the word Banded from everywhere and remove anything that belongs to GridBand. For SelectedCells method you need to convert column.View to GridView. Also I suggest you to add GridView.BeginSelection and GridView.EndSelection methods into SelectedCells method.
Here is example:
Imports DevExpress.Utils
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
'...
Private Sub gridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles gridView1.MouseDown
If Control.ModifierKeys <> (Keys.Shift Or Keys.Control) Then
Return
End If
Dim view As GridView = CType(sender, GridView)
Dim hInfo As GridHitInfo = view.CalcHitInfo(e.Location)
If hInfo.InColumn Then
view.ClearSelection()
SelectCells(hInfo.Column)
Else
Return
End If
CType(e, DXMouseEventArgs).Handled = True
End Sub
Private Sub SelectCells(ByVal column As GridColumn)
Dim view As GridView = CType(column.View, GridView)
view.BeginSelection()
For i As Integer = 0 To view.RowCount - 1
view.SelectCell(i, column)
Next i
view.EndSelection()
End Sub

Set the Progress Bar Value according to data entered?

How can Set the Progress Bar Value increasing/ decreasing according to data entered in each controls on a form ? I mean, I am working with number of controls, so go for a sample [Suppose I have four textboxes, Progress Bar Maximum 100, when entering txt_1 Progress value have to increase to 100/4= 25, if I remove data from txt_1 the Value should decreased to Zero.
Sorry for my language, I am not good in English.
Can any one help me, please..... Thank You.
The Firs thing you do is set the progressbar maximum in your formload event to the number of textboxes you are using.
Dim TextBoxNumber As Integer = 4
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Maximum = TextBoxNumber
End Sub
Then you need to make 2 events.
1. TextBox enter event."Private Sub TextBox_Enter(sender As Object, e As EventArgs)"
2. TextBox TextChanged event"Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)"
Then Link all the textboxes you want to use to the events.
In these events you will use the Sender.
Make a Dim and convert the sender to type Textbox to use its properties." Dim TextControl As TextBox = CType(sender, TextBox)"
In the Enter event you count the text that is entered in the textbox.
If the count is 0 you set the boolean to true and if its more than 0 you set the Boolean to False.
This Boolean you will use in the Text Change event.
Dim CheckTextBox As Boolean
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles TextBox4.Enter, TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
Dim TextControl As TextBox = CType(sender, TextBox)
If TextControl.Text.Count > 0 Then
CheckTextBox = False
Else
CheckTextBox = True
End If
End Sub
In the Text Change event you need to do a few things.
1.Use a if statement to check if the textbox is empty."This will only trigger if the user deletes the text in the textbox."
So if empty remove 1 from the progressbar and set CheckTextBox"Boolean" to True
"ProgressValue = ProgressValue - 1"
"CheckTextBox = True"
2.Then use a ElseIf to check if the CheckTextBox"Boolean" is True.
If true add 1 to progressbar and set Boolean to false.
"ProgressValue = ProgressValue + 1"
"CheckTextBox = False"
You need to set the Boolean to false because otherwise you will add 25 for every time you add somthing to the textbox.
Dim ProgressValue As Integer
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
Dim TextControl As TextBox = CType(sender, TextBox)
If TextControl.Text = "" Then
ProgressValue = ProgressValue - 1
CheckTextBox = True
ElseIf CheckTextBox = True Then
ProgressValue = ProgressValue + 1
CheckTextBox = False
End If
ProgressBar1.Value = ProgressValue
End Sub

Datagridview selections with vb.net

Help pls! I want to automatically select all the rows that has the same value in a particular cell of every rows when the user manually select or click on a particular row. am using vb.net. Thanks.
Although the question is not clear enough, you can try:
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.RowIndex <> -1 Then
If DataGridView1.CurrentCell IsNot Nothing AndAlso DataGridView1.CurrentCell.Value IsNot Nothing Then
findOnGridview(DataGridView1, DataGridView1.CurrentCell.Value.ToString(), DataGridView1.CurrentCell.ColumnIndex)
End If
End If
End Sub
Private Sub findOnGridview(g As DataGridView, s As String, c As Integer)
Dim counter As Integer = 0
For i As Integer = 0 To g.Rows.Count - 1
If g.Rows(i).Cells(c).Value = s Then
g.Rows(i).Cells(c).Style.BackColor = Color.Yellow
Else
g.Rows(i).Cells(c).Style.BackColor = Color.White
End If
Next
End Sub

.NET fulltext autocomplete in a combobox. Any performance-positive way of overriding listitems?

I'm struggling to meet a demand from my supervisors. I really hope that someone could give some advice.
Basically there are places in our project where there is a big selection of options. The most concrete example is choosing a city in the world. The items are hundreds of thousands.
Using standard winforms controls and properties, one can search through a list fast.
The problem is that we're using a concatenation of city&district name for all the items. Essentially PREFIX autotomplete works but does not work as needed. The task is to filter and show items by any given string in any part of the item. Essentially a FULL TEXT search in the combobox.
Does anyone have an idea about switching autocomplete sources in runtime relatively qiuckly and handling the suggest/suggestappend event?
Also the project is in VB.NET, though any form of .NET advice will be extremely helpful.
Thanks!
UPDATE: The latest attempt using competent_tech's suggestion with some minor modifications.
Imports System.Data
Public Class Form1
Private _ErrorText As String
Private _CommandExecuted As Boolean
Private m_fOkToUpdateAutoComplete As Boolean
Private m_sLastSearchedFor As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Me.SetStatusText("Loading...")
Me._ErrorText = ""
Me.Cities.Clear()
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Me._CommandExecuted = True
Me.Ara_airportsTableAdapter.Fill(Me.Cities.ara_airports)
Catch ex As Exception
_ErrorText = ex.Message
End Try
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Me._ErrorText = "" Then
Me.SetStatusText(Me.Cities.ara_airports.Count & " Records loaded")
Else
Me.SetStatusText(Me._ErrorText)
End If
Me.BindingSource.ResetBindings(False)
End Sub
Private Sub SetStatusText(ByVal sText As String)
Me.Text = sText
End Sub
Private Sub cboPort_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboPort.KeyDown
Try
If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
m_fOkToUpdateAutoComplete = False
Else
m_fOkToUpdateAutoComplete = True
End If
Catch theException As Exception
' ...
End Try
End Sub
Private Sub cboPort_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPort.KeyUp
Try
If m_fOkToUpdateAutoComplete Then
With cboPort
If .Text.Length >= 2 Then
Dim cSuggestions As IList
Dim sError As String = ""
m_sLastSearchedFor = .Text
cSuggestions = GetSuggestions(m_sLastSearchedFor)
.DataSource = Nothing
If cSuggestions IsNot Nothing Then
.BindingContext = New BindingContext
.DisplayMember = "CName"
.ValueMember = "id"
.DataSource = New BindingSource(cSuggestions, Nothing)
System.Threading.Thread.Sleep(10)
System.Windows.Forms.Application.DoEvents()
.DroppedDown = True
.Text = m_sLastSearchedFor
If .Text.Length > 0 Then .SelectionStart = .Text.Length
End If
End If
End With
End If
Catch theException As Exception
' ...
End Try
End Sub
Private Function GetSuggestions(ByVal searchFor As String) As IList
BindingSource.Filter = "CName LIKE '%" & searchFor & "%'"
Return BindingSource.List
End Function
End Class
The way we address this with very large sets of data (full set of drug information) is:
1) Handle the combo's TextChanged event
2) Within this event, get the list of suggestions that match the user's current input from the database. We leverage the power of database searching to find matches anywhere within the string.
3) When the suggestions are retrieved, bind them to the combobox
4) Wait for a little bit (500ms) to let the UI catch up (we use a combination of System.Threading.Thread.Sleep and System.Windows.Format.Application.DoEvents()).
A couple of notes on this approach:
1) Nothing is bound to the list when the form is first opened
2) We wait until the user has entered at least 4 characters before we start searching to reduce the hit on the DB and improve the user experience (you don't want to show all of the matches for A, for example).
Update with code to show full solution:
Here are some additional notes and code to show the actual process.
The ComboBox should be configured with all of the properties set to their default values with the exception of:
AutoCompleteMode = SuggestAppend
PreferredDropDownSize = 0, 0
Here is the code that we use for our specific situation (searching first four chars) with a placeholder for retrieving and assigning the data:
Private m_fOkToUpdateAutoComplete As Boolean
Private m_sLastSearchedFor As String = ""
Private Sub cboName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboName.KeyDown
Try
' Catch up and down arrows, and don't change text box if these keys are pressed.
If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
m_fOkToUpdateAutoComplete = False
Else
m_fOkToUpdateAutoComplete = True
End If
Catch theException As Exception
' Do something with the error
End Try
End Sub
Private Sub cboName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.TextChanged
Try
If m_fOkToUpdateAutoComplete Then
With cboName
If .Text.Length >= 4 Then
' Only do a search when the first 4 characters have changed
If Not .Text.Substring(0, 4).Equals(m_sLastSearchedFor, StringComparison.InvariantCultureIgnoreCase) Then
Dim cSuggestions As IEnumerable
Dim sError As String = ""
' Record the last 4 characters we searched for
m_sLastSearchedFor = .Text.Substring(0, 4)
' And search for those
cSuggestions = GetSomeSuggestions(m_sLastSearchedFor) ' Your code here
.DataSource = Nothing
If cSuggestions IsNot Nothing Then
' Because this can use the same data source as the list, ensure that
' the bindingcontexts are different so that the lists are not tied to each other
.BindingContext = New BindingContext
.DataSource = cSuggestions
' Let the UI process the results
System.Threading.Thread.Sleep(10)
System.Windows.Forms.Application.DoEvents()
End If
End If
Else
If Not String.IsNullOrEmpty(m_sLastSearchedFor) Then
' Clear the last searched for text
m_sLastSearchedFor = ""
cboName.DataSource = Nothing
End If
End If
End With
End If
Catch theException As Exception
' Do something with the error
End Try
End Sub

I’v some query about my code n VB2005 using DataGrid

I’v some query about my code. Im a beginner in VB2005, I js want to add some item n my dgCart(DataGrid) from my txtISBNInfo & txtTitleInfo (Textbox). Upon addin it, the itemx must be limited into 3, and the dgCart(DataGrid) should not contain any doubling or duplication ov items. What should I do?
In my for each it will to determine if it exists and then only after it has determined it doesn't exist, add a new row. By that I mean... Here I show you my code but regrettably it doesn’t work…
Private Sub btnAddToCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToCart.Click
Dim IsFound As Boolean = False
Dim Count As Integer = dgCart.Rows.Count
For Each dgvRow As DataGridViewRow In dgCart.Rows
If dgvRow.Cells("clmISBN").Value.ToString = txtISBNInfo.Text Then
IsFound = True
'at this point you can actually exit for because you have the info you need.
End If
Next
If IsFound Then
dgCart.Rows.Add(txtISBNInfo.Text, txtTitleInfo.Text)
Else
MsgBox("Already in list!")
End If
End Sub
I think you pretty much had it but you had the if statement at the end swapped
Private Sub btnAddToCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToCart.Click
''//Set a flag saying that we have not found the row yet
Dim IsFound As Boolean = False
''//Loop through each row
For Each dgvRow As DataGridViewRow In dgCart.Rows
''//Compare the cell to our inputed
If dgvRow.Cells("clmISBN").Value.ToString = txtISBNInfo.Text Then
''//Flag the we found the item
IsFound = True
''//No need to loop through the remaining rows, exit the loop
Exit For
End If
Next
''If the item was found in the datagrid
If IsFound Then
MsgBox("Already in list!")
Else ''//Otherwise
''//Add it
dgCart.Rows.Add(txtISBNInfo.Text, txtTitleInfo.Text)
End If
End Sub