Creating a handler to increase font size - text

I am working on a text app, which includes a button to increase text size of selected text in a field.
Below is the handler I am using, it works fine when all the selectedText is currently the same size. If some of the text is a different size, the handler returns this error:
execution error at line 42 (Operators +: error in left operand), char
68
The handler:
on txtSizeUp
set the textSize of selectedText to the textSize of selectedText + 2
end txtSizeUp
WHat do I need to do to change the size regardless of difference?

Change your handler to use the selectedChunk instead of the selectedText.
on txtSizeUp
set the textSize of the selectedChunk to the textSize of the selectedChunk + 1
end txtSizeUp
And for good measure, of course:
on txtSizeDown
set the textSize of the selectedChunk to the textSize of the selectedChunk - 1
end txtSizeDown
Edit: The handlers above only work if the textSize of the entire selectedChunk is the same. You want to be able to increment the text size even if there are different sizes within the selection. (I had missed that detail in your original question.)
The problem is that the selectedChunk function returns the string "mixed" when there are varying sizes within the selection. That is why you were getting an error; the set statement was trying to add mixed + 1, a data type mismatch. Here is a handler that should do what you want.
on txtSizeUp
put the effective textSize of the selectedChunk into tSize
if tSize is a number then
set the textSize of the selectedChunk to \
the effective textSize of the selectedChunk + 1
else
lock screen
put the long name of the selectedField into tFld
put word 2 of the selectedChunk into tStartChar
put word 4 of the selectedChunk into tEndChar
repeat with x = tStartChar to tEndChar
set the textSize of char x of tFld to \
the effective textSize of char x of tFld +1
end repeat
unlock screen
end if
end txtSizeUp
There will be other approaches that will work, but they will all involve looping through the selected text in some way.

When setting the textSize, LC needs a chunk expression, like line 3 of fld 1 or word 2 to 4 of fld "yourField"
The phrase the selectedText resolves to the actual text of the selection. So if you had "my dog has fleas" in a field, with "dog" selected, your code is asking to:
set the textSize of "dog" to someValue
This is not allowed. The engine would have no idea what to do. You need to modify your script, and your methodology, to make chunk, not text references.

Related

Stata flag when word found, not strpos

I have some data with strings, and I want to flag when a word is found. A word would be defined as at the start of the string, end, or separated a space. strpos will find whenever the string is present, but I am looking for something similar to subinword. Does Stata have a way to use the functionality of subinword without having to replace it, and instead flag the word?
clear
input id str50 strings
1 "the thin th man"
2 "this old then"
3 "th to moon"
4 "moon blank th"
end
gen th_pos = 0
replace th = 1 if strpos(strings, "th") >0
This above code will flag every observation as they all contain "th", but my desired output is:
ID strings th_sub
1 "the thin th man" 1
2 "this old then" 0
3 "th to moon" 1
4 "moon blank th" 1
A small trick is that "th" as a word will be preceded and followed by a space, except if it occurs at the beginning or the end of string. The exceptions are no challenge really, as
gen wanted = strpos(" " + strings + " ", " th ") > 0
works around them. Otherwise, there is a rich set of regular expression functions to play with.
The example above flags that the code that doesn't do what you want condenses to one line,
gen th_pos = strpos(strings, "th") > 0
A more direct answer is that you don't have to replace anything. You just have to get Stata to tell you what would happen if you did:
gen WANTED = strings != subinword(strings, "th", "", .)
If removing a substring if present changes the string, it must have been present.
Regular expressions can be useful for this type of exercise, with word boundaries allowing you to search for whole words indicated by \b, as in "\bword\b".
gen wanted = ustrregexm(strings, "\bth\b")

SmallBasic: How do I convert the hexadecimal value of the getpixel function to an rgb value?

Example Code:
GraphicsWindow.MouseDown = md
Sub md
color = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndSub
This returns a hexadecimal value, but I need to convert this to an rgb value. How do I do so?
The trick to the conversion is dealing with those pesky letters. I have found the easiest way to do this is with a "Map" structure where you equate the hex digits to decimal values. Small Basic makes this extra easy as array's in Small Basic are actually implemented as maps.
I worked up a full example based on your snippet above. You can get it using this Small Basic import code: CJK283
The subroutine below is the important bit. It converts a two digit hex number into its decimal equivalent. It also underscores how limited subroutines are in Small Basic. Rather than a single line for each call as you would see in other languages, where a parameter is passed in and a value is returned, in Small Basic this requires juggling variables inside the subroutine and at least three lines to call the subroutine.
'Call to the ConvertToHex Subroutine
hex = Text.GetSubText(color,2,2)
DecimalFromHex()
red = decimal
Convert a Hex string to Decimal
Sub DecimalFromHex
'Set an array as a quick and dirty way of converting a hex value into a decimal value
hexValues = "0=0;1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;A=10;B=11;C=12;D=13;E=14;F=15"
hiNibble = Text.GetSubText(hex,1,1) 'The high order nibble of this byte
loNibble = Text.GetSubText(hex,2,1) 'The low order nibble of this byte
hiVal = hexValues[hiNibble] * 16 'Combine the nibbles into a decimal value
loVal = hexValues[loNibble]
decimal = hiVal + loVal
EndSub

