How to detect if StyledText will generate Nilobject

I’m passing the textarea TCont as an object to a method that sets text styling.

If I use this code, it will generate a nilobject on the line RemoveStyleRun.

If tCont.StyledText = Nil Then ElseIf tCont.StyledText <> Nil And tCont.StyledText.StyleRunCount > -1 Then While tCont.StyledText.StyleRunCount > -1 tCont.StyledText.RemoveStyleRun(0) Wend End If

StyledText is not nil but it doesn’t have anything in it either.
Styled is set to ON in the layout properties.

What do I do

You could use Textarea.Text to get the plain text without having to remove style runs yourself.
You could also use StyledText.Text to get the plain text from a StyledText without having to remove the style runs yourself.

Edit: Also, reading the documentation, RemoveStyleRun sounds like it would remove the whole section. At the end of your loop, if it worked, you’d just have an empty StyledText.

Thanks Tim.
I just added this line of code

Dim p As Integer = tCont.StyledText.StyleRunCount

p has a value of 1, which is confusing.
The very confusing part is this the very first run, so there’s only the text that I added from an array. The textchange is called and then this fires. It’s all programmatic in otherwords.
From what I read in the LR StyleRuns are persistent. So what my intent is, is to remove that piece of stuff.

Why would there be styleruns?
When I look in RemovestyleRuns it says it is 0 based.

[quote=364514:@Arthur Gabhart]Why would there be styleruns?
When I look in RemovestyleRuns it says it is 0 based.[/quote]
When you’re staring at an empty TextArea it’s got 1 paragraph / StyleRun in it, even though it’s empty to us as humans.
Counts are 1 based, indexes are 0 based.

If you’re just trying to remove the styling I would recommend one of the other methods instead, less moving parts on your end means less maintenance.

This is also a great example of manipulating data with interface objects being a less than reliable method. If anything, you should extract the StyledText from the TextArea and work with the StyledText object separately from the UI. Then, send it back in when your manipulations are done.

Actually that is my intent. It is to create styledtext with the method. I’m trying to figure out styleruns. The examples for manipulating styleruns starting none or 0 are NIL. Grumble.

Elaborate a little bit for me, maybe I can help. I just created an about box that fills a TextArea with styled text created in code for a project.

Start where the user has entered text into the area, and then triggered this functionality (either by button press or focus change or whatever). From the moment the user triggers this functionality, what is it supposed to do?

I changed it to > 0 and no nil. Thanks.

If tCont.StyledText = Nil Then ElseIf tCont.StyledText <> Nil And tCont.StyledText.StyleRunCount > 0 Then While tCont.StyledText.StyleRunCount > 0 tCont.StyledText.RemoveStyleRun(0) Wend End If

The textarea is a called CREdit and is a control set that are both Readonly and editable. DSPRow is the holder of the text. dadta is a string array of relevant data.
It gets set in a method like this (unless it is edited)

  CREdit(i).text = DspRow.dadta(i)

The lines in textchange are

If BeingEdited = False And index < 5 And DspRow <> Nil And FLost Then If index = 2 Then App.SetFontSize(me, False, True, DspRow.dadta(index)) Else App.SetFontSize(me, True, True, DspRow.dadta(index)) End if End If//BeingEdited = False Then
The base for this method is to calculate String height so it will fit completely or readably into a textarea. The start of the method is here from this realsoftware post:
Character Height

If everything works it would look something like this image
WhatARecordLooksLike
I do various calculations like this one for only before a character set that indicates “Underline and Bold”

If ptVal > 0 Then//for error If ptVal > -1 Then //Only for CardReview.CtrlFocus = 2 do the kPrm have to be before the EOL //If locEOL > -1 Then//For bad tCont.SelLength tCont.SelStart = Prime1Ra+1//finds the Pre-Prm0L area tCont.SelLength = ptVal - tCont.SelStart tCont.SelBold = False tCont.SelUnderline = False tCont.TextFont = DispFont
So this code will generate styledtext but it won’t be defined such.
This whole process might be clear now or it might be clear as mud.

About the StyleRunCount. That is confusing. It looks like an array which uses in XOJO ubound, but it is actually like a List.
The LR has a method called “IndexOf” that returns -1 if empty.

[quote=364527:@Arthur Gabhart]So this code will generate styledtext but it won’t be defined such.
This whole process might be clear now or it might be clear as mud.[/quote]
It’s still a little muddy here. I think you’re looking to just make sure the text fits inside the TextArea and adjust the size to do so?

Instead of removing the style runs, I might loop through them and just make them all a tad smaller until your graphics object test says that it will all fit.

[quote=364531:@Arthur Gabhart]About the StyleRunCount. That is confusing. It looks like an array which uses in XOJO ubound, but it is actually like a List.
The LR has a method called “IndexOf” that returns -1 if empty.[/quote]
For this, try to remember what I said above.
Count is 1-based. So 0 count means no objects.
Index is 0-based. 0 index is item 1.

This can get a little confusing when things like FolderItem.Item is an index, but 0 is a no good value. This is because the first item in the array is the parent directory, so we have to start at index 1 with that to get the first item we’re actually looking for.

IndexOf is searching an array for the index of something, so 0 based, -1 is empty.

StyleRunCount is a count, so 0 means empty.

Luckily for us, the documentation frequently tells us what values to expect for empty / non-existant, but I hope I’ve helped.

And when you’re looping objects it’s often like this:

for i as integer = 0 to SomeObject.Count -1 //Get the object dim oTheObject as SomeClass = SomeObject.GetItemAtIndex(i) next

The count shows you how many so it’s clearly one based. The GetItemAtIndex gets you the item based on it being zero based. All arrays in Xojo are zero based.

If you defined your own array:

[code]Dim ars(9) as string
dim count as integer = ars.ubound + 1

//Example One
for iIndex as integer = 0 to ars.ubound
//do something
next

//Example Two
for iIndex as integer = 0 to Count-1
//do something
next
[/code]

Example one and two are exactly the same however Count is taking the Upperbound of the array and adding 1 because count (in English at least) implies 1 based. It’s only in computer science where the first element is zero.

More often than not most things in Xojo work like this. However, there are exceptions.

Thank you for finding the discussion. I will look at that later.
Edit: One of the forum answers I got was no matter what styles I put in it’s all textsyling, so I just want to get it organized and styleRuns are definitely more effective since I’ve already ran into a problem with my code.
There are not shown in the image sometimes several thousand characters and most of that has one style. Yes it is basically to show some text in textarea, but my goal is to have several index or key words
My intention is basically to have, if I underline one set of text, to prepare for 6 StyleRuns. Each part that is underlined is bracketed by text that is or is not hidden. and can also act like an index.
I want to make it more flexible so i can use other keywords.