Basically I've got a RichTextBox, TextBox and a Button. The RichTextBox has some text in it and the Textbox is used to type the word and the button is used to find it. So far I'm using:
Dim index As Integer = 0
Dim temp As String = txtbx_Write.Text
txtbx_Write.Text = String.Empty
txtbx_Write.Text = temp
While index < txtbx_Write.Text.LastIndexOf(TextBox1.Text)
txtbx_Write.Find(TextBox1.Text, index, txtbx_Write.TextLength, RichTextBoxFinds.None)
txtbx_Write.SelectionBackColor = Color.DarkGray
index = txtbx_Write.Text.IndexOf(TextBox1.Text, index) + 1
End While
Basically what this does is that when I type e.g. "The", if there is "The" in the RichTextBox, it will set a background colour for that word and it will do it for each "The" word.
What I would like to do is that, When I type in the word "The", there will be a label with the amount of results there is and for each time the user clicks the "Find" button, it will select the word "The" and keep selecting and going until there isn't any more.
Pretty much like the native Notepad find feature where it selects and goes through each word.
Not sure what are you trying to do with that temp variable... and you might want to write something to clear the background color when you're done.
Anyway, you could do the following for your "find" button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not RichTextBox1.Focused Then
RichTextBox1.Focus()
End If
RichTextBox1.Find(TextBox1.Text, RichTextBox1.SelectionStart + RichTextBox1.SelectionLength, RichTextBoxFinds.None)
End Sub
Read about Find method in MSDN for more details.
edit: In order to find the amount of hit results, you may simply count the "highlighting" while loop. Because you have to go through all the words to highlight them the information is already there.
Sample code:
Dim index As Integer = 0
Dim count As Integer = 0
While index < RichTextBox1.Text.LastIndexOf(TextBox1.Text)
RichTextBox1.Find(TextBox1.Text,index,RichTextBox1.TextLength,
RichTextBoxFinds.None)
RichTextBox1.SelectionBackColor = Color.DarkGray
index = RichTextBox1.Text.IndexOf(TextBox1.Text, index) + 1
count = count + 1
End While
And count will hold the result amount.
Related
I have a Word Document which creates indexcards for books via series. On every page information about a single book. Sometimes two fields have to be swaped for a specific item. So for my collegue it would save a lot of work if that would be possible with a single click. I have programming experience but not so much Office and no VBA.
Is it possible to do something like below? :
Pseudo code:
dim temp as string
dim temp2 as string
select first textbox on active page
temp = select.Text
select second textbox on active page
temp2 = select.Text
select.Text = temp
select first textbox on active page
select.Text = temp2
Any ideas what functions to look in would be welcome. Especialy if it is possible to select the first and second textbox of a single (active) page.
There is probably a better way to do this, but textboxes in word I find to be a real pain.
Here we set the names of the boxes, how you determine the order I'm unsure, maybe a check using titles, or the anchor point. The ID property is not the index so we can't use that.
Sub Rename()
Dim textbox As Object
Dim iter As Long
iter = 1
For Each textbox In ActiveDocument.Shapes 'I'm not sure how to set names manually
textbox.Name = "TextBox " & iter
iter = iter + 1
Next
End Sub
Assuming you have the boxes named in the order they appear:
Sub TextSwap()
Dim targetbox As Long
Dim val1 As String
Dim val2 As String
targetbox = InputBox("Enter an Integer relating to the position of the textbox")
val1 = ActiveDocument.Shapes("TextBox " & targetbox).TextFrame.TextRange.Text
val2 = ActiveDocument.Shapes("TextBox " & (targetbox + 1)).TextFrame.TextRange.Text
ActiveDocument.Shapes("TextBox " & targetbox).TextFrame.TextRange.Text = val2
ActiveDocument.Shapes("TextBox " & (targetbox + 1)).TextFrame.TextRange.Text = val1
End Sub
You'll need to check the input to make sure you don't get errors. I'm also not convinced the names of the textboxes are reliable, but this should work as long as you aren't doing anything too crazy.
I am wanting to make a wildcard search character (ex. Binary%) so when they click search it finds all the files with the word Binary in the filename and loads them into a list box. My current code is below.
Private Sub _test_TextChanged(sender As Object, e As TextChangedEventArgs) Handles _test.TextChanged
For x As Integer = 0 To _listbox.Items.Count - 1
If _listbox.Items(x).ToString = _test.Text$ Then
_listbox.SelectedIndex = x
Return
End If
Next
End Sub
Any help is welcome!
Thank you!
-Kyvex
What you are asking doesn't really match your code.
... "when they click search it finds all the files" ...
But you have a TextChanged event handler of a TextBox (not a Button)
..."with the word Binary in the filename and loads them into a list box"
But you select only the first item which matches the filter of items already in a ListBox
To get your code to do what it seems to want to do, simply use the Like operator and add the wildcard character (*) after the TextBox.Text
For x As Integer = 0 To _listBox.Items.Count - 1
If _listBox.Items(x).ToString Like _test.Text & "*" Then
_listBox.SelectedIndex = x
Return
End If
Next
Now you can select the first item in the listbox which matches the filter
If you have a multi-select ListBox, you can use this
If _test.Text = "" Then Exit Sub
_listBox.SelectionMode = SelectionMode.MultiSimple
For x As Integer = 0 To _listBox.Items.Count - 1
_listBox.SetSelected(x, _listBox.Items(x).ToString Like _test.Text & "*")
Next
(the filter logic is the same as the first example)
And you can make it case-insensitive
_listBox.SetSelected(x, _listBox.Items(x).ToString().ToUpper() Like _test.Text.ToUpper() & "*")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a TextBox that includes more words
Example:
I'm pRoFeSSoinaL coder
I want to search for the word pRoFeSSoinaL (that contains uppercase and lowercase letters) and then replace it.
OK let me try and address your problem again based on your image:
And on your comment:
...but i need search about word have More than a change of letters example (execute, ExEcute, eXecute, execUtE........ETC) i need find word No matter how large and small the letters are see this proof
Again, if I have this right, you would like to find all occurrences of a word regardless of it's case. In effect you would like for your application to work a bit like Notepad.
I've put together some code that will use the following methods:
String.ToLower:
Returns a copy of this string converted to lowercase.
TextBox.Select:
Selects a range of text in the text box.
String.Remove:
Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
String.Insert:
Returns a new string in which a specified string is inserted at a specified index position in this instance.
String.Substring:
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
I have three Button controls that I've put a bit a code behind. One will find the next occurrence of a word, another will replace the next occurrence of a word and the last one will replace all occurrences of a word.
Now it's not my job to design your application but for ease I have also popped on three TextBox controls. This is how my form looks:
To find the next occurrence of professional this is the code I used:
Private Sub btnFindNext_Click(sender As Object, e As EventArgs) Handles btnFindNext.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
'select next occurrence
txtTextToSearch.Select(position, txtWordToFind.Text.Length)
End If
End Sub
This is a screenshot of how it would look when you click the button:
Clicking the button again will select the next occurrence of the word. It will keep toggling between the two in my example.
To replace the next occurrence of professional this is the code I used:
Private Sub btnReplaceNext_Click(sender As Object, e As EventArgs) Handles btnReplaceNext.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
'remove old word
txtTextToSearch.Text = txtTextToSearch.Text.Remove(position, txtWordToFind.Text.Length())
'insert new word
txtTextToSearch.Text = txtTextToSearch.Text.Insert(position, txtReplaceWith.Text)
End If
End Sub
This is a screenshot of how it would look when you click the button:
Clicking the button again will replace the next occurrence of the word.
To replace all occurrences of the word professional this is the code I used:
Private Sub btnReplaceAll_Click(sender As Object, e As EventArgs) Handles btnReplaceAll.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
While position > 0 AndAlso position < txtTextToSearch.Text.Length
'remove old word
txtTextToSearch.Text = txtTextToSearch.Text.Remove(position, txtWordToFind.Text.Length())
'insert new word
txtTextToSearch.Text = txtTextToSearch.Text.Insert(position, txtReplaceWith.Text)
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
End While
End If
End Sub
This is a screenshot of how it would look when you click the button:
My code will need adapting I'm sure but it should give you something to go on. As for the level of detail I have gone to, may not benefit you but may benefit others that visit this question. Hope this helps.
Judging from your screenshot (http://i.imgur.com/77wdZoI.png) you appear to be after case-insensitive string matching.
The simplest way is to use the String.IndexOf(String, StringComparison) overload to perform case-insensitive comparison:
Private Function FindText(ByVal Source As String, ByVal Text As String) As Integer
Return Source.IndexOf(Text, StringComparison.OrdinalIgnoreCase)
End Function
Usage example:
Dim TextToFind As String = "professional"
Dim TextPos As Integer = FindText(TextBox1.Text, TextToFind)
If TextPos > -1 Then 'Match found.
TextBox1.SelectionStart = TextPos
TextBox1.SelectionLength = TextToFind.Length
End If
Goal
Programmatically highlight part of a string contained in a RichTextBox based on what is selected in a DataGridView.
See screenshot for a more visual example
As you can see, when an Option type (EC - Electrical) is selected, its options are displayed in another DataGridView to the right. From there, the user can check the ones he wishes to have included in the Conveyor Function Summary (Photoeye in this case) and it gets highlighted.
Code Used
This method is executed every time the Option Type DataGridView has a selectionchanged event.
Private Sub SummaryOptionsHighlight()
Dim strToFind As String = ""
Dim textEnd As Integer = rtbSummary.TextLength
Dim index As Integer = 0
Dim lastIndex As Integer = 0
'Builds the string to find based on custom classes - works fine
For Each o As clsConveyorFunctionOptions In lst_Options
If o.Included Then
If strToFind.Length <> 0 Then strToFind += ", "
If o.Optn.IsMultipleQty And o.Qty > 0 Then
strToFind += o.Optn.Description & " (" & o.Qty & "x)"
Else
strToFind += o.Optn.Description
End If
End If
Next
'Retrieves the last index of the found string: ex. Photoeye (3x)
lastIndex = rtbSummary.Text.LastIndexOf(strToFind)
'Find and set the selection back color of the RichTextBox
While index < lastIndex
rtbSummary.Find(strToFind, index, textEnd, RichTextBoxFinds.None)
rtbSummary.SelectionBackColor = SystemColors.Highlight
index = rtbSummary.Text.IndexOf(strToFind, index) + 1
End While
End Sub
Problem
What's going on isn't a highlight but more of a backcolor being set to that selection. The reason I say this is because when I click in the RichTextBox to indicate that it has focus, it doesn't clear the highlighting. Perhaps there is an actual highlight instead of a backcolor selection?
See the difference:
Selection Back Color:
Highlighting:
to highlight, you just need to do this:
if richtextbox1.text.contians("photoeye") then
richtextbox1.select("photoeye")
end if
this should work for what you are trying to do
My code gets a list of words from a txt file and chooses the words randomly. However, the same word can appear more than once and i need to know how to stop this from happening?
Here is the code:
Dim aryName As String() = Nothing
aryName = File.ReadAllLines(Application.StartupPath & "\Random\fnames.txt")
Dim randomWords As New List(Of String)
For i = 0 To aryName.Length - 1
If randomWords.Contains(aryName(i)) = False Then
randomWords.Add(aryName(i))
End If
Next
Dim random As New Random
Label2.Text = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
Maybe this might work, although it's in english and not code :(
if label1.text is changed then
Get label1.text
if label.text becomes this word again then
run the random code
end if
end if
This should prevent immediate repeats:
Dim random As New Random
'Just create a temporary holder for comparison
Dim word As String = Label2.Text
'Run a while loop that works as long as there
'is no change to the word. This should prevent
'back to back repeats.
While word = Label2.Text
word = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
End While
Label2.Text = word
If you don't want it to repeat ever again, you should probably remove the used word from the randomWords List.
Dim random As New Random
Label2.Text = (randomWords(random.Next(0, randomWords.Count - 1)).ToString)
randomWords.Remove(Label2.Text)
You can a) remove the selected word from the list, or b) you can random sort the list first.
Option a) is already addressed in another answer
Option b) lets you retain all the words in memory. Here is the code:
Dim randomWords As New List(Of String)(File.ReadAllLines(Application.StartupPath & "\Random\fnames.txt"))
Dim random As New Random
randomWords.Sort(Function(s1 As String, s2 As String) random.Next(-1, 1))
For index As Integer = 0 To randomWords.Count - 1
Label2.Text = randomWords(index)
Next
Modify your For loop to prevent dupes in aryName from getting into randomWords:
For i = 0 To aryName.Length - 1
If randomWords.Contains(aryName(i)) = False Then
randomWords.Add(aryName(i))
End If
Next