How to give extra space using live code

I have a scrolling field. It's containing some text and special characters like (---,'',.,",) e.t.c. I want add a single space before every special characters.Is it possible
I d not know how long your text is, but you can:
on mouseUp
get fld "yourField"
put "abcdefghijklmnopqrstuvwxyz" into normalChars
repeat for each char tChar in it
if tChar is not in normalChars then put space & tChar after temp
else put tChar after temp
end repeat
put temp into fld "yourOtherField"
end mouseUp
Now this assumes that all non-letter chars are "special" chars. Can you modify the handler to go the other way? That is, to identify only certain chars, rather than identify all non-letter chars?
Craig Newman
ya. Please use this code
replace "--" with " --" in field "yourField"
replace "---" with " ---" in field "yourField"

VBA Excel Label Generator, Adding a number for each type of label

Morning guys.
I made a Label Generator in Excel, on which I converted the items chosen in dropdown lists to leters which translate to these items chosen.
This convertion is quite simple.
If tipoChosen = "UTP" Then
tipo = "U"
End If
This happens for every user input about the label.
I also had to add a number that made each label different, so I made this line:
FinalLabel = tipo & forn & Format(Nextrow - 2, "00000") & "." & Color & "(" & metragem & ")"
Where the "Format(Nextrow - 2, "00000")" Would give me a number depending on which row it was, beggining in 00000 and going through infinity and beyond(until long capacity ended). This worked and I was happy with it.
My boss just told me that what I did was wrong, and that it needs to reset this number for each type of cable. Meaning that
if all the other parameters stay the same, it keeps counting.
If it changes it goes back to 00000
If it goes back to whatever type I already used, it keeps counting from the previous number.
And I honestly have no clue on how to do this the way I made my code. Will I need to do it from scratch and work my way around this? Or is there a way to implement this feature as it is?
Edit: I also have numerical inputs, things such as the lenght of the cable, thing that will change the label. Meaning that there needs to be a "check" to see if that lenght was also used before. Remember that:
if ALL parameters are identical, keep counting from 00000 to whatever labels are to create. (let's say 00100)
if something changes it starts from 00000
if it goes back to a previous used combination of parameters it KEEPS counting from whatever number it was previously. (meaning the next one would be 00101)
You can definitely rework what you have already.
You'll need to add a counter variable for each type, to keep track of the count. Set these counters to 0 at the start of your code.
Easiest method: Dim an array with an element for each type, which will contain the counter for that type:
Dim i
Dim Counter(1 To 5) As Integer
For i = 1 to 5
Counter(i).Value = 0
Next i
Each time you change your type, update the counter index (i). You could use a select statement here to set tipo & Counter index (i):
Select Case tipoChosen
Case "UTP"
tipo = "U"
i = 1
Case "otherType"
tipo = "X"
i = 2
// etc...
End Select
Then increment Counter for each iteration, until the type changes.
FinalLabel = tipo & forn & Format(Counter(i).value, "00000") & "." &
Color & "(" & metragem & ")"
Counter(i).value = Counter(i).value + 1
There are better ways to do this, but this is probably the easiest solution. If you want the more complex approach, go with dynamic variable names, or using a pointer from main counter to type counter but that’s more complicated….

How to fetch content in livecode

I want to catch content between two $ symbols.
Eg: - This is $ only$ an example $so$ respond $quickly$.
Here I want to store text between dollars (only, so and quickly) into an array.
In using this code for catching. But it catches (only, an example, so, respond, quickly). I need "only", "so" and "quickly".
replace "\$" with "\XXdollarXX" in field "MytextField"
put the text of field "MytextField" into ss
repeat with i = 0 to the number of chars in ss
if char i of ss contains "$" then
repeat with x = i+1 to the number of chars in ss
if char x of ss contains "$" then
--answer x
put x into Lpos
put char i to Lpos of ss into jar
answer jar
put Lpos into i
end if
end repeat
end if
end repeat
If you examine your code while stepping through, you will see this has to work that way. The "offset" function, though, can be used in a loop, and contains a parameter "chars to skip". By continuously updating that parameter, and considering that only every other instance is pertinent, you can collect the text between the pairs of "$" as:
between instance 1 and 2
between instance 3 and 4
etc.
Make a button and a field. Put some text into the field sprinkled with "$" for testing. In the button script:
on mouseUp
put 0 into charsToSkip
repeat until charsToskip > the length of fld 1
if the optionKey is down then exit to top --YOU WILL NEED THIS RIGHT NOW...
get offset("$",fld 1,charsToSkip)
add it to charsToSkip
put charsToSkip & return after accum
end repeat
end mouseUp
I leave it to you to add the ability to exit this loop properly. Step through the handler and you will see the value in accum building in the way I mentioned.
Craig Newman

Resources