Highlight specific words in a RichTextBox - richtextbox

How can i do this: I press on a button and then it finds every occurrence of the word let's say "test" and changes the word's fore color to aqua...
txtText.Find("dim")
txtText.SelectionColor = Color.Aqua
If i do this it only finds the first occurrence, makes it aqua and if i press it again it turns all the text like that. Can anyone help me?

You can write this code, to do that:
First you need button(FindBUT), and RichTextBox(TextBox)
Public Class Research
Private Sub FindBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindBUT.Click
'Check button text
If FindBUT.Text = ("Find ""dim""") Then
'Return all text color to black
TextBox.ForeColor = Color.Black
'Find "dim" word
TextBox.Find("dim")
TextBox.SelectionColor = Color.Aqua
'change button text to ("Find all")
FindBUT.Text = ("Find all")
'Check button text
ElseIf FindBUT.Text = ("Find all") Then
'change all text color to Aqua
TextBox.ForeColor = Color.Aqua
'change button text to ("Find ""dim"" ")
FindBUT.Text = ("Find ""dim""")
End If
End Sub
End Class

Related

How to set the cursor to any part of the text using TextFieldValue (w/ FocusRequester) on pressing/clicking the TextField

I tried to look around but I can't find a way to
force a focus,
set the cursor to the end of text
and still be able to set the cursor to any part of the text when
pressing/clicking.
With FocusRequester the cursor is set to the start of text, but with TextFieldValue(<text>, <range>) I'm able to place the cursor at the end, the problem is that with this approach the cursor is always forced to any specified selection = TextRange(<index>)(in my case its the end using the current length of the changing value onValueChange), I have no idea how to set the cursor in any part (selection) when I press/click the TextField.
My Implementation:
var textFieldValue by remember { mutableStateOf(TextFieldValue("Some Text")) }
Row (
modifier = Modifier
.height(150.dp)
.fillMaxWidth()
.clickable {
focusRequester.requestFocus()
textFieldValue = textFieldValue.copy(selection = TextRange("Some Text".length))
}
) {
BasicTextField(
modifier = Modifier.focusRequester(focusRequester),
value = textFieldValue,
onValueChange = {
textFieldValue =
textFieldValue.copy(text = it.text, selection = TextRange(it.text.length))
},
)
}
And what I'm trying to achieve (Large text area with text set and starts from top-left), its entire part should be clickable and triggers focus for the text field, the reason why I wrapped it in a Row with a clickable modifier.
I wasn't able to achieve this with a single text field with specified height, as TextAlign doesn't have a Top-Start alignment

ppt Slide and textbox selection macros

I want to make a PowerPoint macro. This is a macro that selects with textbox slide number, left, top.
'for example
Sub test1()
ActivePresentation.Slides(1).Select
with Left:=-300, Top:=100
End With
Dim TBox1
TBox1 = ActiveWindow.Selection.TextRange.Text
MsgBox (TBox13) '----error
End Sub
'select text box
Here's some sample code that'll do what you want.
Rather than make it specific to your question, I wrote something more general-purpose that you can use to find any shape at any position on any slide you like.
Option Explicit
Sub AddTestTextbox()
' Use this to quickly add a text box to test with
' at the right coordinates
Dim oSh As Shape
With ActivePresentation.Slides(1)
Set oSh = .Shapes.AddTextbox(msoTextOrientationHorizontal, 300, 100, 200, 100)
oSh.TextFrame.TextRange.Text = "Some random text to test with"
End With
End Sub
Sub Test()
Dim oSh As Shape
Set oSh = FindShapeAt(ActivePresentation.Slides(1), 300, 100)
If Not oSh Is Nothing Then
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
MsgBox oSh.TextFrame.TextRange.Text
End If
End If
End If
End Sub
Function FindShapeAt(oSl As Slide, sngLeft As Single, sngTop As Single) As Shape
Dim oSh As Shape
For Each oSh In oSl.Shapes
If oSh.Left = sngLeft Then
If oSh.Top = sngTop Then
Set FindShapeAt = oSh
Exit Function
End If
End If
Next
End Function

