Solved

SEPARATE MULTILINE TEXTBOX TO SEVERAL LABELS

Posted on 2011-02-20
17
470 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
Technology Partners: 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 500 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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

689 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