[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Search for string in a formatted listbox

Posted on 2006-06-05
11
Medium Priority
?
418 Views
Last Modified: 2010-04-23
Hi Experts

In VB.NET I am entering into a listbox the name and EMail address of customers on the same row or index.
If the text in the listbox is not formatted ie(if I dont use the Dim MyFormat as string =" {0,-20}{1,-20} together with String.Format (MyFormat,name,Email) method then the code I have to search the listbox
and select the index of either the name or the Email works perfectly,but if I do format the listbox the search code does not work.
Is there a way to make my search code work with the formatted listbox or format the listbox in another way.
The search code I have is :

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click


Dim strSeparator As String = "   "
        For i As Integer = 0 To lstData.Items.Count - 1
            If CStr(lstData.Items.Item(i)).EndsWith(strSeparator & txtSearch.Text) Then
                lstData.SelectedIndex = (i)
            End If
        Next
        For i As Integer = 0 To lstData.Items.Count - 1
            If CStr(lstData.Items.Item(i)).StartsWith(txtSearch.Text & strSeparator) Then
                lstData.SelectedIndex = (i)
            End If
        Next
    End Sub

Hope somebody can help
0
Comment
Question by:dizzycat
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16832372
What doesn't work when when a string format is specified?  How are the ListBox items formatted?

Bob
0
 

Author Comment

by:dizzycat
ID: 16834197
When the string format is specified my search code for selecting the second item in the row(the Email address) doesn't work,the listbox items are formatted to line up underneath each other.
I hope this is clear to you.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16834394
What does {0,-20}{1,-20} do to the entries?

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dizzycat
ID: 16834470
The above code states how much room the name and then the Email address will take up in the row.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16835530
Maybe like this:

Dim strSeparator As String = "   "
        For i As Integer = 0 To lstData.Items.Count - 1
            If CStr(lstData.Items.Item(i)).EndsWith(strSeparator & txtSearch.Text) OrElse CStr(lstData.Items.Item(i)).StartsWith(txtSearch.Text & strSeparator) Then
                lstData.SelectedIndex = (i)
            End If
        Next

Bob
0
 

Author Comment

by:dizzycat
ID: 16835940
The above code will find and select the index of the name that I enter in the textbox to search for but it will not find and select the index of the Email address if I search for one.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16836000
1) Are you entering single find text and it will either find the name or the email?

2) What does an example entry in the ListBox look like?

   ABCDEFGH, IJLKMNOP       abc@def.ghi

Bob
0
 

Author Comment

by:dizzycat
ID: 16836284
1) YES

2)The listbox looks something like this :

john smith                          john@aol.com
mary jones                         mary@aol.com
peter day                            peter@aol.com
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16836437
Try this then:

         For i As Integer = 0 To lstData.Items.Count - 1
            If CStr(lstData.Items.Item(i)).EndsWith(txtSearch.Text) OrElse CStr(lstData.Items.Item(i)).StartsWith(txtSearch.Text) Then
                lstData.SelectedIndex = (i)
            End If
        Next

Bob
0
 

Author Comment

by:dizzycat
ID: 16836788
Sorry Bob

Still the same problem as before, I can search for the name but it will not work for the Email address.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16933961
Are there extra spaces in the entries?

Would something like this work:

        For i As Integer = 0 To lstData.Items.Count - 1
            Dim item As String = lstData.Items.Item(i).ToString().Trim()
            If item.EndsWith(txtSearch.Text) OrElse item.StartsWith(txtSearch.Text) Then
                lstData.SelectedIndex = i
            End If
        Next

Bob
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question