Gambas3 setting Cursor position

I am using a qt5 TextEdit in Gambas3 for rich text.
Please consider the code:
Dim cursorpos As Integer
If Key.Code = Key.Left Or Key.Code = Key.Up Or Key.code = Key.Right Or Key.Code = Key.Down Or Key.Code = Key.Delete Or Key.Code = Key.Backspace Then
cursorpos = TextEdit1.Pos ' just pick the position
Print cursorpos
Else
cursorpos = TextEdit1.Pos
Print cursorpos
TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this preserves the newlines, and replaces them with a <br> for the rich text
Print "setting : ", cursorpos ' prints the correct value
TextEdit1.Pos = cursorpos ' does not work
Print "got : ", TextEdit1.Pos ' jumps to the end of the string
Endif
Now, I write :
This si a line
this is a second line
I have a typo on the first line. I use my arrow key to get there. I hit backspace twice, and remove the word si. All good. Now I expect to type in the character i, and the cursor should stay just after the character i. But as soon as the i is typed in the correct position, the cursor jumps to the end of the text.
Please help. How can I keep the cursor position in the correct place? Thank you.
your error is this line...
TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>"
' this preserves the newlines, and replaces them with a <br> for the rich text
actually it removes the hidden original RichText formatting
The best way to change color of the text inline without changing the internal RichText formatting is to do something like this...
TextEdit1.Format.Color = Color.Red
then text typed at that position will be in red.
So you could monitor for Delete / Backspace then set the format color to red at the current position.
gambaswiki.org/wiki/comp/gb.qt4.ext/textedit

Swift 4 UiTextView - delete a word inside textView on button click

Is there a way to delete a specific word inside a UITextView?
let's say for example that in a textView the user wrote: "Hello my nome is john".
As soon as he finished typing he noticed that he mistyped a word.
Lets' say that there is an array initialised with a set o word and "name" is one of this.
when the user go back with the cursor and start deleting the misspelled a list of suggestion comes up.
He detect the word name and click on it.
is there a way to delete the word nome and insert the word name on which he just clicked.
So basically is there a way to get the word immediately before the cursor and remove it from the text view?
I have been able to get the first word before the cursor:
func characterBeforeCursor() -> String? {
// get the cursor position
if let cursorRange = postTextField.selectedTextRange {
// get the position one character before the cursor start position
if let newPosition = postTextField.position(from: cursorRange.start, offset: -1) {
let range = postTextField.textRange(from: newPosition, to: cursorRange.start)
return postTextField.text(in: range!)
}
}
return nil
}
but i wouldn't know how to get the entire word (until the first space, or a particular character e.g "#" ) and delte it from the textview.
I am a bit lost.... so Thanks to anyone will help.

How to programmatically enter text in UITextView at the current cursor position

I would like to add (some predefined) text at the current cursor position in a UITextView using Swift. So if I have a UITextField named txtField that has some text in it already such as "this is a beautiful valley" and I tap in the area between "a" and "beautiful" so that the cursor is now in the space between "a" and "beautiful" and then click a Button on my user interface a string of text such as "very" will get typed at the cursor position, so that the text in the UITextView will now become "this is a very beautiful valley". At the end of this operation (after button click event) I would the cursor to be just after the word "very". Many thanks for your help. I can see some question on the forum with similar themes, but the answers are in Objective C. I suicidal like help using Swift.
Try this... Inside your button's IBAction use this (please do not use forced optional unwrapping) or else if the textView has no selection or cursor, the app might crash:
let txt = "whatever you want"
if let range = handleToYourTextView.selectedTextRange {
// From your question I assume that you do not want to replace a selection, only insert some text where the cursor is.
if range.start == range.end {
handleToYourTextView.replaceRange(range, withText: txt)
}
}
try this
textView.replaceRange(textView.selectedTextRange!, withText: "your text")
update:
Swift 5.2
let txt = "whatever you want"
if let range = myTextView.selectedTextRange {
if range.start == range.end {
myTextView.replace(range, withText: txt)
}

Resources