Click here to Skip to main content
14,239,876 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have a lines suppose:

abcde 12385 8755 87878988 dsfgfgfdg
12345 211323 fgfgfdgf 544555 dfghghh weewrerr opopppiopoi weewrewrwe
xyz
587964

and when "current word" is displayed in one textbox3 then "previous word" should be displayed in textbox4 and "next word" from line should be displayed in textbox5

What I have tried:

Due to below working code i am able to get the current word only.

Dim somestring2() As String
somestring2 = RichTextBox2.Text.Split(" ")
Dim sh As New mb.ShowMessagebox
For i = 0 To somestring2.Length - 1


textbox3.Text = somestring2(i)


sh.ShowBox(somestring2(i), mb.MStyle.ok, mb.FStyle.Keys, "Single Word Window")

Next

is there any solution for this?
Posted
Updated 3-Jul-19 22:06pm
v3
Comments
Maciej Los 4-Jul-19 3:21am
   
As far as i see only one line with 4 "words" in it.
shaileshshinde 4-Jul-19 4:36am
   
the richtextboxlineupanddown code was given by you.the same i am using.but now i have managed to split words and show it in messagebox as well as textbox.

but now i want next word and previous word to get showed in different textboxes.

not getting the idea for that method to show next and previous word in different textboxes.
shaileshshinde 5-Jul-19 2:36am
   
any solution for my above query?
shaileshshinde 8-Jul-19 3:14am
   
can you please help me with this solution?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Please, read my comment to the question first.

Check this:
Dim singleline As String = "abcde 12345 xyz 587964"
Dim words As String() = singleline.Split(New String(){" "}, StringSplitOptions.RemoveEmptyEntries)

For Each word As String In words
	Console.WriteLine(word)
Next


Result:
abcde
12345
xyz
587964


[EDIT]

If you would like to get next/previous word, you have to write method similar to SelectLine provided by me in this solution: Move to next & back line in richtextbox via button click[^]


[EDIT#2]

On the top of Form1 module:
Dim currentword As Integer = 0

Public Enum Direction
    Up = -1
    Down = 1
End Enum


Events for next/previous buttons:
Private Sub BtnNextWord_Click(sender As Object, e As EventArgs) Handles BtnNextWord.Click
    DisplayWords(Direction.Down)
End Sub

Private Sub BtnPrevWord_Click(sender As Object, e As EventArgs) Handles BtnPrevWord.Click
    DisplayWords(Direction.Up)
End Sub


Procedure to display words:
Private Sub DisplayWords(direction As Direction)
    Dim words As String() = Me.RichTextBox1.Lines _
                            .SelectMany(Function(line) line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)) _
                            .ToArray()

    Me.TxtCurrentWord.Text = words(currentword)
    Me.TxtPreviousWord.Text = If(currentword > 0, words(currentword - 1), Nothing)
    Me.TxtNextWord.Text = If(currentword < words.Count - 1, words(currentword + 1), Nothing)

    currentword += direction
    If currentword < 0 Then currentword = 0
    If currentword > words.Count - 1 Then currentword = words.Count - 1

End Sub
   
v4
Comments
shaileshshinde 4-Jul-19 4:15am
   
@Maciej not getting the idea for that method.
Maciej Los 8-Jul-19 9:40am
   
Check out updated answer.
shaileshshinde 9-Jul-19 0:45am
   
You are superb maciej.guru
shaileshshinde 9-Jul-19 0:48am
   
But I have one more query regarding this method and other richboxlineupdown code given by you.
How the final file will be saved.as i checked for lineupdown i saw only one line is getting saved and googled also.i did not get logic for saving lineupdown and this wordupdown as a one text/doc file in order as they were in original text/word file
Maciej Los 9-Jul-19 5:18am
   
It should be something Luke this:
File.WriteAllLines("C:\rtb.txt", RichTextBox1.Lines)
shaileshshinde 9-Jul-19 5:36am
   
i have tried below code:
Dim saveMe As New SaveFileDialog()
saveMe.Filter = "Text Documents(*.txt)|*.txt"
If saveMe.ShowDialog() = Windows.Forms.DialogResult.OK Then
'RichTextBox2.SaveFile(saveMe.FileName, RichTextBoxStreamType.PlainText)
File.WriteAllLines(saveMe.FileName, DotPadTextBox.Lines)
MsgBox("File Saved as : " + saveMe.FileName)
Else
MsgBox("Failed to pick up file to save as")
End If

Note:- DotPadTextBox is the richtextbox which first open the file and
RichTextBox2 is the textbox which shows single line of textfile as per your code.


when we edit the single line of file from RichTextBox2 then the line should get saved instead of earlier wrong line in the same textfile with other line exists.

means not only one line.need all line to be saved.but just the edited line should get replaced in file.
shaileshshinde 12-Jul-19 1:38am
   
any solution for this:
i have tried below code:
Dim saveMe As New SaveFileDialog()
saveMe.Filter = "Text Documents(*.txt)|*.txt"
If saveMe.ShowDialog() = Windows.Forms.DialogResult.OK Then
'RichTextBox2.SaveFile(saveMe.FileName, RichTextBoxStreamType.PlainText)
File.WriteAllLines(saveMe.FileName, DotPadTextBox.Lines)
MsgBox("File Saved as : " + saveMe.FileName)
Else
MsgBox("Failed to pick up file to save as")
End If

Note:- DotPadTextBox is the richtextbox which first open the file and
RichTextBox2 is the textbox which shows single line of textfile as per your code.


when we edit the single line of file from RichTextBox2 then the line should get saved instead of earlier wrong line in the same textfile with other line exists.

means not only one line.need all line to be saved.but just the edited line should get replaced in file.
Maciej Los 12-Jul-19 2:08am
   
So, you have to replace a line, which coming from textbox with a line in a richtextbox then to save all lines of richtextbox.

Note, that i've resolved your original issue. It would be good to see my solution as an answer. Your farther requirements does not belong to original question.
shaileshshinde 12-Jul-19 2:52am
   
OK

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100