0

RichTextBoxRTF を操作するためだけにコントロールを使用したいのですが、バックグラウンド スレッドで使用します。

コントロールは UI に追加されませんが、「多くの UI コンポーネントがこれを必要とするため」STA エラーを返します。これを克服する方法はありますか?

ありがとう

 Dim vEditor As New RichTextBox
        'Return the existing log (if it exists)
        strSQL = "SELECT Log FROM Work_Orders WHERE Order_ID = " & Order_ID
        Dim vExistingLog As String = vService.ReturnScalarString(strSQL, Current_HOA_ID)
        If Not vExistingLog = "Empty" Then
            Using MS As New IO.MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(vExistingLog))
                vEditor.Selection.Load(MS, DataFormats.Rtf)
            End Using
        End If
        'Return the caret to the top of the document
        vEditor.CaretPosition = vEditor.Document.ContentStart
        'Add the email text
        Dim LogEntry As String = "Email entry made by " & User_Name & " on " & FullDate(Today) & " at " & Format(Now, "HH:mm") & " UTC" & "~~~"
        LogEntry += "Email sent to " & SendEmail_SupplierName & " at " & SendEmail_EmailAddress & " from " & AnnualDT.Rows(0)("Email_Maintenance") & "~~~ ~~~ ~~~"
        LogEntry += "End of email entry...." & "~~~ ~~~ ~~~"
        vEditor.CaretPosition.InsertTextInRun("Start of email entry ~~~ ~~~ ^^^" & SendEmail_Subject & "### ~~~ ~~~" & TextString & "~~~ ~~~" & LogEntry)

        'Add the divider
        Dim vSeparator As String = "{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}" & Environment.NewLine
        vSeparator += "{\pard\par}"
        Using MS As New IO.MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(vSeparator))
            vEditor.Selection.Load(MS, DataFormats.Rtf)
        End Using

        'change the fontsize to something reasonable
        Dim FullRange As New TextRange(vEditor.Document.ContentStart, vEditor.Document.ContentEnd)
        FullRange.ApplyPropertyValue(TextElement.FontSizeProperty, "10")


        Dim vPath As String = My.Application.Info.DirectoryPath
        If Not IO.Directory.Exists(vPath & "\Temp_Data") Then
            IO.Directory.CreateDirectory(vPath & "\Temp_Data")
        End If
        Dim FullPath As String = vPath & "\Temp_Data\Temp_RTF.rtf"
        Dim vRange As New TextRange(vEditor.Document.ContentStart, vEditor.Document.ContentEnd)
        Using FS As New IO.FileStream(FullPath, IO.FileMode.Create)
            vRange.Save(FS, System.Windows.DataFormats.Rtf)
        End Using
        SendEmail_RTF = System.IO.File.ReadAllText(FullPath)
        'Convert paragraphs
        SendEmail_RTF = SendEmail_RTF.Replace("~~~", "{\pard\par}").Replace("^^^", "\b ").Replace("###", "\b0")


        vEditor = Nothing
4

0 に答える 0