Beginners' question

  • Thread starter Thread starter Sender
  • Start date Start date
S

Sender

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!
 
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
 
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
 
Tom's answer is of course the better one.


Tom Spink said:
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
 
Kirk said:
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.
 
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
 
Kirk said:
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.
 
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 said:
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
 
yes, but we were talking about integer types, not decimal types.

Tom Spink said:
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
 
Tom Spink said:
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.

--

I am struggling to understand this. I use an int16 in a simple sample on a
newsgroup (a sample I should point out that wasn't even the right solution
to the problem), and now we are talking about STANDARDS? Is that really
what this whole chain has been about. Someone got offended that I used an
Int16? Well, why the heck even have it in the Framework if that sort of
thing is going to happen. Shall I check before using VB.net as well, after
all, everyone knows that C# is the standard?

Kirk
 
Kirk,

I don't understand the rumour about such an unimportant part in an example
to a beginner either.

But the registers of every Intel processor after the 80286 is 32bit, I don't
know that assembler, but I assume that a load register from memory is in the
most efficient way 32bits.

That can be the only reason to use (if it is posible) 32bits, but the profit
will be of course not even a fraction from a pico second.

I don't think it has anything to do with standards.
If they do it in C# mostly with 16bit, maybe is that because the people who
do that are still used to 16bit registers.
It has nothing to do with the language you use.

Cor
 
Kirk said:
I am struggling to understand this. I use an int16 in a simple
sample on a newsgroup (a sample I should point out that wasn't even
the right solution to the problem), and now we are talking about
STANDARDS? Is that really what this whole chain has been about.
Someone got offended that I used an Int16? Well, why the heck even
have it in the Framework if that sort of thing is going to happen.
Shall I check before using VB.net as well, after all, everyone knows
that C# is the standard?


_You_ were the one who asked "why". We only tried to explain. You asked
again, we answered again. That's all, it's not a big problem.
 
Hi Kirk,

My appologies if I've offended you.

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Someone got offended that I used an Int16?

Like other posters have said, it's not a big deal. I see no indication in
this thread that anyone was offended by your using an Int16, they were
merely curious.

No worries,

Chris
 
Back
Top