Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Data Flow Window

I need to display data (ASCII text) being read from a serial port in a TextBox or on a Label.  I'm reading the data from the serial port into a string using a timer.  The string contains CR and LF characters.  I don't only want to display one string, but as many strings as possible, depending upon the size of the TextBox/Label. If the TextBox/Label is full I want to remove the oldest line. I want the string to be divided only if it contains a CR/LF character or the TextBox/Label is not wide enough. The code also should be as fast as possible.
Any good ideas?
  • 4
  • 3
1 Solution
1.  Start a new Standard.exe project
2.  Add a Command Button (Command1)
3.  Add a Textbox (Text1) and set the Text1.Multiline property to True and the Text1.Scrollbars property to 2.
4.  Copy and paste the following code into the Form1 code window.
5.  Press F5 to run. Type anything you like in the textbox.. and then press the command button to write 51 lines.

<----- Code Begin ----->

Option Explicit
Private m_intTextLines As Integer
Private m_intTextLimit As Integer

Private Sub Form_Load()

'  At design time
'  Please SET Text1.MultiLine = True
'  Please SET Text1.ScrollBars = 2
   m_intTextLimit = 50

End Sub

Private Sub Command1_Click()
   Dim intindex As Integer
   For intindex = 1 To 51
      xAdd2Textbox ("Data " & intindex & vbCRLF)
   Next intindex

End Sub

Private Sub xAdd2Textbox(ByVal strInput As String)
   If m_intTextLines < m_intTextLimit _
      m_intTextLines = m_intTextLines + 1
      Text1.Text = Text1.Text & strInput
      Dim strWork As String
      strWork = Right(Text1.Text, Len(Text1.Text) _
         - (InStr(1, Text1.Text, vbCrLf) + 1))
      Text1.Text = strWork & strInput
   End If

End Sub

<----- Code End ----->

The textbox will automatically accomodate the CrLf (new line) and if the text line is longer than the width of the textbox, automatically wrap it over onto 2+ lines.

You the Comm control's event instead of timer to poll for the data arriaval
Good point vbGuru.. <smile>
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

OTveitAuthor Commented:
I've made some changes and it works. Is there a trick to make the cursor dissapear from the text box? It's also a little bit flickery...
I don't know that you want the Cursor to disappear.. that can be VERY confusing to a user.. and in most cases, users will think that there is something wrong with the computer.

In lieu of that, what I would suggest, is using the Text.MouseIcon property to have the cursor change to a different Icon whenever the user crosses over into Text space.. <smile>.
OTveitAuthor Commented:
Oh, sorry ... a missunderstanding. I do not want the mouse pointer to dissapear, but I want the cursor "|" blinking in the TextBox to dissapear.
That is known as the caret.. <smile>. Change property Text.Locked = True to make it go away.. <smile>.
OTveitAuthor Commented:
Ok, Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now