Results 1 to 2 of 2

Thread: Richtextbox Savefile and UTF-8

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Posts
    234

    Richtextbox Savefile and UTF-8

    I'm just getting starting working with VB.NET -- mostly because I have need for using UTF-8 encodings within an application. However, I see that the Richtextbox.SaveFile method doesn't include a UTF-8 encoding option. I'm sure that this must be available (since V. 3 of the RichEdit dll allows for UTF-8) or there must be a way to do what I'm looking to do. Basically, I just need to be able to save files in UTF8 and load files in UTF8. Can anyone get me pointed in the right direction.

    Thanks

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Posts
    234
    Ok -- Maybe this question is more difficult than I thought. How about this then. In VB6, I'd re-written the LoadFile and SaveFile functions so that I could load UTF-8 text into the control. To do this, I had to re-write the Callbacks. I've tried to translate them, but I'm not seeing any data. I'm sure its because I'm not understanding how the datatypes should be working. Here is the form and Module code that I'm using. Can anyone figure out how this might work? I can't get the ReadFile function to work and can't figure out how to pass a FileStream handle into the Callback (which would probably work better).

    Thanks

    Form Code
    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3. Imports System.Runtime.InteropServices
    4.  
    5. Friend Class Form1
    6.     Inherits System.Windows.Forms.Form
    7. 'Region Code was Delete to save space
    8.     Public Enum ERECFileTypes
    9.         SF_TEXT = &H1s
    10.         SF_RTF = &H2s
    11.         SF_UNICODE = &H10s
    12.     End Enum
    13.  
    14.  
    15.  
    16.     Private m_hWnd As IntPtr
    17.  
    18.  
    19.  
    20.     Public Function LoadFromFile(ByVal sFile As String, ByVal eType As Form1.ERECFileTypes) As Boolean
    21.         Dim hFile As Integer
    22.         Dim tOF As OFSTRUCT = New OFSTRUCT()
    23.         Dim tStream As EDITSTREAM = New EDITSTREAM()
    24.         Dim lR As Integer
    25.         Dim FF As Integer
    26.  
    27.         tOF.Initialize()
    28.  
    29.         m_hWnd = RichTextBox1.Handle
    30.         hFile = OpenFile("c:\documents and settings\reeset\desktop\finished_ebsco.mrk", tOF, OF_READ)
    31.  
    32.  
    33.  
    34.         If (hFile <> 0) Then
    35.             tStream.dwCookie = hFile
    36.             'UPGRADE_WARNING: Add a delegate for AddressOf LoadCallBack Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1048"'
    37.             tStream.pfnCallback = AddressOf LoadCallBack
    38.             tStream.dwError = 0
    39.             FileMode = True
    40.  
    41.             ' The text will be streamed in though the LoadCallback function
    42.             'Dim lParam As System.IntPtr
    43.             'Marshal.StructureToPtr(tStream, lParam, True)
    44.             lR = SendMessage(m_hWnd, EM_STREAMIN, eType, tStream)
    45.             MsgBox("called")
    46.             LoadFromFile = (lR <> 0)
    47.             FileMode = False
    48.  
    49.  
    50.  
    51.  
    52.             CloseHandle(hFile)
    53.  
    54.         End If
    55.  
    56.         MsgBox("finished")
    57.  
    58.     End Function
    59.  
    60.  
    61.  
    62.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    63.  
    64.     End Sub
    65.  
    66.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    67.         If LoadFromFile("c:\documents and settings\reeset\desktop\finished_ebsco.mrk", ERECFileTypes.SF_TEXT) = False Then
    68.             MsgBox("nothing loaded")
    69.         End If
    70.     End Sub

    Module Code
    VB Code:
    1. Option Strict Off
    2. Option Explicit On
    3. Imports System.Runtime.InteropServices
    4.  
    5. Module Module1
    6.     <StructLayout(LayoutKind.Sequential)> _
    7.     Public Structure EDITSTREAM
    8.         <MarshalAs(UnmanagedType.I4)> Dim dwCookie As Integer ' /* user value passed to callback as first parameter */
    9.         <MarshalAs(UnmanagedType.I4)> Dim dwError As Integer ' /* last error */
    10.         Dim pfnCallback As LoadCallBackDelegate 'EDITSTREAMCALLBACK
    11.     End Structure
    12.  
    13.  
    14.  
    15.     Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As EDITSTREAM) As Integer
    16.     Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As EDITSTREAM) As Integer
    17.     Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As Integer, ByVal lpvSource As Integer, ByVal cbCopy As Integer)
    18.     Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As Integer, ByVal lpvSource As IntPtr, ByVal cbCopy As Integer)
    19.     Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As String, ByVal lpvSource As IntPtr, ByVal cbCopy As Integer)
    20.     Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As IntPtr, ByVal lpvSource As Integer, ByVal cbCopy As Integer)
    21.     Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As String, ByVal lpvSource As String, ByVal cbCopy As Integer)
    22.  
    23.     Public Structure OVERLAPPED
    24.         Dim Internal As Integer
    25.         Dim InternalHigh As Integer
    26.         Dim offset As Integer
    27.         Dim OffsetHigh As Integer
    28.         Dim hEvent As Integer
    29.     End Structure
    30.     Public Const OFS_MAXPATHNAME As Short = 128
    31.  
    32.     <StructLayout(LayoutKind.Sequential)> _
    33.         Public Structure OFSTRUCT
    34.         Dim cBytes As Byte
    35.         Dim fFixedDisk As Byte
    36.         Dim nErrCode As Short
    37.         Dim Reserved1 As Short
    38.         Dim Reserved2 As Short
    39.         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=OFS_MAXPATHNAME)> _
    40.         Dim szPathName() As Byte
    41.  
    42.         Public Sub Initialize()
    43.             ReDim szPathName(OFS_MAXPATHNAME)
    44.         End Sub
    45.  
    46.     End Structure
    47.     ' Streaming support:
    48.     'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1016"'
    49.     Public Declare Function WriteFile Lib "kernel32" (ByVal hFile As Integer, ByRef lpBuffer As Integer, ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByVal lpOverlapped As Integer) As Integer 'lpOverlapped As OVERLAPPED) As Long
    50.     'UPGRADE_WARNING: Structure OFSTRUCT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1050"'
    51.     Public Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, ByRef lpReOpenBuff As OFSTRUCT, ByVal wStyle As Integer) As Integer
    52.     'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1016"'
    53.     Public Declare Function ReadFile Lib "kernel32" (ByVal hFile As Integer, ByRef lpBuffer As String, ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByVal lpOverlapped As Integer) As Integer 'lpOverlapped As OVERLAPPED) As Long
    54.     Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
    55.     Public Const OF_CANCEL As Short = &H800S
    56.     Public Const OF_CREATE As Short = &H1000S
    57.     Public Const OF_DELETE As Short = &H200S
    58.     Public Const OF_EXIST As Short = &H4000S
    59.     Public Const OF_PARSE As Short = &H100S
    60.     Public Const OF_PROMPT As Short = &H2000S
    61.     Public Const OF_REOPEN As Short = &H8000S
    62.     Public Const OF_SHARE_COMPAT As Short = &H0S
    63.     Public Const OF_SHARE_DENY_NONE As Short = &H40S
    64.     Public Const OF_SHARE_DENY_READ As Short = &H30S
    65.     Public Const OF_SHARE_DENY_WRITE As Short = &H20S
    66.     Public Const OF_SHARE_EXCLUSIVE As Short = &H10S
    67.     Public Const OF_VERIFY As Short = &H400S
    68.     Public Const OF_WRITE As Short = &H1S
    69.     Public Const OF_READ As Short = &H0S
    70.     Public Const OF_READWRITE As Short = &H2S
    71.  
    72.  
    73.  
    74.     ' /* stream formats */
    75.  
    76.     'Public Const SF_TEXT = &H1
    77.     'Public Const SF_RTF = &H2
    78.     Public Const WM_USER As Short = &H400S
    79.     Public Const SF_RTFNOOBJS As Short = &H3S ' /* outbound only */
    80.     Public Const SF_TEXTIZED As Short = &H4S ' /* outbound only */
    81.     Public Const SF_UNICODE As Short = &H10S ' /* Unicode file of some kind */
    82.     Public Const EM_STREAMIN As Integer = (WM_USER + 73)
    83.     Public Const EM_STREAMOUT As Integer = (WM_USER + 74)
    84.     ' /* EM_SETCHARFORMAT wParam masks */
    85.     Public Const SCF_SELECTION As Integer = &H1
    86.     Public Const SCF_WORD As Integer = &H2
    87.     Public Const SCF_DEFAULT As Integer = &H0 '// set the default charformat or paraformat
    88.     Public Const SCF_ALL As Integer = &H4 '// not valid with SCF_SELECTION or SCF_WORD
    89.     Public Const SCF_USEUIRULES As Integer = &H8 '// modifier for SCF_SELECTION; says that
    90.     ' // the format came from a toolbar, etc. and
    91.     ' // therefore UI formatting rules should be
    92.     ' // used instead of strictly formatting the
    93.     ' // selection.
    94.  
    95.     Private m_sText As String
    96.     Private m_lPos As Integer
    97.     Private m_lLen As Integer
    98.     Private m_bFileMode As Boolean
    99.     Private m_lObj As Integer
    100.     Public Delegate Function LoadCallBackDelegate(ByVal dwCookie As Integer, ByVal lPtrPbBuff As IntPtr, ByVal cb As Integer, ByVal pcb As Integer) As Integer
    101.  
    102.  
    103.     Public Property FileMode() As Boolean
    104.         Get
    105.             FileMode = m_bFileMode
    106.         End Get
    107.         Set(ByVal Value As Boolean)
    108.             m_bFileMode = Value
    109.             m_lPos = 0
    110.             m_lLen = -1
    111.         End Set
    112.     End Property
    113.  
    114.  
    115.     Public Function LoadCallBack(ByVal dwCookie As Integer, ByVal lPtrPbBuff As IntPtr, ByVal cb As Integer, ByVal pcb As Integer) As Integer
    116.         Dim sBuf As String
    117.         Dim b() As Byte
    118.         Dim lLen As Integer
    119.         Dim lRead As Integer
    120.  
    121.         If (m_bFileMode) Then
    122.             ReadFile(dwCookie, sBuf, cb, pcb, 0)
    123.  
    124.             MsgBox(System.Text.Encoding.UTF8.GetString(b))
    125.  
    126.             'ReDim lPtrPbBuff(4094 - 1)
    127.             'ReadFile(dwCookie, lPtrPbBuff, cb, pcb, 0)
    128.  
    129.  
    130.             'MsgBox(System.Text.Encoding.UTF8.GetString(lPtrPbBuff))
    131.  
    132.  
    133.             'CopyMemory(lRead, lPtrPbBuff(0), 4)
    134.  
    135.             If (lRead < cb) Then
    136.                 ' Complete:
    137.                 Return 0
    138.             Else
    139.                 ' More to read:
    140.                 Return 0
    141.             End If
    142.             m_lPos = m_lPos + lRead
    143.  
    144.  
    145.        
    146.  
    147.         End If
    148.  
    149.     End Function

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