Solved

VB.Net MultiLine Text Boxes

Posted on 2016-08-26
3
41 Views
Last Modified: 2016-08-27
Here is my problem, i have a multi line Text Box and a List Box on my form.
The user can copy and paste a List of numbers in the multi line text Box.
When he clicks on Process i want to read line for line from text box and then
do some validation and if successful move the item to list box and remove from
text box.
I have my code who does the validation and putting int into the list box,, but having
issues with the removing from text box.

I tried to load the lines into a string list and then remove from string but problem
is that if i remove the item the list no longer has the same count as when i start and
the system will complain index out of range

Any ideas how to solve this cleanly ?
0
Comment
Question by:AlexPonnath
  • 2
3 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41772398
Can you show your code and how you manipulate the lines in the TextBox? Are you using the Lines collection?
0
 

Author Comment

by:AlexPonnath
ID: 41772458
If txtNumbers.Lines.Count > 0 Then
            Dim newList As List(Of String) = txtNumbers.Lines.ToList
            For i As Int32 = 0 To newList.Count - 1

                lbNumbers.Items.Add(AddPhoneFormat(newList.Item(i)))
                ' Remove the first line.  
                'newList.RemoveAt(i)

            Next
        End If

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41772561
Hi Alex;

Try this modified version of your code. It should do what you need.
If txtNumbers.Lines.Count > 0 Then
    Dim newList As List(Of String) = txtNumbers.Lines.ToList
    '' Access the newList in reverse order so that deleted items do not effect un-processed items
    '' by having its index changed and therefor changing all other item index
    For i As Int32 = newList.Count - 1 To 0 Step -1
        '' Because items are being inserted in reverse order use the Insert methods in place of
        '' The Add method and always Insert at index zero.
        lbNumbers.Items.Insert(0, newList.Item(i))
        'Remove the first line.  
        newList.RemoveAt(i)
    Next
    '' Now seeming you are working with a copy of the Items in the TextBox you need to place the 
    '' Items that were not removed back in the TextBox
    txtNumbers.Lines = newList.ToArray
End If

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now