Reading values from a textbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Gray

    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.


  • J French

    #2
    Re: Reading values from a textbox

    On Tue, 19 Aug 2003 12:44:34 +0100, David Gray <police@spamcop .net>
    wrote:
    [color=blue]
    >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.[/color]

    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 "SendMessag eA" _
    (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 "SendMessag eA" _
    (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(Tex t1.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(Tex t1.hwnd, _
    EM_LINEINDEX, _
    LINE_NO - 1, _
    0)
    Me.Print "Line 2 Starts at:"; P
    ' --- Now Get its Length
    L& = SendMessage(Tex t1.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 "SendMessag eA" _
    (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(Lis t1.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.TwipsPer PixelY
    Text1.Left = List1.Left + Rect.Left _
    * Screen.TwipsPer PixelX
    Text1.Width = (Rect.Right - Rect.Left + 2) _
    * Screen.TwipsPer PixelX
    Text1.Height = (Rect.Bottom - Rect.Top + 1) _
    * Screen.TwipsPer PixelY
    Text1.Text = List1.List(List 1.ListIndex)

    Text1.Visible = True
    ' note height of Textboxes
    ' is auto adjusted by windows
    End Sub


    Comment

    • Rick Rothstein

      #3
      Re: Reading values from a textbox

      > I'm working on a program that allows a user to enter notes in a[color=blue]
      > 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. :-)[/color]

      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


      Comment

      • Cookie

        #4
        Re: Reading values from a textbox

        David Gray wrote:[color=blue]
        > 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.
        >
        >[/color]

        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

        Comment

        • NW FLA JEEP

          #5
          Re: Reading values from a textbox

          Dude

          Use split :)

          Take Care



          Dim lines() As String

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






          "David Gray" <police@spamcop .net> wrote in message
          news:cn24kv8vfk plhc2vs0r7m4usd 567fhfo0n@4ax.c om...[color=blue]
          > 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.
          >
          >[/color]


          Comment

          • Rick Rothstein

            #6
            Re: Reading values from a textbox

            > Each note can contain multiple sentences and paragraphs, blank lines[color=blue]
            > 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.[/color]

            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?
            [color=blue]
            > Alternatively can I just dump the entire contents of a text box to a
            > file?[/color]

            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


            Comment

            • Suzette

              #7
              Re: Reading values from a textbox

              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(lngLin es)
              lngCrlf = InStr(1, txtBox, vbCrLf)
              txtLines(lngLin es) = 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" <cookie@yucom.b e> wrote in message
              news:3F425900.1 010600@yucom.be ...[color=blue]
              > David Gray wrote:[color=green]
              > > 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.
              > >
              > >[/color]
              >
              > 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
              >[/color]


              Comment

              Working...