Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SEPARATE MULTILINE TEXTBOX TO SEVERAL LABELS

Posted on 2011-02-20
17
Medium Priority
?
478 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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