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

Reading values from a textbox

P: n/a
Greetings all,

I'm working on a program that allows a user to enter notes in a
multiline textbox. I would like to be able to read the contents of
the textbox (as records - one per line) and store in an array then a
file.

Perhaps this is the wrong control to use as there seems no way of
referencing each line of the text box.

Therefore my question is this: What is the best control to use that
will allow a user to enter one of more lines of text in a form then
write away to a file afterwards.

Apologies if this is a noddy question - too long using VAX-BASIC. :-)

Dave.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 19 Aug 2003 12:44:34 +0100, David Gray <po****@spamcop.net>
wrote:
Greetings all,

I'm working on a program that allows a user to enter notes in a
multiline textbox. I would like to be able to read the contents of
the textbox (as records - one per line) and store in an array then a
file.

Perhaps this is the wrong control to use as there seems no way of
referencing each line of the text box.

Therefore my question is this: What is the best control to use that
will allow a user to enter one of more lines of text in a form then
write away to a file afterwards.


It is quite possible to get individual lines from a Multi-Line Textbox

Whether it is the right control is another matter

Here is how to get the Lines - another example follows this :-

Option Explicit
' Add one Textbox
' Set it to MultiLine

Private Declare Function SendMessage Lib _
"user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Function SendMessageStr Lib _
"user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String) As Long

Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETLINE = &HC4
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEINDEX = &HBB
Private Sub Command1_Click()
Dim Lines&, S$, P&, L&
Const LINE_NO = 2

' --- Count the Lines
Lines& = SendMessage(Text1.hwnd, _
EM_GETLINECOUNT, _
0, _
0)
Me.Print Lines
' Note: If Text1.Text = "" then 1 is returned

' --- Now get position of start of 2nd line
' this is zero based
P& = SendMessage(Text1.hwnd, _
EM_LINEINDEX, _
LINE_NO - 1, _
0)
Me.Print "Line 2 Starts at:"; P
' --- Now Get its Length
L& = SendMessage(Text1.hwnd, _
EM_LINELENGTH, _
P, _
0)
Me.Print "Length of Line 2 is:"; L
' --- Now Get Line 2
S$ = Space$(L)
L& = SendMessageStr(Text1.hwnd, _
EM_GETLINE, _
LINE_NO - 1, _
S)
Me.Print S$
' --- And to prove it
Me.Print Mid$(Text1.Text, P + 1, L)

End Sub

========== END OF FIRST SAMPLE ============

This puts a Textbox over a Listbox
- it is also not ideal, but may give you some ideas

IMO one is far better off creating UserControls out of the simpler
Controls, rather than using more complex things that invariably behave
in a way that is not quite what one wants.

Option Explicit

Private Declare Function SendMessage _
Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Const LB_GETITEMRECT = &H198

Private Const LB_ERR = -1

Private Type TRECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Sub Form_Load()
Dim L9%

List1.Font.Size = 10
For L9 = 1 To 20
List1.AddItem "This is item" + Str$(L9)
Next
Text1.ZOrder vbBringToFront
Call MoveBox
End Sub

Private Sub List1_Click()
Call MoveBox
End Sub

Private Sub List1_Scroll()
Call MoveBox
End Sub
Private Sub MoveBox()
Dim Rect As TRECT, Q&

If List1.ListIndex < 0 Then
Text1.Visible = False
Exit Sub
End If

Q = SendMessage(List1.hwnd, LB_GETITEMRECT, _
List1.ListIndex, Rect)
If Q = LB_ERR Then
Text1.Visible = False
Exit Sub
End If

Text1.Top = List1.Top + Rect.Top _
* Screen.TwipsPerPixelY
Text1.Left = List1.Left + Rect.Left _
* Screen.TwipsPerPixelX
Text1.Width = (Rect.Right - Rect.Left + 2) _
* Screen.TwipsPerPixelX
Text1.Height = (Rect.Bottom - Rect.Top + 1) _
* Screen.TwipsPerPixelY
Text1.Text = List1.List(List1.ListIndex)

Text1.Visible = True
' note height of Textboxes
' is auto adjusted by windows
End Sub
Jul 17 '05 #2

P: n/a
> I'm working on a program that allows a user to enter notes in a
multiline textbox. I would like to be able to read the contents of
the textbox (as records - one per line) and store in an array then a
file.

Perhaps this is the wrong control to use as there seems no way of
referencing each line of the text box.

Therefore my question is this: What is the best control to use that
will allow a user to enter one of more lines of text in a form then
write away to a file afterwards.

Apologies if this is a noddy question - too long using VAX-BASIC. :-)


I'm a little unclear as to what your question is actually asking... Each
note can span more than one display line (that is, each note can wrap its
text), right? Are you trying to read each **displayed** line of text
individually (whether that is a full sentence/paragraph or not)? Or are you
trying to read each sentence (no matter how many display lines to
encompasses)? Or are you trying to read each individual paragraph (which I
assume are separated from each other by a blank line (produced by hitting
the Enter Key twice)?

Rick - MVP
Jul 17 '05 #3

P: n/a
David Gray wrote:
Greetings all,

I'm working on a program that allows a user to enter notes in a
multiline textbox. I would like to be able to read the contents of
the textbox (as records - one per line) and store in an array then a
file.

Perhaps this is the wrong control to use as there seems no way of
referencing each line of the text box.

Therefore my question is this: What is the best control to use that
will allow a user to enter one of more lines of text in a form then
write away to a file afterwards.

Apologies if this is a noddy question - too long using VAX-BASIC. :-)

