Scrolling text in a box like in a chat prog

jaypal
05-03-2002, 04:20 PM
Hi, gotta question about how to scroll text in a box, sorta like in a chat session. I'm building an app where I'll have new info coming in periodically, and this needs to be displayed, scrolling previous text, but without losing it. I don't need to edit this text, but may need to save it once I'm done.

For example, the Yahoo Messenger chat interface.

Thanks.

John
05-03-2002, 04:28 PM
What is wrong with the intrinsic TextBox control or the RichTextBox control which would allow collors and formatting?

Orbity

BillSoo
05-03-2002, 04:40 PM
If you use the textbox, you can autoscroll it by setting the seltext property....

Try this:

make a new form and add a textbox and a timer to it.

Set the timer interval to 1000ms. Set the textbox
.Text = ""
.MultiLine = TRUE
.ScrollBars = 2

Add the following code:
Private Sub Timer1_Timer()
Dim s$, i%
s = vbNullString
Do
s = s & Chr$(Rnd() * 26 + Asc("A"))
If Rnd() < 0.2 Then Exit Do
Loop
Text1.SelText = s & vbCrLf
End Sub

Run the program. It will add a random string to the textbox every second. As the words approach the bottom, the box will auto scroll to keep pace. If you want to disable editing, you can set the Locked property to TRUE. This still allows you to select text for copy/paste however.

Nitin Garg
05-03-2002, 10:41 PM
Hi Jaypal...

Here is a peice of code which i think work for you best.

I put a multiline textbox (Text1) and a command button (Command1) on a form and put some lines of text in textbox. Then on button click i first get the total no. of lines in the textbox and then scroll that textbox to the last line. Only one api you have to use is SendMessage.

'The code is:
'---------------

Option Explicit

Private Declare Function SendMessage 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_LINESCROLL = &HB6

Private Sub Command1_Click()
Dim lnTotLin As Long
Dim lnResult As Long

'Get total lines in TextBox
lnTotLin = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)

'Scroll the TextBox to specified line (here last line - lnTotLin)
lnResult = SendMessage(Text1.hwnd, EM_LINESCROLL, 0, lnTotLin)

End Sub

'----------End------------

jaypal
05-04-2002, 01:57 PM
Hey Nitin,

Thanks man, looks promising. I'll give it a try and see. Orbity, I did try the intrinsic TextBox, but the problem was that the text would keep jumping to the beginning whenever new text was added to the existing.

I'll keep you posted on what happens.

Cheers.

John
05-04-2002, 03:00 PM
Originally posted by jaypal
...Orbity, I did try the intrinsic TextBox, but the problem was that the text would keep jumping to the beginning whenever new text was added to the existing.

I'll keep you posted on what happens.

Cheers.

Text1.SelStart = Len(Text1.Text)


As BillSoo pointed out before. This seems to do the trick for me and it is only one statement :D

Orbity

jaypal
05-14-2002, 10:21 AM
Uh, Orbity, I tried BillSoo's code on a Rich text control. Works well.
Thanks all. :D

Oh, by the way, any easy reference to how you control formatting in the Rich Text box? To be more specific - when I display a line of text, I'd like the text to have a different background and Font color. The font color part is OK, but I'm not able to change the background.

Volte
05-14-2002, 10:28 AM
To change the background color of a line of RichText, you need to
1) be using RTB v6, and 2) use raw RTF codes. I wrote a
tutorial that is in Tutors Corner about it (although it only covers
how to do font color, you can color the background by changing
\cf# to \highlight#). Once you learn the RTF codes,
the control will become a whole lot more powerful.

Bramus
05-21-2002, 04:39 AM
OMG .... some hard explanations you guys have ... here's the easy method:

frmMain.txtDebug.Text = DateTime.Date & "@" & DateTime.Time & " " & SentData & vbCrLf & frmMain.txtDebug.Text

that means that the new text is added above the rest ... if you want it the other way around (let the new text be added at the bottom), use this:

frmMain.txtDebug.Text = frmMain.txtDebug.Text & vbCrLf & DateTime.Date & "@" & DateTime.Time & " " & SentData

Thinker
05-21-2002, 07:25 AM
Bramus, it isn't very intuitive for the text to be added to the top.
At least I can't think of a single chat-type app that does it that
way. The concept is the newest text is at the bottom, and
scrolling upwards means the text is older. Since that is the
standard, it would be quite confusing to someone to reverse it.

Squirm
05-21-2002, 08:49 AM
Its not just breaking the standard in terms of display, its also impossibly hard to read. You may not think so, but try it. People living in the "Western world" naturally read from top to bottom and its very hard to read upwards to follow the flow of a coversation.

:-\

Banjo
05-21-2002, 10:13 AM
Well technically, Europeans and our descendants (ie Americans, Australians etc.) read top to bottom. There are many cultures who read bottom to top, for Chinese for example. It is most certainly not a 'human' thing.

John
05-21-2002, 10:30 AM
Chineese read top to bottom too, in columns rather than like us, in rows.

Orbity

Bramus
05-21-2002, 10:32 AM
I know ... but I also posted top to bottom underneath it...

Thinker
05-21-2002, 11:08 AM
Then apparently you didn't understand the original question. It
wasn't how to append text to the box, it was how to keep the
text scrolling so that the last lines were always visible. The
correct answer for that had already been posted, and it doesn't
get any easier than setting SelStart. Your statement that there
was an easier way implied that putting the text at the top was
the correct way.

Banjo
05-21-2002, 05:03 PM
Chineese read top to bottom too, in columns rather than like us, in rows.

Orbity

Oh, maybe your right. I know there are languages that read bottom to top though. Maybe I was thinking of Arabic.

BillSoo
05-21-2002, 05:12 PM
I don't know of any languages that go from bottom to top....the reason is quite practical: when you write from bottom to top, your arm smudges the ink (or originally, clay).

The earliest clay writings used to be written in columns, top down, with the first column on the right and last on the left. But if you were right handed, your arm tended to smudge the first columns written. So early scribes learned to turn the clay tablet sideways and write sideways to match. Eventually, everybody simply learned to read it that way as well. That's why so many alphabet characters resemble pictograms on their sides.

BillSoo
05-21-2002, 05:31 PM
I just did a quick google search....

It seems that Left to Right was standardized by the Romans. The greeks seem to have alternated lines left to right then right to left (zigzag pattern).

Arabic languages, like Hebrew, are right to left.

I also found a language with reads from bottom to top: Ancient Numidean.

jaypal
07-01-2002, 02:40 PM
Wow, guys, I seem to have learnt a whole lot more than I bargained for :D

Anyway, thanks heaps, fellas. By the way, I did manage to change the background color of text in the RTB the hard way - using RTF codes. I guess its not worth it.

I seem to be asking a lot of questions, but here goes -
Any easy way (or API call) to get the no. of lines in a text file? I could of course use a "Do while not eof()" loop and read each line and use a counter, but I wondered if there was a more elegant way to do this.

Thanks.

Banjo
07-01-2002, 03:14 PM
No, I think the read the file and count the lines is the only one.

Squirm
07-01-2002, 04:29 PM
Read the whole file into an string/byte array and count the vbLf's is another way, though probably no faster.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum