Results 1 to 3 of 3

Thread: [2005] reading and saving rtf control data to file

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    6

    Question [2005] reading and saving rtf control data to file

    Hi,
    Fairly new to .net so appologies if this is fairly simple, i have a windows form with a datagrid control. On the form there is also a rich text box control.
    What i would like is for the rich text box control to be comments about each record selected. This needs to save the files localy, not in the datasource.
    and then load and populate the rtf control if there is an existing file for that record.


    Has anyone got any pointers or example code I could work from

    Thanks in advance.
    Last edited by firespeed81; Mar 18th, 2007 at 03:24 PM.

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] reading and saving rtf control data to file

    I don't know how you plan on filling you grid, but anyway each record must have an ID I would use that ID as the name of the file dscription and locate it in a specific folder. Here is the code to read the file and put it in the RTF

    Code:
     Dim reader As IO.StreamReader
    
     Try
         reader = IO.File.OpenText("Folder" & ID & ".txt")
    
         While reader.Peek <> -1
              Me.RichTextBox1.Text &= reader.ReadLine()
         End While
    
     Catch ex As Exception
         Console.WriteLine("File not found")
     End Try

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] reading and saving rtf control data to file

    You should store the file path in the database. When the user selects a record in your DataGridView (please use the proper names to avoid confusion, a DataGridView is NOT a DataGrid) you can load the associated file into the RTB:
    vb Code:
    1. Private filesByRecord As New Dictionary(Of DataRow, String)
    2.  
    3. Private Sub BindingSource1_CurrentChanged(ByVal sender As System.Object, _
    4.                                           ByVal e As System.EventArgs) Handles BindingSource1.CurrentChanged
    5.     'The user just selected a record.
    6.     Dim row As DataRow = DirectCast(Me.BindingSource1.Current, DataRowView).Row
    7.  
    8.     If row.IsNull("FilePath") Then
    9.         'There is no associated file.
    10.         Me.RichTextBox1.Clear()
    11.     Else
    12.         'This record has an associated file.
    13.         If Not Me.filesByRecord.ContainsKey(row) Then
    14.             'Cache the associated file.
    15.             Me.filesByRecord.Add(row, IO.File.ReadAllText(CStr(row("FilePath"))))
    16.         End If
    17.  
    18.         'Load the file contents from the cache.
    19.         Me.RichTextBox1.Text = Me.filesByRecord(row)
    20.     End If
    21. End Sub
    This code assumes that you are using a BindingSource between your data and the grid. You don't have to do so but I strongly suggest that you do.
    Last edited by jmcilhinney; Mar 18th, 2007 at 08:29 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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