By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,680 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,680 IT Pros & Developers. It's quick & easy.

Beginners' question

P: n/a
I have created a Windows Form with TextBox with multiline property True. I
have created three buttons. Button1, Button2, Button3. I want to display the
contents of first line of textbox when some one presses Button1, and so on.
What code should I write to show a particular line of a textbox (when
multiline=true).

Thanks in advance!
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Here is a function that answers the question.

*******************************
Private Function GetLine(ByVal LineNum As Int16, ByVal TextString As String)
As String
Dim startpos As Int32 = 0
Dim endpos As Int32

Dim icount As Int16
' this line is a bit of a cheat
TextString = TextString + vbNewLine
' get the starting position of the requested line.
For icount = 1 To LineNum - 1
startpos = TextString.IndexOf(vbNewLine, startpos + 1)
Next
' get the ending position of the requested line
endpos = TextString.IndexOf(vbNewLine, startpos + 1)
' return the string between start and end positions
Return TextString.Substring(startpos, endpos - startpos)
End Function
*************************

call it like this

***************************
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show(GetLine(1, TextBox1.Text))
End Sub
**************************

Kirk Graves

"Sender" <us**@domain.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
I have created a Windows Form with TextBox with multiline property True. I
have created three buttons. Button1, Button2, Button3. I want to display the contents of first line of textbox when some one presses Button1, and so on. What code should I write to show a particular line of a textbox (when
multiline=true).

Thanks in advance!

Nov 20 '05 #2

P: n/a
Hi Sender,

The TextBox object exposes a Lines property, which is an array of all the
lines, and can be used as follows:

Arrays are usually defined as base 0, so 0 will be the first line, 1 the
second, 2 the third, etc

MsgBox(TextBox1.Lines(0)) ' Display first line
MsgBox(TextBox1.Lines(1)) ' Display second line
MsgBox(TextBox1.Lines(2)) ' Display third line

Of course, there's no error checking in there, and if you try to display a
line that doesn't exist, you'll get an exception. The number of lines in the
array can be determined by TextBox1.Lines.Length, this returns the number of
items in the array. This isn't the index number, so the Length will start at
1, 2, 3, ...

Therefore: MsgBox(TextBox1.Lines(TextBox1.Lines.Length - 1))

Will display the last line of the textbox.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Sender" <us**@domain.com> wrote in message
news:#n**************@tk2msftngp13.phx.gbl...
I have created a Windows Form with TextBox with multiline property True. I
have created three buttons. Button1, Button2, Button3. I want to display the contents of first line of textbox when some one presses Button1, and so on. What code should I write to show a particular line of a textbox (when
multiline=true).

Thanks in advance!

Nov 20 '05 #3

P: n/a
"Kirk" <mc*******@hotmail.com> scripsit:
Private Function GetLine(ByVal LineNum As Int16, ByVal TextString As String)

^^^^^
Why?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bl************@ID-208219.news.uni-berlin.de...
"Kirk" <mc*******@hotmail.com> scripsit:
Private Function GetLine(ByVal LineNum As Int16, ByVal TextString As
String) ^^^^^
Why?
I am afraid I don't understand the question. Why? is a little vague.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #5

P: n/a
Tom's answer is of course the better one.
"Tom Spink" <th**********@ntlworld.com> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
Hi Sender,

The TextBox object exposes a Lines property, which is an array of all the
lines, and can be used as follows:

Arrays are usually defined as base 0, so 0 will be the first line, 1 the
second, 2 the third, etc

MsgBox(TextBox1.Lines(0)) ' Display first line
MsgBox(TextBox1.Lines(1)) ' Display second line
MsgBox(TextBox1.Lines(2)) ' Display third line

Of course, there's no error checking in there, and if you try to display a
line that doesn't exist, you'll get an exception. The number of lines in the array can be determined by TextBox1.Lines.Length, this returns the number of items in the array. This isn't the index number, so the Length will start at 1, 2, 3, ...

Therefore: MsgBox(TextBox1.Lines(TextBox1.Lines.Length - 1))

Will display the last line of the textbox.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Sender" <us**@domain.com> wrote in message
news:#n**************@tk2msftngp13.phx.gbl...
I have created a Windows Form with TextBox with multiline property True. I have created three buttons. Button1, Button2, Button3. I want to display

the
contents of first line of textbox when some one presses Button1, and so

on.
What code should I write to show a particular line of a textbox (when
multiline=true).

Thanks in advance!


Nov 20 '05 #6

P: n/a
"Kirk" <mc*******@hotmail.com> schrieb

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
message news:bl************@ID-208219.news.uni-berlin.de...
"Kirk" <mc*******@hotmail.com> scripsit:
Private Function GetLine(ByVal LineNum As Int16, ByVal TextString
As

String)
^^^^^
Why?


I am afraid I don't understand the question. Why? is a little
vague.


AFAIR, the "native" integer data type since 80386 and, I think, Win95
(several years later...) is Integer = Int32.
--
Armin

Nov 20 '05 #7

P: n/a
Well, actually, in VB 5 and VB 6, an Integer was always just an Int16. A
Long as an Int32, and we had nothing that mapped to an Int64. Besides, an
Int16 was big enough to do the job, so I still don't understand why the WHY?

Kirk

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Kirk" <mc*******@hotmail.com> schrieb

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
message news:bl************@ID-208219.news.uni-berlin.de...
"Kirk" <mc*******@hotmail.com> scripsit:
> Private Function GetLine(ByVal LineNum As Int16, ByVal TextString
> As

String)
^^^^^
Why?


I am afraid I don't understand the question. Why? is a little
vague.


AFAIR, the "native" integer data type since 80386 and, I think, Win95
(several years later...) is Integer = Int32.
--
Armin

Nov 20 '05 #8

P: n/a
"Kirk" <mc*******@hotmail.com> schrieb
Well, actually, in VB 5 and VB 6, an Integer was always just an
Int16. A Long as an Int32, and we had nothing that mapped to an
Int64.
Right
Besides, an Int16 was big enough to do the job,
Since VB4 I usually used Long (unless there was a reason to use Integer).
so I still
don't understand why the WHY?


32 bit integer is usually processed faster than 16 bit integer because the
system is optimized for 32 bit integers.

--
Armin

Nov 20 '05 #9

P: n/a
Hi Kirk, a Currency datatype was 64-bit in VB5/6

The 'why' is, is that you should use Integer (which is Int32) because that
is the standard.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Kirk" <mc*******@hotmail.com> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl...
Well, actually, in VB 5 and VB 6, an Integer was always just an Int16. A
Long as an Int32, and we had nothing that mapped to an Int64. Besides, an
Int16 was big enough to do the job, so I still don't understand why the WHY?
Kirk

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Kirk" <mc*******@hotmail.com> schrieb

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
message news:bl************@ID-208219.news.uni-berlin.de...
> "Kirk" <mc*******@hotmail.com> scripsit:
> > Private Function GetLine(ByVal LineNum As Int16, ByVal TextString
> > As
String)
> ^^^^^
> Why?

I am afraid I don't understand the question. Why? is a little
vague.


AFAIR, the "native" integer data type since 80386 and, I think, Win95
(several years later...) is Integer = Int32.
--
Armin


Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.