我刚停止了这个错误
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
答案 0 :(得分: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
,T
中的"5TY5"
也会被替换。
如果您不想这样,则必须将原始文本拆分为字符并逐个开始检查。
以上代码有一些变化:
代码记录了注释,应该是不言自明的。
祝你好运!' 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