(VB.net) Find specified text on an entire line (not just beginning characters) in a listbox - vb.net

I have a listbox where each line contains a short 3-4 character model number followed by a tab and then the product name the model number corresponds to. I also have a textbox which I am using to search the listbox.
The code I am using so far works somewhat, just not exactly how I would like. If I enter search text it will highlight the results in the listbox but only for the first characters, is there anyway to search the text of an entire line (index) of a listbox?
Right now I am using the following:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
ListBox.SelectedIndex = ListBox.FindString(txtSearch.Text)
End Sub

On the assumption that your line representation is the ToString() of the item in the list:
ListBox.SelectedItem.ToString().Contains(txtSearch.Text)

I got it to work by looping through and using contains. Issues with upper and lower case were giving me incorrect results so I converted the criteria to all lower.
For i = 0 To ListBox.Items.Count - 1
If ListBox.Items(i).ToString.ToLower.Contains(Trim(LCase(txtSearch.Text))) Then
ListBox.SelectedIndex = i
Exit For
End If
Next

Related

All elements of an array cannot be accessed from a for loop in monthCalendar Control - Visual Basic

I have a bit of an issue here with the monthCalendar Control in Visual Basic. What is happening in the program is I have a calendar and a text box. The user selects a date on the calendar, then types in information into the textbox component, then presses the add button. From there the add button sets up a parallel array that contains the selected date and the added string from the text box as a way to add planner information for each date. Then the date is bolded for each time an addition like this has happened.
From there you are suppose to be able to click each bolded date and view the information you added. The issue is this only works on the last added date. For some reason it will only read the last element added to the array. I have the for loop down below. I do not understand why I cannot select any bolded date and view the text added. I have option strict on so I know no data loss is occurring. Also I know the date is being selected and placed into the string each time a date is selected on the calendar because I am displaying it onto a label each time. What is the problem. Is it some logical error I am not catching or is it something else?
Public Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthCalendar1.DateChanged
For index As Integer = 0 To intSizeOfSelectedDatesAry - 1
strSelectedDay = CStr(MonthCalendar1.SelectionRange.Start) ' place the selected date into a string
lblError.Text = strSelectedDay
lblError.Visible = True
Dim strAryDate As String = arySelectedDates(index)
If (arySelectedDates(index) = strSelectedDay) Then
lblInstructions.Text = strSelectedDay
lblSelectedDate.Text = aryTextForDates(index)
index = intSizeOfSelectedDatesAry - 1
End If
Next
End Sub

Replace certain word in textbox with vb and retrieve whole string after

I'm looking to make an application that replaces a users selected word with underscores, the issue here is the fact that it does that well, however, it does not return the entire string but rather the underscores themselves.
tbText.Text = Replace(tbText.Text, tbText.SelectedText,
generateUnderscores(tbText.SelectedText), tbText.SelectionStart, 1)
Generateunderscores is a function i created that returns underscores depending on the number of letters in the selectedtext
tbText is the textbox, when a user highlights it I want this function to run. This will replace the selected text with underscores.
Notice how I make tbText.Text contain this, it then becomes ONLY underscores without the remainder of the text in the textbox.
How can I return the text in the text-box as-well as the underscores in it?
I've tried using a string replace however, the issue with that was it found multiple words instead of the ONE word I wanted removed (selected word)
Thanks.
when a user highlights it I want this function to run I dont know how you will do this part, because there is no TextSelected or SelectedTextChanged event. I used right mouse down. You could try to use Left Mouse Up, but that means the text is changed even if the user wants to made a mistake or wants to change whats selected.
Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs)
Handles TextBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right AndAlso
TextBox1.SelectedText.Length > 0 Then
TextBox1.SelectedText = MakeUnderScores(TextBox1.SelectedText.Length)
End If
End Sub
Function MakeUnderScores(n As Integer) As String
Return New String("_"c, n)
End Function
I am not sure that VB's Replace function wouldn't do the same as String.Replace

i am try exchange data between two textbox in vb.net using vs2010

