How to manipulate the position of character(s) in a string in LiveCode - livecode

So, I have a very specific problem. I have a random word which I have already generated. I also have a separate string that uses the length of that character, 'n' to repeat "_ " n times to field wordDisplay. These elements of the program work. So, if the random word was "meme", the wordDisplay, on openCard would show "_ _ _ _ ".
Now, I have an input button "a". If this button is clicked, it will check for "a" in the randomword variable and if it is found, it will replace the "_ " in the wordDisplay with "a". So, if the randomword was "name", then before you click "a", the wordDisplay shows "_ _ _ _ ". After you click "a", wordDisplay will show "_ a _ _ ". However, if there is more than one "a", it will reveal all of them.
It is also worth knowing that I plan to have the program do something if there is no letter found.
Being a beginner to LiveCode, this all seems very confusing to me and I would really appreciate someone to share their knowledge on how I would do something like this. Thanks!

There are several ways to do this, but I wouldn't try to do everything in a single field. Unless you use a monospaced font, the text's width will shift each time you swap characters and underscores.
I would suggest using a separate field (or button or graphic) to display each character of the word (named something like letter1, letter2, letter3...), and place a line graphic under each field (named line1, line2, line3...). Create a number of controls that matches the length of your worst-case word, and hide the unneeded controls each time a new word is selected.
By laying the controls out this way, you're guaranteed a relatively consistent alignment regardless of what font is used, or how the card is scaled.
Now you can simply toggle the visibility of each "letter" field and the visibility of each "underscore" line as needed, by looping through the set of controls. Something sort of like:
[ the secret word is 'aardvark' ]
command checkForCharacterMatch pUserChar
local noMatch = true
repeat with N = 1 to length(theSecretWord)
put long id of field ("letter" & N) into theLetter
put long id of graphic ("line" & N) into theLine
if (the text of theLetter = pUserChar) then
show theLetter
hide theLine
put false into noMatch
end if
end repeat
if noMatch then
-- DO PENALTY STUFF HERE
end if
end checkForCharacterMatch
In reality, you don't even need to hide the lines, but this follows your original approach.

Related

I have text in separate lines inside same box, How to make it all in one line