Dave.


Text-control is fine...

But consider text-wrapping (like Rick Rothstein said).
Anyway: to check for individual lines in textbox: check for
chr(13)+chr(10). Or was it chr(10)+chr(13)? Or just chr(10)
or just chr(13)..... Don't know anymore ;-)

Homework 'How to figure this out?':
Create a text-control, write some text in it (make sure it
wraps around) Enter a Following line or two which don't wrap.
Read out the text-control-value and see what characters (like
chr(13), chr(10), chr(10)+chr(13) or chr(13)+chr(10)) are used
to seperate the lines.....

Cookie

Jul 17 '05 #4

P: n/a
Dude

Use split :)

Take Care

Dim lines() As String

lines = Split(Text1, vbCrLf)
MsgBox lines(0)
MsgBox lines(1)


"David Gray" <po****@spamcop.net> wrote in message
news:cn********************************@4ax.com...
Greetings all,

I'm working on a program that allows a user to enter notes in a
multiline textbox. I would like to be able to read the contents of
the textbox (as records - one per line) and store in an array then a
file.

Perhaps this is the wrong control to use as there seems no way of
referencing each line of the text box.

Therefore my question is this: What is the best control to use that
will allow a user to enter one of more lines of text in a form then
write away to a file afterwards.

Apologies if this is a noddy question - too long using VAX-BASIC. :-)

Dave.

Jul 17 '05 #5

P: n/a
> Each note can contain multiple sentences and paragraphs, blank lines
may also be present. I don't care about the whether a sentence spans
multiple lines, I just need to put each line into an array so I can
dump it to a file.
You still didn't make it clear whether you wanted to preserve the
**displayed** line structure or not. That is, did you want to preserve the
line wrapping exactly as seen on the screen or not?
Alternatively can I just dump the entire contents of a text box to a
file?


This question makes it seem like you don't want the **displayed** line
structure, only the sentence/paragraph structure. Assuming that, you just
want to save out the Text property of the TextBox (assumed to be named Text1
for this example). Something like this.

Dim FF As Long
Dim FileName As String
FileName = <<Put Name Of Storage File Here>>
FF = FreeFile
Open FileName For Output As #FF
Print #FF, Text1.Text
Close #FF

The above creates a new file using the name you provide in the FileName
variable above: OR completely overwrite an existing file, if any, that has
that same name (thus, you would lose its contents). If there is an existing
file by that same name, and if you want to append the contents of the
TextBox onto instead, use this Open statement instead of the one above (and
leave the other lines as is).

Open FileName For Append As #FF

Rick - MVP
Jul 17 '05 #6

P: n/a
Create a loop that looks for the return character then extracts the
information prior to that. Line 1,
Do it again until no more returns are found. Ta da.. all in separate lines.

Private Sub BreakLine()
Dim txtBox As String
Dim txtLines() As String
Dim lngLines As Long
Dim lngCrlf As Long
lngStart = 1
txtBox = Text1

Do While InStr(1, txtBox, vbCr) <> 0
ReDim Preserve txtLines(lngLines)
lngCrlf = InStr(1, txtBox, vbCrLf)
txtLines(lngLines) = Left(txtBox, lngCrlf - 1)
txtBox = Mid(txtBox, lngCrlf + 2, Len(txtBox) - lngCrlf)
' You need to add 2 to the carriage return because a blank space is in there
for some reason.
lngLines = lngLines + 1
Loop

End Sub
Suzette

"Cookie" <co****@yucom.be> wrote in message
news:3F**************@yucom.be...
David Gray wrote:
Greetings all,

I'm working on a program that allows a user to enter notes in a
multiline textbox. I would like to be able to read the contents of
the textbox (as records - one per line) and store in an array then a
file.

Perhaps this is the wrong control to use as there seems no way of
referencing each line of the text box.

Therefore my question is this: What is the best control to use that
will allow a user to enter one of more lines of text in a form then
write away to a file afterwards.

Apologies if this is a noddy question - too long using VAX-BASIC. :-)

Dave.


Text-control is fine...

But consider text-wrapping (like Rick Rothstein said).
Anyway: to check for individual lines in textbox: check for
chr(13)+chr(10). Or was it chr(10)+chr(13)? Or just chr(10)
or just chr(13)..... Don't know anymore ;-)

Homework 'How to figure this out?':
Create a text-control, write some text in it (make sure it
wraps around) Enter a Following line or two which don't wrap.
Read out the text-control-value and see what characters (like
chr(13), chr(10), chr(10)+chr(13) or chr(13)+chr(10)) are used
to seperate the lines.....

Cookie

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.