Solved

SEPARATE MULTILINE TEXTBOX TO SEVERAL LABELS

Posted on 2011-02-20
17
466 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 85

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Get size of each directory on each mapped drive 5 28
Error handling in asp.net site 5 23
SqlServer no dupes 25 34
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

14 Experts available now in Live!

Get 1:1 Help Now