473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 16842
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, the Panel is disabled during the PostBack and the...
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. But when I try to get updated values I'm getting the...
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 anywhere from 1-9 parts. The DataList contains 6 ...
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 update the values with those values. Here is the code...
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" ...> control, and I add rows with textboxes like this (from...
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 span with the innerhtml command in my code behind....
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 supposed to be doing that in the oninit event,...
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.. I have a formview and i have to use it that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.