12 Replies - 1618 Views - Last Post: 05 May 2012 - 03:41 PM Rate Topic: -----

#1 stuang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-April 12

Please help me to read specific lines in a TextBox

Posted 14 April 2012 - 02:58 PM

Hi, I'm new to Dreamincode and programming stuffs.

Assuming I have a Form and a Textbox

Posted Image

and a short text like this :

Quote

John B Smith
12345 Mesa, AZ 99999-8888
(123) 456-7890

Age: 65+


What I want is when I copy the text into clipboard and paste it to the TextBox, the Textbox only displays the third line (the phone number). It should look like this :

Posted Image
a
So what code should I use ?

Please help and explain me, I'm a real newbie here :(.

Thank you so much

Is This A Good Question/Topic? 0
  • +

Replies To: Please help me to read specific lines in a TextBox

#2 VB ASRAAF007   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: Please help me to read specific lines in a TextBox

Posted 14 April 2012 - 03:10 PM

Allright here's the issue

i think mayB you can use a label instead of a textbox...try and see for each line use different label and rename the "text" in the propeties...:)
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV   User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Please help me to read specific lines in a TextBox

Posted 14 April 2012 - 03:13 PM

No… you can alter it, using the Lines array.
Try something like this:
Dim temp As String = TextBox1.Lines(2) 'line 3 is line 2 because it starts at zero.
TextBox1.Clear() 'clear the textbox
TextBox1.Text = temp 'set it to our variable 



I would put that code in the TextChanged event of the TextBox, so it does this as soon as you paste.
Was This Post Helpful? 1
  • +
  • -

#4 stuang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-April 12

Re: Please help me to read specific lines in a TextBox

Posted 14 April 2012 - 03:29 PM

View PostDimitriV, on 14 April 2012 - 03:13 PM, said:

No… you can alter it, using the Lines array.
Try something like this:
Dim temp As String = TextBox1.Lines(2) 'line 3 is line 2 because it starts at zero.
TextBox1.Clear() 'clear the textbox
TextBox1.Text = temp 'set it to our variable 



I would put that code in the TextChanged event of the TextBox, so it does this as soon as you paste.


thanks for helping me but it doesn't work :(

It just displays an error System.IndexOutOfRangeException :(

I don't know what's wrong :(
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV   User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Please help me to read specific lines in a TextBox

Posted 14 April 2012 - 03:32 PM

My bad. Sorry, of course that will happen. Try this code:
If TextBox1.Lines.Count = 1 Then 'if there is only 1 line then
'do nothing because our code doesn't apply
Else 'there is more than 1

Dim temp As String = TextBox1.Lines(2) 'line 3 is line 2 because it starts at zero.
TextBox1.Clear() 'clear the textbox
TextBox1.Text = temp 'set it to our variable 

End If


The error is because it changes the text and so raises that event, looking for a non-existent line 2. Try that out :)

This post has been edited by DimitriV: 14 April 2012 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 stuang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-April 12

Re: Please help me to read specific lines in a TextBox

Posted 03 May 2012 - 10:02 AM

Assuming I have a Form, a Textbox and a Button like this :

Posted Image

And a short text like this :

...<br\/> Baths: -\-<br\/> Sqft: 1,692<br\/> Lot: 7,013...


Posted Image

What I want to be helped is when I copy and paste the text into the Textbox, then I press the Button, it only display the numbers after the "Sqft :"

It should be like this :

Posted Image

The texts and numbers are variable, it could be 2,203 or 8,232 or etc..., the "Sqft :" is fixed. Now what code should I use to make the Textbox display only the 4 numbers after the "Sqft :" ?

Please help me. I'm a real newbie at coding and programming stuffs :(
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14352
  • View blog
  • Posts: 57,553
  • Joined: 12-June 08

Re: Please help me to read specific lines in a TextBox

Posted 03 May 2012 - 10:10 AM

Merging duplicate topics.


If I were you I would look into regex (regular expressions). They take a template of what you want to match up on, and checks to see if that applies to a give string or not. Pattern matching!

http://msdn.microsof...y/az24scfc.aspx
http://msdn.microsof...v=vs.80%29.aspx
http://msdn.microsof...y/kweb790z.aspx
http://regexlib.com/...CookieSupport=1
http://www.regextester.com/
Was This Post Helpful? 0
  • +
  • -

#8 stuang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-April 12

Re: Please help me to read specific lines in a TextBox

Posted 03 May 2012 - 10:20 AM

Thanks for reminding me Moderator :smile2:

There is no edit or delete post function ? :( I'm a newbie here :(
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14352
  • View blog
  • Posts: 57,553
  • Joined: 12-June 08

Re: Please help me to read specific lines in a TextBox

Posted 03 May 2012 - 10:48 AM

Quote

There is no edit or delete post function ?

A Change to Edit Times
Was This Post Helpful? 0
  • +
  • -

#10 zeeshanef   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 77
  • Joined: 14-April 12

Re: Please help me to read specific lines in a TextBox

Posted 05 May 2012 - 09:11 AM

put this code in Button click event this will extract 5 digit number including comma after sqft: ?.
        Dim ExtractedValue As String = ""
        Dim StrCharArr() As Char
        Dim Found As Boolean = False
        StrCharArr = Textbox1.Text.ToCharArray() 'string into char array

        
        For cnt As Integer = LBound(StrCharArr) To UBound(StrCharArr)
            If Not (cnt + 11) > UBound(StrCharArr) Then
                If StrCharArr(cnt) = "S" And StrCharArr(cnt + 1) = "q" And StrCharArr(cnt + 2) = "f" And StrCharArr(cnt + 3) = "t" And StrCharArr(cnt + 4) = ":" And StrCharArr(cnt + 5) = " " Then
                    ExtractedValue = StrCharArr(cnt + 6) + StrCharArr(cnt + 7) + StrCharArr(cnt + 8) + StrCharArr(cnt + 9) + StrCharArr(cnt + 10)
                    Found = True
                End If                
            End If
        Next cnt

        If Found = True Then
            Textbox1.Text = ExtractedValue 'show extracted value into same textbox
        Else
            MsgBox("Not found")
        End If

Was This Post Helpful? 1
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14352
  • View blog
  • Posts: 57,553
  • Joined: 12-June 08

Re: Please help me to read specific lines in a TextBox

Posted 05 May 2012 - 09:22 AM

@zeeshanef - let's have the OP work through the code themselves and not just hand out answers!
Was This Post Helpful? 1
  • +
  • -

#12 stuang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-April 12

Re: Please help me to read specific lines in a TextBox

Posted 05 May 2012 - 12:23 PM

Thanks zeeshanef and moderator

I just found out the easier way to do that :balloon:

Imports System.Text.RegularExpressions
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Contains("Sqft: ") Then
Dim Str1() As String
Dim str2 As String
str2 = TextBox1.Text
Str1 = Regex.Split(TextBox1.Text, "Sqft: ") 'Split texts into 2 parts, 1 part before the "Sqft: ", 1 part after it
TextBox1.Text = Str1(1) 'TextBox1 now only displays the part after the word "Sqft :"
TextBox1.Select(0, 5) 'Select the first 5 digits of the part
TextBox1.Copy() 'Copy that 5 digits
TextBox1.SelectAll 'Select the whole textbox
TextBox1.Paste() 'Overwrite the 5 digits into the textbox. Now it displays only the 5 digits :D
End If
End Sub

I know it is not a professional way, but it works perfectly for me. Thanks all for your help :clown:
Was This Post Helpful? 0
  • +
  • -

#13 DimitriV   User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Please help me to read specific lines in a TextBox

Posted 05 May 2012 - 03:41 PM

View Poststuang, on 06 May 2012 - 06:23 AM, said:

...

I know it is not a professional way, but it works perfectly for me. Thanks all for your help :clown:

Yeah. I think some of those lines you have are a bit unnecessary. Like so, we can cut it down a bit:
TextBox1.Text = Str1(1) 'TextBox1 now only displays the part after the word "Sqft :"
TextBox1.Text = TextBox1.Text.Substring(0, 5) 'start index then the length


You could try that out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1