2010-07-21 84 views
0

我創建了一個應用程序,用於檢查剪貼板中的文本是否以「file」開頭,如果它以word文件開頭,它將處理剪貼板文本,然後用<替換它「a href =」some value 「>剪貼板類的問題

例如它剪貼板字符串是

文件:/// C:/Users/Searock/Desktop/dotnet/Apress.Pro.VB.2008.and.the.dot.NET .3.5.Platform.3rd.Edition.Mar.2008.html#link22

然後程序將處理剪貼板TXT,然後用< A HREF = 「#link22」 代替>

這裏是我的代碼:

變量聲明:

Dim strProcess As String 
Dim intPos As Integer 
Dim check As String 
Dim strBuilder As New StringBuilder() 

窗體的Load:

bwCopyPaste.RunWorkerAsync() 

BackGroundWorker DoWork事件:

While (True) 
    Thread.Sleep(150) 

    If bwCopyPaste.CancellationPending = True Then 
      Exit While 
    End If 

    check = Clipboard.GetText() 

    If check <> "" Then 

    If check.StartsWith("file") Then 

    Try 

      strProcess = Clipboard.GetText() 

      intPos = strProcess.LastIndexOf("#") 

      strProcess = strProcess.Substring(intPos, strProcess.Length - intPos) 

      strBuilder.AppendFormat("<a href=""{0}"">", strProcess) 

      Clipboard.SetText(strBuilder.ToString()) 

    Catch ex As Exception 

      MessageBox.Show("Invalid Url", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      Application.Restart() 

    Finally 
      strBuilder.Clear() 
    End Try 

    End If 

End If 
End While 

我沒有得到任何運行時錯誤,但即使是在剪貼板中的任何文字,這種說法

check = Clipboard.GetText() 
If check <> "" Then 

總是返回我一個錯誤值。

任何人都可以指出我正確的方向嗎?

謝謝。

+0

打開記事本並鍵入一些文字,然後將其複製到剪貼板並查看它是否有效。確保它的記事本,而不是像微軟的Word或什麼的。 – Icemanind 2010-07-21 20:09:33

+0

是的,我試圖通過在記事本中粘貼某些東西來檢查剪貼板,還有一個文本仍然是我的錯誤條件,我認爲剪貼板類只能由斯蒂芬賴特倫說過的STA使用。 – Searock 2010-07-22 03:43:52

回答

1

這裏的問題是BackgroundWorkers是MTA(多線程公寓),剪貼板類只能由STA(單線程公寓)線程使用。

在「bwCopyPaste.RunWorkerAsync()」所在的位置嘗試類似這樣的操作。

If Clipboard.ContainsText() then 
    bwCopyPaste.RunWorkerAsync(ClipBoard.GetText()) 
End if 

然後,您可以通過它的eventArgument獲取傳遞給後臺工作者的值。

+0

我會試一試。 – Searock 2010-07-22 03:37:20

+0

感謝您的回答。 – Searock 2010-07-22 05:06:02