Solved

SEPARATE MULTILINE TEXTBOX TO SEVERAL LABELS

Posted on 2011-02-20
17
464 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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

14 Experts available now in Live!

Get 1:1 Help Now