如何计算RichTextbox

时间:2016-12-13 23:10:10

标签: c# winforms

我有一个程序,我需要计算已经读入richtextbox的文件中有多少女性和男性,但我不知道该怎么做,在文件中有名称,性别,具体工作。我必须计算15个不同的人

例如:“Donna,女,人力资源。”,

这是我到目前为止所做的:

 private void Form1_Load(object sender, EventArgs e)
{
    StreamReader sr;
    richTextBox1.Clear();
    sr = new StreamReader("MOCK_DATA.txt");
    string data;
    while (!sr.EndOfStream)
    {
        data = sr.ReadLine();
        richTextBox1.AppendText(data + "\n");
    }
}

private void button1_Click(object sender, EventArgs e)
{
    string[] data = richTextBox1.Text.Split(',');
    for (int n = 0; n < data.Length; n++)
    {
        if (data[n] == richTextBox1.Text)
            n++;

2 个答案:

答案 0 :(得分:0)

从RichTextBox获取纯文本(从this article被盗):

string StringFromRichTextBox(RichTextBox rtb)
{
    TextRange textRange = new TextRange(
        // TextPointer to the start of content in the RichTextBox.
        rtb.Document.ContentStart, 
        // TextPointer to the end of content in the RichTextBox.
        rtb.Document.ContentEnd
    );

    // The Text property on a TextRange object returns a string
    // representing the plain text content of the TextRange.
    return textRange.Text;
}

基本单词计算程序:

int CountWord(string textToSearch, string word)
{ 
    int count = 0;
    int i = textToSearch.IndexOf(word);
    while (i != -1)
    {
        count++;
        i = textToSearch.IndexOf(word, i+1);
    }
    return count;
}

把它放在一起:

var plainText = StringFromRichTextBox(richTextBox1);
var countOfMale = CountWord(plainText, "Male");
var countOfFemale = CountWord(plainText, "Female");

答案 1 :(得分:0)

private void toolStripButton81_Click(object sender, EventArgs e)
{
                    string findterm = string.Empty;
                    findterm = toolStripTextBox2.Text;
                    // the search term - specific word
                    int loopCount = 0;
                    // count the number of instance
                    int findPos = 0;

                    // depending on checkbox settings
                    // whole word search or match case etc

                    try
                    {
                        while (findPos < GetRichTextBox().Text.Length)
                        {
                            if (wholeWordToolStripMenuItem.CheckState == CheckState.Checked & matchCaseToolStripMenuItem.CheckState == CheckState.Checked)
                            {
                                findPos = GetRichTextBox().Find(findterm, findPos, RichTextBoxFinds.WholeWord | RichTextBoxFinds.MatchCase);
                            }
                            else if (wholeWordToolStripMenuItem.CheckState == CheckState.Checked)
                            {
                                findPos = GetRichTextBox().Find(findterm, findPos, RichTextBoxFinds.WholeWord);
                            }
                            else if (matchCaseToolStripMenuItem.CheckState == CheckState.Checked)
                            {
                                findPos = GetRichTextBox().Find(findterm, findPos, RichTextBoxFinds.MatchCase);
                            }
                            else
                            {
                                findPos = GetRichTextBox().Find(findterm, findPos, RichTextBoxFinds.None);
                            }
                            GetRichTextBox().Select(findPos, toolStripTextBox2.Text.Length);


                            findPos += toolStripTextBox2.Text.Length + 1;
                            loopCount = loopCount + 1;

                        }
                    }
                    catch
                    {
                        findPos = 0;

                    }
                    // at the end bring the cursor at the beginning of the document
                    GetRichTextBox().SelectionStart = 0;
                    GetRichTextBox().SelectionLength = 0;
                    GetRichTextBox().ScrollToCaret();
                    // Show the output in statusbar
                    toolStripStatusLabel2.Text = "Instances: " + loopCount.ToString();               
}