Click here to Skip to main content
15,893,722 members

Save splitted words of lines again in original file after editing

shaileshshinde asked:

Open original thread
I have done splitting word by word of textfile

But I want to save each and every word on the original position in original textfile after editing done on that splitted word.


[Edit]Shout removed[/Edit]

What I have tried:

VB
Imports System.IO
Imports System.Security
Imports Microsoft.CSharp

Imports Word = Microsoft.Office.Interop.Word


Public Class Form1
   
    Dim currentword As Integer = 0
    Dim Index1 As Integer = 0
    Dim Index2 As Integer = 0
    Dim line As String
    Dim opp As New OpenFileDialog


    Public Enum Direction
        Up = -1
        Down = 1

    End Enum

    Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles DotPadTextBox.SelectionChanged
        Dim fci As Integer = DotPadTextBox.GetFirstCharIndexOfCurrentLine()

        currentline = DotPadTextBox.GetLineFromCharIndex(fci)

        Dim s As String = String.Format("Current line: {0}", currentline)
        Me.Text = s
        Me.BtnUp.Enabled = currentline >= 0

        Me.BtnDown.Enabled = currentline < DotPadTextBox.Lines.Count
    End Sub

    '***********CODE TO SPLIT ,RichTextBox2 LINE INTO WORD****************
    Private Sub DisplayWords(direction As Direction)
        Dim words As String() = Me.RichTextBox2.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
            If currentword > words.Count - 1 Then
                currentword = words.Count - 1
            End If
        End If
    End Sub



    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
    '***********CODE ENDS TO SPLIT ,RichTextBox2 LINE INTO WORD****************

    '************CODE TO OPEN FILE*****************
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


        Try
            opp.Filter = "Document Files(*.doc)|*.doc|Document Files(*.docx)|*.docx|Text Files(*.txt)|*.txt|Rtf Files(*.rtf)|*.rtf|HTML Files(*.html)|*.html| AllFiles(*.*)|*.*"


            If opp.ShowDialog() = DialogResult.OK Then
                
                Dim SR As New IO.StreamReader(opp.FileName)
                DotPadTextBox.Text = SR.ReadToEnd()
                
            End If
            
        Catch ex As Exception
            Exit Sub
        End Try

    End Sub
    '************CODE ENDS TO OPEN FILE*****************

    '************CODE TO SAVE FILE*****************
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        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

    End Sub
    '************CODE END SAVE FILE*****************

End Class<pre>
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900