How can i search using a Text box and Listbox in vb.Net? - vb.net

give me code, in which i can enter a word in a textbox and a listbox appear with a item that has same string in which i enter in a textbox.
Please Help me...

I found the following via Google, which sound like the type of things you want to do:
Autosearch ListBox in VB.NET
(WinForms)
Search Listboxes as You Type
(WinForms or is this VB6?)
Searching for items in a ListBox
(WPF)
Using # 1, here is some code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
List1.Items.Add("Computer")
List1.Items.Add("Screen")
List1.Items.Add("Modem")
List1.Items.Add("Printer")
List1.Items.Add("Scanner")
List1.Items.Add("Sound Blaster")
End Sub
Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
Dim i As Integer = List1.FindString(Text1.Text)
List1.SelectedIndex = i
If Text1.Text = "" Then
List1.SelectedIndex = -1
End If
End Sub

Think pseudocode, you can do this.
Grab the text from the textbox.
Set a pointer / counter to the listbox and loop through each item until the end of the list. If the textbox value has the same value as the listboxitem.text then you've found a match exit the for loop.

Add this code to texboxchange
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text.Trim)
End Sub

Related

variable object name error

I have the following:
a listbox called ListBox1 with a couple of random values in it
a couple of rich textboxs called Rit11 up to Rit 154
a button
When I click a rich textbox, I want to set the a variable to the number that the box is called. After this I select a value from the listbox, and when I press the button, I want this value to move to the selected textbox, and be removed from the listview. I made the following code, but it gives an error.
Private Sub Rit12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rit12.Click
SelBox = "12"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim BoxName
BoxName = ("Rit" & SelBox)
Me.Controls(BoxName).Text = ListBox1.SelectedItem
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
It worked fine, till I tried using the variable name. This rule seems to give the problem:
Me.Controls(BoxName).Text = ListBox1.SelectedItem

HOw to set default value in text box VB+

I am working On VB and Arduino At same time. My main intention is to get data serially displayed on Textbox of VB
Now i wanted to how can assign string or Serial data out put to textbox.
I have google it but the sytax is not working
I created simple textbox AND assigned variable abcd , Now i wanted to display Abcd on text box.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "ABCD"
End Sub
If i wanted to read serial data display it on textbox
Private Sub Current_Read_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
Dim str As String
str = SerialPort1.ReadExisting
Current_Read.Text = str
SerialPort1.Close()
End Sub
So why it not working
Your code doesn't result as you expected because the code which you are using will executed only when you type any thing in the textbox, while using this code you can't able to type anything in the textbx when you type any thing in it will result in "ABCD". to avoid this
Move this code TextBox1.Text = "ABCD" from TextBox1_TextChanged event to form_Load event. that is
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "ABCD"
End Sub
Try this for some reason. Add a button and double click the button so you can write your code to the event Button_Click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
Dim str As String
str = SerialPort1.ReadExisting
Current_Read.Text = str
SerialPort1.Close()
End Sub
run the application. and click the button

vb2010 pass data from one form to another

I am trying to pass a value of a textbox on frmMain to a textbox on frmDepartment. I have tried the following code which I thought would work but it dosen't. I ma a new user to VB and come from a php background where it would have been a simple task of setting a seesion. Can anyone help with this? If you need to see more code, please ask. many thanks
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
In frmMain, I am getting value like this:
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.Show()
End Sub
Which is showing in frmMain ok.
In frmDepartment I have this code
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
'Dim customer As String
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
instead of putting the code within the txtDeptCustomer.TextChanged sub, try putting it within the frmDepartment_load:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
or you could set the frmDepartment text box text on the frmMain button click:
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.txtDeptCustomer.Text = txtCustomerActive.Text
frmDepartment.Show()
End Sub

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub

visual basic List.box question

i have 1 text box and 1 listbox in my VB form.
i want to check duplicate item,compare with textbox.text1 and listbox.list item.
and if textbox.text1 value is '3333' and listbox.list multiple value is '1111' '2222' '3333' '4444'
so how to implement such like duplicate check routine?
so if duplicate detect compare with current text1 value and compare with one of listbox's
value is if detect,want to popup messagebox
thanks in advance
Assuming you are inserting strings into your ListBox you can do this:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim x As String
For Each x In ListBox1.Items
If (x = TextBox1.Text) Then
MessageBox.Show("Error")
Return
End If
Next
ListBox1.Items.Add(TextBox1.Text)
End Sub
If it's another type of object that has a property called Value then you need a small change:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim x As Foo
For Each x In ListBox1.Items
If (x.Value = TextBox1.Text) Then
MessageBox.Show("Error")
Return
End If
Next
ListBox1.Items.Add(TextBox1.Text)
End Sub
Assuming that the ListBox contains strings, you can use the Contains method of the Items collection to check for matches. Example (make a form with a ListBox called '_theListBox', a TextBox called '_theTextBox' and a Label called '_theLabel'):
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_theListBox.Items.AddRange(New String() {"aaaa", "bbbb", "cccc", "dddd"})
End Sub
Private Sub _theTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _theTextBox.TextChanged
If ListBoxContainsItem(_theListBox, _theTextBox.Text) Then
_theLabel.Text = "It's a match"
Else
_theLabel.Text = ""
End If
End Sub
Private Function ListBoxContainsItem(ByVal lb As ListBox, ByVal text As String) As Boolean
' check if the string is present in the list '
Return lb.Items.Contains(text)
End Function