?
Solved

SEPARATE MULTILINE TEXTBOX TO SEVERAL LABELS

Posted on 2011-02-20
17
Medium Priority
?
472 Views
Last Modified: 2012-05-11
Hi All,

I have a multiline textbox.
How could I separate the contains (separate by vbcrlf) to several labels ?

Let's say it has fixed 4 lines and four labels.

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936797
Use something like that

Imports System.IO

Public Class Form1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "Carlie" + vbCrLf + "Paulie"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim reader As TextReader = New StringReader(TextBox1.Text)
        Dim input As String
        Do
            input = reader.ReadLine()
            If Not (input Is Nothing) Then
                Console.WriteLine(input)
                ' Do what you want with the label here
                'mylabel1.text = input
            End If
        Loop Until input Is Nothing
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34936824
Hi HawyLem,

There are several labels.

My problem is how to code to store to those labels.
For example : Address1, Address2, Address3, Address4

Thank you.
0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936833
If you have always 4 labels you could hardcode their names (as i've understood i suppose)

you can use an integer variable to track the line you are reading and then write each line in the appropriate label.

Or maybe I misunderstood your needs?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:emi_sastra
ID: 34936883
I want to know a smart way to code it.

Using counter and select case to do it is easy.

Thank you.
0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936893
For a 4 variables assignment in a managed language I suppose you don't even need any particular code optimization and can have it coded the way you prefer

I don't get it, are you searching for something in particular?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34936911
I wonder I could get like below code.

lblAddress1.text = GetLineData(txtData, 1)
lblAddress2.text = GetLineData(txtData, 2)
lblAddress3.text = GetLineData(txtData, 3)
lblAddress4.text = GetLineData(txtData, 4)

Thank you.
0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936917
You just need a bit of refactoring.

Declare a GetLineData function and pass it two parameters (or just the line number). Make it return the text you chose.

Use a counter integer inside the function  to reach the iteration you need to read the line
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34936922
What is below code do ?

 Dim reader As TextReader = New StringReader(TextBox1.Text)

Thank you.
0
 
LVL 4

Accepted Solution

by:
HawyLem earned 2000 total points
ID: 34936926
    Private Function GetLineData(ByVal textbox As System.Windows.Forms.TextBox, ByVal line_number As Integer) As String
        Dim counter As Integer
        counter = 0
        Dim reader As TextReader = New StringReader(textbox.Text)
        Dim input As String
        Do
            input = reader.ReadLine()
            counter = counter + 1
            If Not (input Is Nothing) And counter = line_number Then
                GetLineData = input
                Exit Function
            End If
        Loop Until input Is Nothing
        GetLineData = ""
    End Function

Open in new window

0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936930
The code

Dim reader As TextReader = New StringReader(TextBox1.Text)

declare a reader variable as a textreader, a character sequential reader
http://msdn.microsoft.com/en-us/library/system.io.textreader(v=vs.80).aspx

and then initialize it as a new stringreader because TextReader is an abstract base class for StringReader

You need a bit of object oriented programming to understand abstract classes and their implementations. The extreme case is an interface and its implementation (all methods are abstract)
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34936952
Yes, I want a bit OOP programming.

Would please provide the function GetLineData code in OOP way or a recursive way to do it  ?

Thank you.
0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936955
._.
0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936961
Sorry I didn't get it..

if you mean creating a class or module for just one function I need to advice it's strongly a programming bad practice. Classes should be created to handle more than just one method, otherwise will only be a massive and useless wrapping (generating mess in the code)

I suggest you to use the function "as is", that is the best way to achieve code clearness.

Don't mind OOP, you're already using it :D
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34936990
Yes, it works.

Thank you very much for your help.
0
 
LVL 4

Expert Comment

by:HawyLem
ID: 34936992
Your welcome :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34937884
Try something like this:
Private Sub txtData_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtData.TextChanged
        Dim ctls() As Control
        For i As Integer = 0 To txtData.Lines.Length - 1
            ctls = Me.Controls.Find("lblAddress" & (i + 1), True)
            If ctls.Length > 0 Then
                ctls(0).Text = txtData.Lines(i)
            End If
        Next
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34939978
Hi Idle_Mind,

Thank you very much for your code.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 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