2014-09-13 49 views
1

我是C#的新手,因此可能這是一個非常簡單的問題,但即使在我閱讀完所有內容後,我仍無法找到添加Visual Studio 2010所需定義的方法,儘管在每個表達式的MSDN文檔頁面上添加using語句和引用。添加引用後不包含定義

我的錯誤:

System.windows.forms does not contain a definition for document 

我加入基於什麼我讀了微軟網站上的參考「presentationframework」。感謝下面的一些評論,我發現我顯然混合了兩種不同的方式從文本框中獲取文本。我需要做的就是將內容粘貼到文本框中,並在按下按鈕時獲取框的內容。有人可以告訴我(或更好地顯示)我如何做到這一點,而不是混合策略?

Form1.cs中:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
using System.Windows.Documents; 
using System.Windows.Controls.RichTextBox; 
using System.Windows.Forms; 
using System.Windows.Controls; 
using System.Collections; 


namespace WindowsFormsApplication1 
{ 
public partial class HackController : Form 
{ 
    ArrayList ipList = new ArrayList(); 
    ArrayList acctList = new ArrayList(); 
    int myAcct = 0; 
    string myIp = ""; 
    public HackController() 
    { 
     InitializeComponent(); 
    } 

    public void sync_Click(object sender, EventArgs e) 
    { 
     string data = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text;// ERROR HERE 
     string[] lines = Regex.Split(data, "\n"); 
     Regex ipMatch = new Regex(@"\d+.\d+.\d+.\d+"); 
     Regex acctMatch = new Regex(@"#\d+"); 
     foreach(string line in lines) 
     { 
      foreach(Match m in ipMatch.Matches(line)) 
      { 
       ipList.Add(m); 
      } 
      foreach(Match m in acctMatch.Matches(line)) 
      { 
       acctList.Add(m); 
      } 
     } 
    } 
} 
} 
+0

它是'.Split'還使用本地結構'字符串'作爲字符串 – Sam 2014-09-13 18:13:04

+0

嘗試刪除'using System.Windows.Forms;','rtb'應該是'System.Windows.Controls.RichTextBox'而不是'System。 Windows.Forms.RichTextBox' – 2014-09-13 18:27:52

+0

@YuliamChandra,我試着做你的建議,但它沒有解決我的問題。如果我刪除.Forms我碰到我的pulic部分類錯誤....我會更新我的代碼,以包括整個班級 – Rilcon42 2014-09-13 18:43:30

回答

0

下面是示出顯示在一個WinForms RichTextBox控件數據的一種方法一(比較奇怪)樣品。 (對不起,但這是我在編程中唯一可以找到的示例,因爲我通常使用Developer Express WinForms控件而不是基本的.Net。)

但首先,要確保100%不混合WinForms和WPF,請確保在創建項目時選擇WinForms作爲項目模板類型(而不是WPF)。當您將RichTextBox控件從Visual Studio工具箱拖放到窗體上時,請確保它是WinForms RichTextBox,而不是WPF。

您可以通過查看.Designer.cs文件來查看,並查找由Visual Studio設計器創建的RTB控件的定義。它應該是這個樣子:

 this.rtbResults = new System.Windows.Forms.RichTextBox(); 

    ... 
    ... 

    // 
    // rtbResults 
    // 
    this.rtbResults.BorderStyle = System.Windows.Forms.BorderStyle.None; 
    this.rtbResults.Location = new System.Drawing.Point(12, 52); 
    this.rtbResults.Name = "rtbResults"; 
    this.rtbResults.Size = new System.Drawing.Size(640, 133); 
    this.rtbResults.TabIndex = 17; 
    this.rtbResults.Text = ""; 

    ... 
    ... 

    private System.Windows.Forms.RichTextBox rtbResults; 

重要的是,它說「System.Windows.Forms.RichTextBox」,而不是別的東西。

OK,這是我奇怪的例子,它顯示一個拼字遊戲作弊程序的結果:

/// <summary> 
    /// Method to display the results in the RichTextBox, prefixed with "Results: " and with the 
    /// letters J, Q, X and Z underlined. 
    /// </summary> 
    private void DisplayResults(string resultString) 
    { 
    resultString = UnderlineSubString(resultString, "J"); 
    resultString = UnderlineSubString(resultString, "Q"); 
    resultString = UnderlineSubString(resultString, "X"); 
    resultString = UnderlineSubString(resultString, "Z"); 

    rtbResults.Rtf = @"{\rtf1\ansi " + "Results: " + resultString + "}"; 
    } 


    /// <summary> 
    /// Method to apply RTF-style formatting to make all occurrences of a substring in a string 
    /// underlined. 
    /// </summary> 
    private static string UnderlineSubString(string theString, string subString) 
    { 
    return theString.Replace(subString, @"\ul " + subString + @"\ul0 "); 
    } 

這將使用微軟獨有的豐富文本格式,但RichTextBox中也可以使用連勝文。而這個演示只寫入RTB,它不會讀取它,儘管這樣做相當微不足道。

希望這會有所幫助。

+0

我的問題的解決方案是:字符串dataFromBox = rtb.Text; – Rilcon42 2014-09-14 04:14:49