0

我刚刚被这个错误阻止了

 System.NullReferenceException was unhandled   HResult=-2147467261  
 Message=Object reference not set to an instance of an object.  
 Source=System.Windows.Forms   StackTrace:
        at System.Windows.Forms.RichTextBox.StreamIn(String str, Int32 flags)
        at System.Windows.Forms.RichTextBox.set_SelectedText(String value)
        at Novania_JAV_Viewer_EncDec.Form1.Button1_Click(Object sender, EventArgs ef) in c:\users\toshiba\documents\visual studio
 2010\Projects\Novania JAV Viewer EncDec\Novania JAV Viewer
 EncDec\Form1.vb:line 91
        at System.Windows.Forms.Control.OnClick(EventArgs e)
        at System.Windows.Forms.Button.OnClick(EventArgs e)
        at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
        at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
        at System.Windows.Forms.Control.WndProc(Message& m)
        at System.Windows.Forms.ButtonBase.WndProc(Message& m)
        at System.Windows.Forms.Button.WndProc(Message& m)
        at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
        at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
        at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
        at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
        at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr
 dwComponentID, Int32 reason, Int32 pvLoopData)
        at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
 reason, ApplicationContext context)
        at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
 reason, ApplicationContext context)
        at System.Windows.Forms.Application.Run(ApplicationContext context)
        at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
        at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
        at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
 commandLine)
        at Novania_JAV_Viewer_EncDec.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
        at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
        at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
        at System.Threading.ExecutionContext.RunInternal(ExecutionContext
 executionContext, ContextCallback callback, Object state, Boolean
 preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
 preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        at System.Threading.ThreadHelper.ThreadStart()   InnerException:

这是我的全部代码,我正在尝试用一个单词替换所有字母,它需要一个一个替换一个变量。

错误位于此处(“标题”)字

Do Until RichTextBox1.Find("titelz") = -1
RichTextBox1.Find("titelz")
RichTextBox1.SelectedText = titel

这是我的整个表单类代码

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.LoadFile("template.rtf")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal ef As System.EventArgs) Handles Button1.Click

        Dim A As String
        Replace(TextBox1.Text, "A", "5TY5")
        Console.WriteLine(A)
        Dim B As String
        Replace(A, "B", "WEE")
        Console.WriteLine(B)
        Dim C As String
        Replace(B, "C", "23RW")
        Console.WriteLine(C)
        Dim D As String
        Replace(C, "D", "RW4R")
        Console.WriteLine(D)
        Dim E As String
        Replace(D, "E", "DFS")
        Console.WriteLine(E)
        Dim F As String
        Replace(E, "F", "R5")
        Console.WriteLine(F)
        Dim G As String
        Replace(F, "G", "QWEDQ")
        Console.WriteLine(G)
        Dim H As String
        Replace(G, "H", "ZDCZ")
        Console.WriteLine(H)
        Dim I As String
        Replace(H, "I", "VGHN")
        Console.WriteLine(I)
        Dim J As String
        Replace(I, "J", "ZSC")
        Console.WriteLine(J)
        Dim K As String
        Replace(J, "K", "ZSD")
        Console.WriteLine(K)
        Dim L As String
        Replace(K, "L", "WER")
        Console.WriteLine(L)
        Dim M As String
        Replace(L, "M", "GN")
        Console.WriteLine(M)
        Dim N As String
        Replace(M, "N", "xfv")
        Console.WriteLine(N)
        Dim O As String
        Replace(N, "O", "DAASAD")
        Console.WriteLine(O)
        Dim P As String
        Replace(O, "P", "WET")
        Console.WriteLine(P)
        Dim Q As String
        Replace(P, "Q", "DFCV")
        Console.WriteLine(Q)
        Dim R As String
        Replace(Q, "R", "ADSDA")
        Console.WriteLine(R)
        Dim S As String
        Replace(R, "S", "SGFDG")
        Console.WriteLine(S)
        Dim T As String
        Replace(S, "T", "SFSFD")
        Console.WriteLine(T)
        Dim U As String
        Replace(T, "U", "AWDAD")
        Console.WriteLine(U)
        Dim V As String
        Replace(U, "V", "RERE")
        Console.WriteLine(V)
        Dim W As String
        Replace(V, "W", "GBGDBB")
        Console.WriteLine(W)
        Dim X As String
        Replace(W, "X", " AWDAW ")
        Console.WriteLine(X)
        Dim Y As String
        Replace(X, "Y", "AWDEAVA")
        Console.WriteLine(Y)
        Dim Z As String
        Replace(Y, "Z", "AWEDWADW")
        Console.WriteLine(Z)
        Dim titel As String = Z

        Do Until RichTextBox1.Find("titelz") = -1
            RichTextBox1.Find("titelz")
            RichTextBox1.SelectedText = titel
        Loop
        RichTextBox2.Text = RichTextBox1.Text
        RichTextBox1.Clear()
        RichTextBox1.LoadFile("template.rtf")
    End Sub
End Class
4

1 回答 1

0

为了进行替换,我会尝试以下代码......它使用Dictionary类型变量。它们在 .NET 中被大量用作某种关联数组。

