Rolling text in rich text box
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Rolling text in rich text box

  1. #1
    Join Date
    May 2005
    Posts
    39

    Rolling text in rich text box

    hi all,
    I have a string in my project.

    it is like "ABBNK 120 up | BXTX 66 down | CTBNK 800 down|" and so so

    Here
    ABBNK is Stock Name
    120 is price
    up is its trend

    I just wanna roll this string through a richtext box (like a stock ticker) and in different color.
    (green for up, red for down)


    Is it possible??

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,986

    Re: Rolling text in rich text box

    RTB's don't scroll ACCROSS. This will scroll DOWN

    Code:
    Option Explicit
    
    Private Declare Function SendMessage _
     Lib "user32.dll" Alias "SendMessageA" ( _
     ByVal hwnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Long, _
     ByRef lParam As Any) As Long
    
    Private Const EM_GETFIRSTVISIBLELINE As Long = &HCE
    Private Const EM_LINESCROLL As Long = &HB6
    
    Public Sub SetTopIndex(rtb As RichTextBox, ByVal nLine As Long)
        Dim nIndex As Long
        nIndex = nLine - SendMessage(rtb.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
        Call SendMessage(rtb.hwnd, EM_LINESCROLL, 0&, ByVal nIndex)
    End Sub
    
    Private Sub Form_Load()
      rtb.Text = ""
     rtb.SelText = "This is a text, this is only a text test, which is long"
     SetTopIndex rtb, 0
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Rolling text in rich text box

    In fact, a RichTextBox DOES scroll horizontally, too, for has it not got a horizontal scrollbar option?
    But in the end I think this will not provide for the ticker effect you want.
    I'd say you have to program it yourself, using a timer control which 'ticks' the text on, by deleting the first character of the vertical oriented RichTextBox and appending the next to it's end.
    Coloring is a different story.

    I'd do it like this:
    Assumed you have a vertical rtb box, arranged like a ticker line.
    First fill in as many blanks (or dots) to fill the visible field.
    Then you split your original string into an array, splitting by "|"
    Then you append each partial string to the rtb, coloring it depending on the occurrence of up or down.
    After all strings are appended, you start the timer, which will simply always delete the first character in the rtb, thus advancing the text through the line with the time specified by the timer interval.

    If you have problems to implement the algorithm like discribed, please come back for more detailed help.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Rolling text in rich text box

    In fact, come to think of it, scrolling is easier with the scrollbar:
    Put a RichTextBox named rtb on a form.
    Set its .MultiLine property to false and the .Scrollbar property to rtfHorizontal
    Put a Timer Timer1 on your form. Set the interval to 80
    The following code fills the rtb with the ticker line, colors it and scrolls it by timer control.
    Code:
    Private Sub Form_Load()
      Dim n%, p%
      'this only fills in a dummy ticker line. Tick should contain your ticker line to show.
      For n = 1 To 10: Tick = Tick + "ABBNK 120 up | BXTX 66 down | CTBNK 800 down | ": Next
      
      'fill the rtb with a set of dots (or spaces) depending on its size, + the ticker string
      rtb.Text = String(PreLen, ".") + Tick
      
      'now do the coloring
      n = PreLen + 1
      Do
        rtb.SelStart = n - 1
        p = InStr(n, rtb.Text, "|")
        If p = 0 Then p = Len(rtb.Text)
        rtb.SelLength = p - n
        rtb.SelColor = IIf(InStr(rtb.SelText, "up"), vbGreen, vbRed)
        n = p + 1
      Loop Until p >= Len(rtb.Text)
      rtb.SelStart = 0
    End Sub
    
    Private Sub Timer1_Timer()
      SendKeys "{right}"
    End Sub
    Also the timer control is rather primitive it works fine.
    If necessary the timer can use SendMessage to scroll the rtb forward. Or there is maybe an even better way to emulate the right arrow key.

  5. #5
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: Rolling text in rich text box

    Correct me if I am wrong but it seems to me that the intention would be to continually scroll this text. In other words when it gets to the end of the data string it begins scrolling the first again.

    What I would suggest is to use something similar to post number 3 but rather than delete the first character you would move the first character to the end of the string thereby creating a never ending marquee of this text though I would imagion given the content that these values would also need to be modified at some interval. What was up may be down on the next pass in some cases.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    May 2005
    Posts
    39

    Re: Rolling text in rich text box

    Thanks to WoF.
    I m proceeding using ur solution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center