472,353 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Reading values from a textbox

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
6 16572
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
> 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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
5
by: Robert Phillips | last post by:
I have a Panel control containing a few TextBox controls. The Panel is originally enabled, I enter data into the TextBox controls. When I submit,...
0
by: Newasps | last post by:
Hi guys, I have a problem with UpdateCommand Event. In tihs event İ'm creating required controls to get that controls' values and also get them....
4
by: bwalke | last post by:
I am developing a web form which is going to be used for manufacturing input. The form uses a DataList which list a batch of parts that may range...
2
by: Kiran Kumar Pinjala | last post by:
Hi, May be this is silly, or I just need a second pair eyes to look at this. I am trying to get values that I have edited in a datagrid and...
1
by: KasperBirch | last post by:
Hi NG Im trying to read the values of a bunch of TextBox'es that I add in a loop, dynamically. The page has an <asp:Table id="table" ...>...
7
by: Nez | last post by:
Help needed! Hello, I have looked everywhere for a solution to my problem and this is pretty much my last resource. I have created a table in a...
0
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was...
4
by: pankajsingh5k | last post by:
Hi guys, These question is for all the experts... Please help me before my brain explodes The problem is again with the formview control.....
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.