Some of my text are in different lines inside same cell. I want them in single line. How do I bring them in single line ?
Example:
first cell contains:
Hi Ram, I want to go to movie today.
Are you willing to join?
If yes, let me know early.
Example:
Expected output:
Hi Ram, I want to go to movie today.Are you willing to join?If yes, let me know early.
New line in a cell A1 caused by alt+Enter for example, may be removed using a formula such as:
=SUBSTITUTE(A1,CHAR(10)," ")
Where A1 is the cell containing the text to be changed. You can enter the formula above in a different cell of course.
The parameter " " indicates 1 space to replace the line break. You could use any other character.
Another type of line break is CHAR(13). You can remove CHAR(13) using the same function again:
=SUBSTITUTE(SUBSTITUTE(A1, CHAR(13)," "), CHAR(10), " ")
In case you had some spaces already before the new-line character, you need to wrap the above formula in a TRIM function like so:
=TRIM(SUBSTITUTE(A1,CHAR(10)," "))
OR
=TRIM(SUBSTITUTE(SUBSTITUTE(A1,CHAR(13)," "),CHAR(10)," "))
Always make a copy of your file before you apply formulas that could change the data.
Note-1:
char(13) is officially called "carriage return" and char(10) is called "line feed".
CHAR(10) returns a line break on Windows, and CHAR(13) returns a line break on the Mac. This answer is for Windows. You can't visually see it but you can see its effect.
Note-2:
As #kojow7 answered, a text wrap can cause the text to appear on more than 1 line depending on the cell width and the text length. This answer does not resolve this case.
Related discussion can be found here: Remove line breaks from cell.
Two things you may need to fix here: 1) Line breaks and 2) Text Wrapping
To fix line breaks:
Select the cells that need to be changed
Press CTRL+H to open Search and Replace
In the Find box type CTRL+J to insert the line break character (it may look like nothing was inserted in the field, but it does insert a line break)
Decide whether to replace the line breaks with a space or with nothing
Press Replace All
To turn off text wrapping:
Select the cells that need to be changed
Go to the Home Tab
In the Alignment Group check to see if the Wrap Text button is clicked.
If it is, click on it again to deselect it.
Depending on your situation, you may need to fix either one or both of these.
Depending on your document it might contain linefeeds or carriage returns or BOTH.
Alexander Frolov (https://www.ablebits.com/office-addins-blog/2013/12/03/remove-carriage-returns-excel/) has written a very good blog post about different technics of finding and removing linebreaks in an Excel file. We will use the “macro way” of doing that – as it is the one that works either on Windows AND Mac. The search replace method offered here too will not work on Mac but on windows.
Add the below Macro to your document (slighlty modified from the original)
Change the value of “ReplaceWith” from ” ” (space) to anything you like a linebreak to be replaced with.
E.g. ReplaceWith = “-” will result in “Line1-Line2-Line3”
Run the Macro (Extras > Macro) while all cells are selected.
Sub RemoveCarriageReturns()
ReplaceWith = " "
LinefeedChar = Chr(10)
Dim recordRange As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each recordRange In ActiveSheet.UsedRange
If 0 < InStr(recordRange, LinefeedChar) Then
recordRange = Replace(recordRange, LinefeedChar, ReplaceWith)
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
If your separate lines are not gone by now please change "LinefeedChar" from "Chr(10)" to "Chr(13)" and run it again

Splitting a string of a multiline textbox by lines

I want to split the text of a textbox after a specific amount of visible lines.
I've found some codes that "allows that", but all of them consider the lines by the "vbCrLf" parameter, but i want to split using the visible lines of a multiline textbox.
To make it more clear to understand, consider a multiline textbox with the following text:
"The history of textbooks dates back to civilizations of ancient history. For example, Ancient Greeks wrote texts intended for education. The modern textbook has its roots in the standardization made possible by the printing press. Johannes Gutenberg himself may have printed editions of Ars Minor, a schoolbook on Latin grammar by Aelius Donatus. Early textbooks were used by tutors and teachers, who used the books as instructional aids (e.g., alphabet books), as well as individuals who taught themselves."
When i use the Textbox.Linecount function it returns the number 6 because the textbox shows six lines (which depends on the size of the control), but if i use a function like strText = Split(TextBox.Text, vbCrLf) it will return 1, because there is only one vbCrLf. But i need to split the text into two textbox considering the visible lines of the control, something like what happens in page breaks of MS Word.
For a better visual explanation, please look at the attached image.
Example
Firstly, I'm not convinced there is a robust and elegant way to do this, but it was fun to experiment and it might be useful to you.
The following will split the contents of TextBoxInput into TextBoxPage1 and TextBoxPage2 breaking on the line number specified by PAGED_TEXT_BOX_LINES.
It uses the textbox itself to detect natural line breaks and thus implicitly caters for the size of the textbox, the font, etc.
The desired line count is hard coded as a constant - not doing this would require an alternative of calculating the line height of the textbox (requiring calculations based on the font metrics and the textbox's internal line-leading size).
It only handles two "pages". But the concept could be extended simply by repeating the process based on the remainder of text that ends up in TextBoxPage2.
Private Sub CommandButton1_Click()
Const PAGED_TEXT_BOX_LINES As Integer = 5
Dim text As String
Dim i As Long
Dim textLength As Long
Dim curLine As Integer
text = TextBoxInput.text
textLength = Len(text)
TextBoxPage1.SetFocus
'add characters of the input string until the first page textbox
' exceeds maximum line count
For i = 1 To textLength
TextBoxPage1.text = Mid$(text, 1, i)
If TextBoxPage1.LineCount > PAGED_TEXT_BOX_LINES Then
'retreat cursor until we reach previous line, so we can
' detect the word that wrapped
curLine = TextBoxPage1.curLine
Do While TextBoxPage1.curLine = curLine
TextBoxPage1.SelStart = TextBoxPage1.SelStart - 1
Loop
'the remaining text after the SelStart is what
' wrapped, so stop page 1 after SelStart
TextBoxPage1.text = Mid$(text, 1, TextBoxPage1.SelStart)
TextBoxPage2.text = Trim$(Mid$(text, TextBoxPage1.SelStart + 1))
Exit For
End If
Next i
End Sub

Tkinter selecting text by index

I am implementing a search function in python with Tkinter and would like to select the first match it comes to. I have seen many examples with creating a tag_config to highlight the background of the indexed range, however I would like to select the text (the same way one would by clicking at the first index, then shift clicking the last index).
Thus far I have got both the start and end index of the area I need to select, I just don't know the command to "select" the text with that information.
My current code (that uses a highlight approach) is:
def search_command():
word = askstring("Search", "Enter word to search")
length = len(str(word))
pos = textPad.search(word, '1.0', stopindex=END)
row, col = pos.split('.')
endlen = int(col) + length
end = row + '.' + str(endlen)
textPad.tag_add("found", pos, end)
The "found" tag just highlights the background of the text rather than selecting it.
Any help with finding the correct function would be greatly appreciated.
The selection is defined by the "sel" tag. Apply that tag to the range of text you want selected:
textPad.tag_add("sel", pos, end)

VBA: How to start and end a list, bulleted or numbered, in Word?

I just can't figure out how to get VBA to start a bulleted list in Word.
I've got some code that types out stuff into word, I can get font and paragraph formatting, no problem, but now I want to create a bulleted list. I've found the following code,
ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(2)
which should create a bulleted list of the second standard type, but all I can determine is to use it with a 'Range' command which causes the entire document to have the list applied to it. What I'd like to do is have it applied just to the new line that I'm having the code type, and then, at some point, be able to turn the list off, to be able to continue without the list being applied.
Thanks!
This link should help you with your query:
VBA - Bullet Points
Basically this code applies it to a selection:
Selection.Range.ListFormat.ApplyBulletDefault
And this code adds it to the selected paragraph number (in this case paragraph 2):
Documents("MyDoc.doc").Paragraphs(2).Range.ListFormat _
.ApplyBulletDefault
This code applies the Bullet points to a range of paragraphs:
Set myDoc = ActiveDocument
Set myRange = myDoc.Range( _
Start:= myDoc.Paragraphs(3).Range.Start, _
End:=myDoc.Paragraphs(6).Range.End)
If myRange.ListFormat.ListType = wdListNoNumbering Then
myRange.ListFormat.ApplyBulletDefault
End If
Assuming you know the text that is being added, you can use the second example. If you don't know how many paragraphs are being added, then each time you create a new one, increment an integer by 1 and use that integer in the third example.
For Example:
Start:= myDoc.Paragraphs(2).Range.Start, _
End:=myDoc.Paragraphs(i).Range.End)

In VBA for Excel, How can I manipulate a header/footer when the string contains '&'

I have a section of code that writes to the header section of the page. The problem is that the string that is written to the header occasionally contains a '&' symbol. This interferes with the code because VBA automatically views & and the following character as a piece of code, even if its part of the string.
ActiveSheet.PageSetup.CenterHeader = "&B&14 Cool New Header for &16 &I" & vbNewLine & aString
Everything works great when aString has no '&' symbol in it. I get a bold, size 14 top line followed by the size 16, italicized aString text on a second line.
In the event that aString is something like 'B&S Company of Greatness' the header will come out with "Company of Greatness" stricken out because of the '&S' contained within the string.
How can I get around this? I'd do a search in string for '&S and compensate manually by inserting another &S to cancel it out but &S might not be the only occurance of the '&' symbol that occurs.
What's the best way to get around this? Would properly diming things help by telling excel to read aString ONLY as a string value and to not apply its contents improperly?
You need to escape the ampersand by adding a second ampersand to each instance in the string, like this:
aString = Replace(aString, "&", "&&")
ActiveSheet.PageSetup.CenterHeader = "&B&14 Cool New Header for &16 &I" & _
vbNewLine & aString

Resources