CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2010
    Posts
    10

    [RESOLVED] Richtextbox with mouse cursor and add/replace saving

    hi, I have a RichTextBox with buttons two check boxes add and replace. the problem is when i open a file it should open it exactly where the mouse cursor is and replace the text that is selected in the richtextbox (if there is any ).

    And the second part would be to with the save button. If add is selected then it should add the content to the file or it should replace the content if replace is selected.

    i have an open dialog and it's a txt file.
    I am using the following code to open the file.

    Code:
    Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.DefaultExt = "txt"
    CommonDialog1.ShowSave
    Cancel:
    End Sub



    thanks a lot for the help given

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Richtextbox with mouse cursor and add/replace saving

    Apparently the code posted has nothing to do with the issue at hand.

    If you are looking to read data from a file and replace the selected text with the data from the file then you will need to open the file, read the data into a variable, insert it into the RTB and then of course save the data in the RTB to file if you want to save it.

    If you have written code for this then post that and let us know where you are having an issue. The code above just shows a dialog to save as but does not really do anything else.

  3. #3
    Join Date
    Mar 2010
    Posts
    10

    Re: Richtextbox with mouse cursor and add/replace saving

    the problem is i don't know the code for opening the file and replacing the text if any is selected , and if text is not selected then it should just start placing the txt content into the RTB where the cursor is.

    the second part is with the saving , i have to make it replace the file if replace is selected or add the content if add is selected:-s

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Richtextbox with mouse cursor and add/replace saving

    Look up Open, input, output, append and .seltext in your online help for a nudge in the right direction.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Richtextbox with mouse cursor and add/replace saving

    For example when you open a file for output the file will be created if not there and overwritten if it is there already whereas if you open it for append it will be created if not there and data will be added to the end if the file exists.

    Of course when you open for input this allows you to read the file. [e.g. input the data into memory]

  6. #6
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: Richtextbox with mouse cursor and add/replace saving

    To Read the File
    Code:
       Richtextbox.LoadFile(FileName,FileType)
       ' File Type = 1(rtfText) = Text file
       ' File Type =2(rtfRTF) = RTF File

    To Save the File
    Code:
       Richtextbox.SaveFile(FileName,FileType)
       ' File Type = 1(rtfText) = Text file
       ' File Type =2(rtfRTF) = RTF File
    you can use Replace function to find and replace text.
    Last edited by ComITSolutions; March 11th, 2010 at 11:23 PM. Reason: tags
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Richtextbox with mouse cursor and add/replace saving

    Yes but if I understood correctly the intention was to have a file or some text already in the RTB then load data from a file into the selected area or the cursor location of the RTB. This would require reading the file into a variable first and then the use of the seltext method.

    Also he mentioned the need to append to existing file when saving. The basic RTB load and save options would likely not be an option.

  8. #8
    Join Date
    Mar 2010
    Posts
    10

    Re: Richtextbox with mouse cursor and add/replace saving

    DataMiser you are right. The basic stuff doesn't work because i have to load the text when i have text in the rtb or when there is nothing there ( when it's empty that's no problem ) , the problem comes when i have text and i have to load the file. The text from the file has to be placed exactly where the mouse cursor is in the rtb and if i have selected text in the rtb replace it.


    For the saving part , the save button has to take some attributes from the two option buttons ( add / replace ) . when add is selected is should just add the content to the file and when replace is selected it should replace the entire file, which again i can't figure out how to code it .

    The basic open and save and the part for editing text, font and stuff wasn't so hard, but this is beyond my knowledge , and believe me i have tried searching google and youtube for tutorials

  9. #9
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: Richtextbox with mouse cursor and add/replace saving

    Quote Originally Posted by DataMiser View Post
    Yes but if I understood correctly the intention was to have a file or some text already in the RTB then load data from a file into the selected area or the cursor location of the RTB. This would require reading the file into a variable first and then the use of the seltext method.

    Also he mentioned the need to append to existing file when saving. The basic RTB load and save options would likely not be an option.
    Yes, Datamiser I understood it wronly.

    The following Code may be useful, I have added one more invisible richtextbox to the same form.
    Logic is when user clicks button, first richtextbox lost focus event is raised during that time i am storing the cursor position to a variable and enabling common dilogbox to select input file and reading that file to richtextbox2. Using mid$ function Concatenating the texts in both richtextboxes.

    Hope this what he was looking!
    Code:
    Option Explicit
    Dim CurrentCursPosition As Long
    Private Sub CmdRead_Click()
        CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
        "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
        CommonDialog1.FilterIndex = 2
        CommonDialog1.ShowOpen
          If CommonDialog1.FileName <> "" Then
              RichTextBox2.LoadFile CommonDialog1.FileName, rtfText
              RichTextBox1.Text = Mid$(RichTextBox1.Text, 1, CurrentCursPosition) & RichTextBox2.Text & _
                  Mid$(RichTextBox1.Text, CurrentCursPosition + 1)
              
          End If
    End Sub
    
    Private Sub cmdSave_Click()
        CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
        "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
        CommonDialog1.FilterIndex = 2
        CommonDialog1.ShowSave
          If CommonDialog1.FileName <> "" Then
              RichTextBox1.SaveFile CommonDialog1.FileName, rtfText
          End If
    
    End Sub
    
    Private Sub RichTextBox1_LostFocus()
      CurrentCursPosition = RichTextBox1.SelStart
    End Sub
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  10. #10
    Join Date
    Mar 2010
    Posts
    10

    Re: Richtextbox with mouse cursor and add/replace saving

    Thanks a lot ComITSolutions almost everything is working now, all i have to figure out now is how to delete the selected text

  11. #11
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: Richtextbox with mouse cursor and add/replace saving

    Quote Originally Posted by gabriel10 View Post
    Thanks a lot ComITSolutions almost everything is working now, all i have to figure out now is how to delete the selected text
    If your problem is solved, mark this thread as [Resolved]
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  12. #12
    Join Date
    Mar 2010
    Posts
    10

    Re: Richtextbox with mouse cursor and add/replace saving

    for now it's solved about 90% :-s , i am searching to find the way to delete the text from the rtb (if any is selected ) when opening a file, i will post the answer when i find it and mark it resolved then

  13. #13
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: Richtextbox with mouse cursor and add/replace saving

    Quote Originally Posted by gabriel10 View Post
    for now it's solved about 90% :-s , i am searching to find the way to delete the text from the rtb (if any is selected ) when opening a file, i will post the answer when i find it and mark it resolved then
    Code:
    RichTextBox1.selText="<---Replacing Text ------>"
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  14. #14
    Join Date
    Mar 2010
    Posts
    10

    Re: Richtextbox with mouse cursor and add/replace saving

    yup, it's done now, thanks a lot ComITSolutions!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured