In my project is code that measures a string. My problem is that I do not know whether I should move this code over to the paint event or is there are more convenient way of measuring a string. Do I really need the paint event to measure a string or is there a better method? Bearing in mind, that I want the most efficient practical way of achieving this.
[quote=63759:@Oliver Scott-Brown]This does not sound very efficient. It just sounds like, I might as well stick with the old deprecated way of doing this.
Deprecated should be read as “do this the new way as the old one WILL go away at some point”.
Using a Paint event is really NOT necessary and perhaps awkward
The simplest way to do this is to create a global method and have it create a 1x1 image as a static local variable so it only gets allocated once.
You’ll likely also want to make sure you have a means to set the font, font size, bold and other properties of text so what you measure is set up the same as how you intend to use it.
Function MeasureString(s as string) as double
dim p as picture
If you are going to be doing this alot in your program, and the textfont/size are not going to change, the create the picture object as a global property, assign all the attributes one time and then just “return p.graphics.stringwidth(s)” is all you need to call
Sure, it seems ‘efficient’ right now, but ultimately you have no idea about what’s going on behind the scenes, especially when using a third party toolkit. Yeah, it’s less code for you to type, but the recommended replacement ‘might’ be more efficient in the Xojo framework, it might even be more efficient in the OS.
Not to mention, your app is using a function that either the toolkit vendor or OS vendor has marked as depreciated (or unsupported) and therefore you can not expect to function in a newer version, in the same way as it does now.
Save yourself the potential hassle, do it right when you know what is the right way to do it. Heck like I mentioned, the right way to do it, might actually be more efficient.
I had this happen with Mavericks, while I was using a method that worked before, it broke with Mavericks. Once I figured out the right way of accomplishing it, not only did my app use less memory (30% less), it received a speed boost of 200%!
Let me see if I understand: When you need to actually draw into a picture, you need to do so in the paint event of the associated Graphics object. If you need to use a Graphics object for something else, creating a picture and using its Graphics is the right thing to do, but not necessarily its Paint event.