Suggest Append Count in combobox (vb.net) - vb.net

I'm searching for a way to count my remaining suggest appends in my combobox.
In my example I have a list of 7 items
When I start to type with the suggest append function, this list gets narrowed down. But I don't see any possibility to count these remaining appends.
What my main objective is, is that I do an action once I have only 1 suggest append remaining.
But I can only check on the selectedindex, which is in this case always -1, or my comboboxcount is still 7. I don't see a way to count the remaining suggest appends.
Any idea?

Supposing that your combobox listitems are of string type then this code will do that. First you should create a list of string with combobox items. Then on keyup event of combobox you should create the searchtext which you use to filter list then count. See code below (i have shown also searchtext just to see its value):
Dim lst As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each it In ComboBox1.Items
lst.Add(it)
Next
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
Dim seltext = ComboBox1.SelectedText
Dim searchtext = ""
If seltext <> "" Then
searchtext = ComboBox1.Text.ToLower.Replace(seltext, "")
Else
searchtext = ComboBox1.Text.ToLower
End If
Label1.Text = lst.Where(Function(d) d.ToLower.StartsWith(searchtext)).Count & " - " & searchtext
End Sub
If your combobox listitems are of different object type then you have to populate list with text field of listitem.

I had the same basic idea as Shurki, except I didn't use a list or replace the selected text with a zero-length string.
I use the SelectionStart property of the ComboBox to get a substring from the ComboBox's Text property..
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ComboBox1.Items.Add("Candy")
ComboBox1.Items.Add("Car")
ComboBox1.Items.Add("Crush")
ComboBox1.Items.Add("Canned")
ComboBox1.Items.Add("Can")
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
Dim query As IEnumerable(Of Object) =
From item As Object In ComboBox1.Items
Where item.ToString().ToUpper().StartsWith(ComboBox1.Text.Substring(0, ComboBox1.SelectionStart).ToUpper())
Select item
Debug.WriteLine("Number of items: " & query.Count())
End Sub
End Class

Related

How to transfer data from listbox to textbox in Visual Basic

I did some activity but I cant figure out how to retrieve data from listbox1 to textbox1. Example in the listbox1 there are 4 names: John, Jorge, Joe. Then I want to transfer Joe from listbox1 to textbox1. I did an arraylist where I adding those 3 names in the listbox1 but I didn't know how to retrieve the name "Jorge" from listbox1 to textbox1. Send help.
Here's the code where I try to retrieve one of the name from listbox1 to textbox1
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Here's the whole code
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
Private Sub removeBtn_Click(sender As Object, e As EventArgs) Handles removeBtn.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
End Class
Here's the Image of the interface i try to retrieve the name Joe but it wasn'tshowing
Let's go over the code you posted.
In the retrieve button click event you are comparing and Integer to a String with textbox1.Text = ListBox1.Items.Count This won't compile with Option Strict On. You do have Option Strict On, don't you? You should always have this on.
On the next line, you assign the value of ArrayofNames(x), where x refers to your form level variable, to a text box's Text property. Since the value of x is never changed anywhere in the code this will always return the 0 index in the array. (The first element) I can't imagine why it should matter that the Textbox.Text should equal the count of the ListBox items.
In the add button click event you first declare a local variable x. Any code in the method will use this x, not the form level x. You ReDim (without the Preserve keyword) the array to an array with a single element. Your For loop is silly because the length of ArrayofNames is 1, so it is 0 To 0.
The ArrayofNames will never have more than a single element and it will be overwritten each time the add button is clicked.
Fortunately the Listbox maintains its own collection of items.
We can use this collection to simplify your code. Just add the contents of the text box to the the list items.
To retrieve a value from the list box you must first determine if the user has entered a valid index. First, is the entry a valid Integer and is it an index present in the list box.
Remember that .net collections start at index 0.
I use default names in my test program but you should continue to use meaningful names for your controls.
'Your add button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim index As Integer
If Integer.TryParse(TextBox1.Text, index) AndAlso index >= 0 AndAlso ListBox1.Items.Count - 1 >= index Then
TextBox2.Text = ListBox1.Items(index).ToString
End If
End Sub
'Your add button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
End Sub

How to remove list from textbox after removing the last list of list collection

I have a windows application which input string to be insert into list of collections. Then, displays it in a text box field(Multiple lines). The problem I'm having now is the list is still there in the textbox after I clicked 'Delete button', but the last index is successfully deleted. How can I automatically delete the list in the textbox as well as the list of collection itself? it that possible to do?
These are the code snippet that I have done.
Public strList As List(Of String) = New List(Of String)
'add string to list
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TxtBox.Text <> "" Then
strList.Add(TxtBox.Text)
TxtBox.Clear()
End If
txtList.Clear()
For Each s As String In strList
txtList.Text += s & Environment.NewLine & Environment.NewLine
Next
End Sub
'delete Button
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strList.RemoveAt(strList.Count - 1)
End Sub
Thanks in advance!
Have you tried using
strList.Clear() ?
It's not possible to clear the TxtBox automatically, since it's not bind with the list, you just added the text to the textbox, try to use datasource and listView and bind them together if you want to achieve this.
Thanks
Edited:
In case you want to remove last line from the textbox,
You have to rebuilt the string after you remove your last item from the list, the method will be something like this
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strList.RemoveAt(strList.Count - 1)
txtList.Clear()
For Each s As String In strList
txtList.Text += s & Environment.NewLine & Environment.NewLine
Next
End Sub
Also i prefer to use another control for managing your list, like ListView
VB Listview Basics

vb.net - How to get value from each line in richtextbox and show each values into textboxes

i have a question in my code, Question is
How to get value from each line in richtextbox and show each values into textboxes?
my code is :
Imports System.IO
Public Class Form1
Private Results As String
Private Sub UpdateText()
Dim xList As New List(Of KeyValuePair(Of String, String))
Results = Results.Replace(vbLf, "")
Dim LineSplit() As String = Results.Split(vbCr)
For Each xLine As String In LineSplit
If xLine <> "" Then
xList.Add(New KeyValuePair(Of String, String)(xLine.Split("=")(0), xLine.Split("=")(1).Trim.Replace(" ", "")))
End If
Next
'do some work here to put the values in the right textboxes
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UpdateText()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Results = RichTextBox1.Text
End Sub
End Class
sorry for my bad english, i am from Indonesia, thanks..
Instead of List you could loop over controls of Textbox type.
If number of lines is Always equal to number of textboxes you could loop over your lines or textboxes or even do a static for x=0 to 11 then simply put line x into textbox x.
As we don't know how you named these textboxes I'll show you a way that should work for you:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter As Integer
For Each controlOnForm In Me.Controls
If TypeOf controlOnForm Is TextBox Then
if counter <12 then 'depending how you ordered them you may need to fix this sign
controlOnForm.text = TextBox1.Lines(counter).Split("=")(1)
counter += 1
end if
End If
Next
End Sub

Moving average method VB

i'd try to make moving average in vb
i want to check the cells and set the value to text box
but the result is all the text box has the same value
how to make my first check value (penjualan/bulan) is inputed into first text box and the second check (penjualan/bulan) to second text box.
here is my code
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 5 Then
tb1.Text = DataGridView1.CurrentRow.Cells(3).Value
tb2.Text = DataGridView1.CurrentRow.Cells(3).Value
tb3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
End Sub
thanks.
You set EVERY time the cellClicked-event is raised all 3 Textboxes to the same value, CurrentRow.Cells(3).Value.
Another problem is that your code will set the text in the Textboxes always. It dont check if the Checkbox is checked or not. it just updated every time you click in any cell in this column, the text in the 3 boxes to the value of your currently selected row.
Here you have a solution. Its not perfect but should work, although you should try to understand and optimize it.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDgv()
End Sub
Private Sub InitializeDgv ()
Dim row as String() = New String(){"2016",240}
DataGridView1.Rows.Add(row)
row = New String(){"2017",223}
DataGridView1.Rows.Add(row)
row = New String(){"2015",54}
DataGridView1.Rows.Add(row)
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
if e.ColumnIndex=2
Dim checkedRows=(From dgv as DataGridViewRow in DataGridView1.Rows where dgv.Cells(2).Value=True select dgv).ToList()
Dim controlsList As new List(of TextBox)
controlsList.Add(TextBox1)
controlsList.Add(TextBox2)
controlsList.Add(TextBox3)
for Each item in controlsList
item.Text=String.Empty
Next
for i=0 to checkedRows.Count-1
controlsList(i).Text=checkedRows.Item(i).Cells(0).Value
Next
End If
End Sub
End Class

How to change selected item text in list box at run time?

I tried with code like this:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
The form is looked like this:
I need to change that list text while user typing in the text box. Is it possible to do that at run time?
You are using the form's leave event MyBase.Leave, so when it fires, it is useless to you.
Try using the TextChanged event of the TextBox instead.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
Make sure to check if an item is actually selected in the ListBox:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
Use Double click to select line (item) inside list box and change or modify.
Instead of using text box use ListBox1_MouseDoubleClick event
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
then add this code inside this event
Dim intIndex As Integer = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox1.Items.Insert(intIndex, objInputBox)
End If
OR
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = objInputBox
End If