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