添加引用后不包含定义
本文关键字:包含 定义 引用 添加 | 更新日期: 2023-09-27 18:21:42
我是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);
}
}
}
}
}
这里有一个(相当奇怪)示例,展示了在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",而不是其他内容。
好吧,这是我的一个奇怪的例子,它显示了拼字游戏作弊程序的结果:
/// <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,而不从中读取,尽管这样做相当琐碎。
希望这能有所帮助。