I'm trying to change a textbox text dynamically from another textbox and When i'm writing in the first textbox, the text that i'm writing must appare in the second textbox. but here problem is that i have not contain id or name destination text box Please help!and i have work in vb.net using vs2010
Welcome to StackOverflow. I will assume you're a complete newbie and will try to be as comprehensive as possible.
Right click on the first text box and choose properties. Go to the first property: (Name). This will give you the name of that text box. Now do the same for the second text box. Now, double click on the first text box. You will see two dropdown lists on top of your code window. The one on the left stores controls like textbox, form, etc., and one on the right shows the properties of the control. Select second textbox name from the first dropdown list and select TextChanged from the second dropdown list. You will get something like this:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Now add this to the above subroutine:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox2.Text = TextBox1.Text
End Sub
I am assuming that your first textbox's name is TextBox1 and second textbox's name is TextBox2. These might be different in your code.
For more details about events, properties, and functions, I recommend you read this article: http://msdn.microsoft.com/en-us/library/ms172576(v=vs.90).aspx. That article is based on Visual Studio 2008 but it is equally applicable for Visual Studio 2010 as well. You will also find additional references and samples on the MSDN Visual Basic site: http://msdn.microsoft.com/en-us/library/vstudio/2x7h1hfk.aspx
Change TextBox1 and TextBox2 with the names of your textboxes:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
TextBox2.Text = TextBox1.Text
End Sub

How do I sum numbers in multiple text boxes while simulatneously putting the sum of those boxes in a seperate box without a button

How do I sum numbers in multiple text boxes while simulatneously putting the sum of those boxes in a seperate box without a button. Ok, I am looking for the code to sum numbers from multiple boxes (2 in this case) and automatically fill another box with the sum without clicking a button or anything. I'm talking about displaying the sum in the third box while numbers are being inputted into the first two boxes. Is this possible? If so, how?
Yes it is possible. You need to add an event handler for the TextChanged event. Add the handler for the two text boxes where the addends are being placed and update the sum in the event handler. The link has an example of parsing the text into numbers and doing something with them. In your case you need to parse two text boxes, you can either share the handler and parse both boxes each time, or add a separate handler for each box and update a member variable in the event and update the sum from the member variables (add a method called UpdateSum or something like that that you call after updating the member variables).
I am a C++/C# programmer so my VB syntax may be slightly off
Private Sub txtMonT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles txtMonT.TextChanged
Dim number1 As Double
Dim number2 As Double
If Double.TryParse(txtMon1.Text, number1)
And Double.TryParse(txtMon2.Text, number2) Then
txtMonResult.Text = (number1 + number2).ToString()
End If
End Sub
Edit - Example of UpdateSum method
Note: _addend1 and _addend2 are assumed to be Private members of the class containing UpdateSum and that they are of type Double and have been set in the event handlers for the two text boxes. It is assumed that UpdateSum is being called from those handlers.
Private Sub UpdateSum()
Dim result As Double
result = _addend1 + _addend2
txtMonResult.Text = result.ToString()
End Sub

finding a solution for find string in listbox

I has 4 item on my form.........
tow listbox ,one button and one text box
I has a listbox 'A' with many items.....
i need a item in a listbox 'B' from listbox 'A'
steps are as follow.....that i like to performe...........
1)enter a word or character in a textbox
2)press a button
3)the list appear in listbox 'B'.......that is character or string start in listbox 'A' that is we write in textbox (for matching)
i need a help in which a item that can be in listbox 'B' is getting for listbox 'A'
that is Starting string or character we enter in the text box.
please try to solve me out..........
Not quite sure I follow. Using the text box' Changed event would be a good trigger instead of a button. Just iterate the list items and check for a match with String.StartsWith. For example:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox2.Items.Clear()
If TextBox1.Text.Length > 0 Then
For index As Integer = 0 To ListBox1.Items.Count - 1
Dim txt = ListBox1.Items(index).ToString()
If txt.StartsWith(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) Then
ListBox2.Items.Add(txt)
End If
Next
End If
End Sub
I don't have an IDE in front of me, and it's been a while since I've done WinForms development, so I may not have the exact event name or other stuff, but you get the idea. This also means my code will be in C# since I'm more familiar with that, but you should be able to find the VB equivalent.
You'll want to bind to the proper event on the text box first. Maybe the KeyPress or KeyUp event? Or TextChanged? You want one that fires any time text changes in the text box. In that event, you'll loop through the items in listbox A and compare their values to the text in the text box. Basic string comparison is all that's needed, if there's a .StartsWith() or something of that nature, otherwise some basic use of .Substring() will do fine (based on the length of the string in the text box).
The loop would likely be something along the lines of:
listboxA.Items.ForEach(i =>
{if (i.StartsWith(textboxA.Text)) listboxB.Items.Add(i);});
Or...
foreach (var i in listboxA.Items)
if (i.StartsWith(textBoxA.Text))
listboxB.Items.Add(i);
Like I said, this is all off the top of my head, so the code may not be exact. But hopefully you get the idea.