Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I autosize a richtextbox width?

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
Is there a way of making the width of a one line richtextbox grow to suit the width of the characters as they are added. This would have to take care of different size fonts and proportional spacing.
I can do it when adding text thru .seltext by also printing to a dummy form and measuring the text width but this does not work if I fill the richtextbox with .textrtf from a database unless I fill the box, then take the .text out of it, measure it and set the width from that.

I need this for a jumbo scroll across the bottom of a screen.

Incidentally I found that richtextboxes don't jitter when you put them in a frame and scroll the frame with the ccrpTmr6.ocx.
 
Might I suggest that you investigate the EM_FORMATRANGE Windows message.
 
From what I can see EM_FORMATRANGE sets the print line length to the existing text box for printing.
What I want to do is set the richtextbox width to the print (like autosize on a label) but I am probably missing something as usual.
(Richtextbox.width=len(Richtextbox.text) * FontSizeFactor) is OK for fixed width fonts but not for proportional unless I somehow measure the width of each letter and add them all up!

This might be five screens wide for a long sentence of font size 64 letters.
 
>but I am probably missing something as usual

You are indeed missing something. I'll quote the specific bit that is important:

wParam
Specifies whether to render the text. If this parameter is not zero, the text is rendered. Otherwise, the text is just measured
 
(although, as ever, there's a little bit more to it than that ...)
 
Haven't had time to sort through the mire yet!
 
Nope but thanks a lot for all your help - I've been flat out trying to meet another deadline.

And I just got an offer of another project but at the age of 72 I'm trying to retire but they won't let me!

On my other ongoing quest to speed up winsock large file transfers, have you ever found that the Sendcomplete sometimes fires before the file has been completely sent?

Also the packet sizes are not always the same size, mostly 8192 but some around 2k. (250 or so packets for a 1meg pic)
If I put a slight delay in Dataarrival so the packets are all 8k, I get less packets but the overall time is about the same. This points to Winsock being the limit on speed to me (unless I am wrong again.)

I was having intermittent trouble using this to resend the remainder of my pic but if I put a delay of only 2 getticks in the sendcomplete before I sent the remainder, it seems to work fine but of course slows it down by 2ms.
Using 1ghz network on core2 duos that maybe is too fast for ye old VB6?

I am wondering if I should migrate to VBnet of C#?
 
>This points to Winsock being the limit on speed to me

No, I'd agree that, as the code is now fairly optimised, the main bottleneck will be winsock and the network itself. You might be able - as dilettante suggested last year - to get a little more performance out of winsock by investigating overlapped, asynchronous I/O. WHich I have to admit I've never tried implementing in VB.
 
And here is a hint if you get back to this.

What EM_FORMATRANGE measures (when wParam is set to 0) is not necessarily made explicit in the API documention.
The returned value is documented as
Code:
[b]Return Value[/b]

This message returns the index of the last character that fits in the region, plus 1

But how does that help us? We want to know how big a region contains a particlar string, which this doesn't tell us. Well, with wParam set to 0 we can repeatedly call SendMessage, each time widening the region. Once the returned value is greater than the length of our selected text we know that the region would be wide enough to display that many characters, and we simply have to set the width of our RTB to match the width of the region (remembering to take account of the fact that there is some padding in the RTB display caused by borders and 3D appearance)
 
Thanks Ill try it next week.
On the subject of winsock transfer of large file I found an even faster and more reliable way was to send the picture first in one normal statement and accumulate it into a stream at the receiving end, Then use the sender's Sub Sendcomplete to send just 2 bytes (chr(1) and chr(255)) like a sort of password code that a pic has been sent.
I start off with the stream already open,
In the dataarrival I just have it like -
Sub Dataarrival(Bytestotal)
Dim buffer() as byte
Getdata buffer
If bytesTotal=2 then (the only extra statement in the loop)
If buffer(0)=1 and buffer(1)=255 then ProcessData
exit sub
End if
Put buffer into stream here
End if

In Sub Processdata I put it into the propertybag etc.
It sends a 1 meg pic in about 140ms

The chances of the last chunk of the picture being only 2 bytes and equal to 1 and 255 are pretty remote and even if they were I could have a routine in Processdata that if the propertybag errorred you could still add them to the pic.
 
Have not revisited this yet but it is still on my list of things to do (in my retirement!)
 
Your 72 and not allowed to retire ???


I am Younger ..... But have been trying to retire since I was 25 .......

But My Wife wont let me !

Peter
 
tedsmith said:
I am wondering if I should migrate to VBnet of C#?
Programming languages are not like people, i.e. the younger ones are not necessarily faster (often it is the opposite).

Some possible reasons you might move to .NET:
1. Pretty much all object-oriented features are supported (VB6 does not support inheritance)
2. The framework is covers so much, and is implemented fairly consistently (leverage your learning)
3. Microsoft will continue to add to the framework for some time to come, whereas VB6 will no longer be enhanced

But for pure efficiency, and lesser footprint on resources, I think VB6 will still usually beat a .NET solution.

Joe Schwarz
Custom Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top