' First, create dictionary that you can loop through.
' This variable is created on form level (so only once)
' and contains all the "words" as key-value pairs.
' So you can search for key and replace with value.
Dim replaceDictionary As New Dictionary(Of String, String) From {{"A", "5TY5"}, _
                                                                 {"B", "WEE"}, _
                                                                 {"C", "23RW"}, _
                                                                 {"D", "RW4R"}, _
                                                                 {"E", "DFS"}, _
                                                                 {"F", "R5"}, _
                                                                 {"G", "QWEDQ"}, _
                                                                 {"H", "ZDCZ"}, _
                                                                 {"I", "VGHN"}, _
                                                                 {"J", "ZSC"}, _
                                                                 {"K", "ZSD"}, _
                                                                 {"L", "WER"}, _
                                                                 {"M", "GN"}, _
                                                                 {"N", "xfv"}, _
                                                                 {"O", "DAASAD"}, _
                                                                 {"P", "WET"}, _
                                                                 {"Q", "DFCV"}, _
                                                                 {"R", "ADSDA"}, _
                                                                 {"S", "SGFDG"}, _
                                                                 {"T", "SFSFD"}, _
                                                                 {"U", "AWDAD"}, _
                                                                 {"V", "RERE"}, _
                                                                 {"W", "GBGDBB"}, _
                                                                 {"X", " AWDAW "}, _
                                                                 {"Y", "AWDEAVA"}, _
                                                                 {"Z", "AWEDWADW"}
                                                                }

Private Sub btnReplace_Click(sender As System.Object, e As System.EventArgs) Handles btnReplace.Click
    ' Declare temporary variable to store text
    Dim tmpText As String = txtTitel.Text

    ' Loop through dictionary
    For Each replaceItem In replaceDictionary
        ' Replace key ("A" etc.) with value ("5TY5" etc.)
        ' and store in temporary variable
        tmpText = Replace(tmpText, replaceItem.Key, replaceItem.Value)
    Next

    'Display text
    txtTitel.Text = tmpText
End Sub

现在对于您的“查找”部分......"titelz"上面的代码不会改变任何东西(只有大写),所以如果它在那里,它就会留在那里。所以我不知道该怎么办......

编辑

作为记录:这需要很长的替换...一旦替换了A字符串中的所有 ',Tfrom"5TY5"也将被替换。如果你不想要这个,你将不得不将原始文本分成字符并开始一一检查。


一次检查一个字母的标题字符串

上面的代码有一些变化:

  • 字典现在是 (Char, String) 类型,而不是 2 个字符串。
  • 标题存储在一个单独的变量中,以便继续使用原始标题文本。
  • 不是遍历字典,而是一次遍历一个字母的标题文本。

代码记录在注释中,应该是不言自明的。

祝你好运!

' First, create dictionary that you can loop through.
' This variable is created on form level (so only once)
' and contains all the "words" as key-value pairs.
' So you can search for key and replace with value.
Dim replaceDictionary As New Dictionary(Of Char, String) From {{"A", "5TY5"}, _
                                                               {"B", "WEE"}, _
                                                               {"C", "23RW"}, _
                                                               {"D", "RW4R"}, _
                                                               {"E", "DFS"}, _
                                                               {"F", "R5"}, _
                                                               {"G", "QWEDQ"}, _
                                                               {"H", "ZDCZ"}, _
                                                               {"I", "VGHN"}, _
                                                               {"J", "ZSC"}, _
                                                               {"K", "ZSD"}, _
                                                               {"L", "WER"}, _
                                                               {"M", "GN"}, _
                                                               {"N", "xfv"}, _
                                                               {"O", "DAASAD"}, _
                                                               {"P", "WET"}, _
                                                               {"Q", "DFCV"}, _
                                                               {"R", "ADSDA"}, _
                                                               {"S", "SGFDG"}, _
                                                               {"T", "SFSFD"}, _
                                                               {"U", "AWDAD"}, _
                                                               {"V", "RERE"}, _
                                                               {"W", "GBGDBB"}, _
                                                               {"X", " AWDAW "}, _
                                                               {"Y", "AWDEAVA"}, _
                                                               {"Z", "AWEDWADW"}
                                                              }

Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
    ' Declare temporary variable to store text
    Dim tmpTitle As String = txtTitel.Text
    Dim tmpText As String = ""
    Dim tmpLetter As Char

    ' Loop through title string
    Dim i As Integer = 0
    Do While (i < tmpTitle.Length)
        ' Increase counter
        i += 1

        ' Save next letter in tmpLetter
        tmpLetter = Mid(tmpTitle, i, 1)

        ' Check for key in dictionary
        If replaceDictionary.ContainsKey(tmpLetter) Then
            ' When found, append associated value to tmpText
            tmpText &= replaceDictionary(tmpLetter)
        Else
            ' Not found: use letter from title string
            tmpText &= CStr(tmpLetter)
        End If

    Loop

    'Display text
    txtTitel.Text = tmpText
End Sub
于 2013-09-23T10:40:29.643 回答