I want to count the words in richtextbox. First, i input a word in textbox1(for searching), and press "count" button, textbox2 will show a number about how many the same words in the richtextbox. Here are my codes, but it doesn't work, the textbox2 always shows 0.Thank you for your help.
private void button2_Click(object sender, EventArgs e)
{
string a=richTextBox1.Text;
string b=textBox1.Text;
for (int i = 0; i < a.Length; i++)
{
int n=0;
if (a.Equals(b))
{
n++;
}
textBox2.Text = n.ToString();
}
}
You need to compare the individual word to b, not the whole sentence. You can use the following code as a reference:
string[] data = richTextBox1.Text.Split(' ');
for(int i=0;i<data.Length;i++)
{
if(data[i]==textBox1.Text)
n++;
}
Try this one:
string data = richTextBox1.Text;
var target = textBox1.Text;
var count = data.Select((c, i) => data.Substring(i))
.Count(sub => sub.ToUpper()
.StartsWith(target));
textBox2.Text = count;
It's a bit more easier to show word count for richtextbox like this:
Dim wordcount As Integer
Dim a As String() = RichTextBox1.Text.Split(" ")
wordcount = a.Length
You can use 'wordcount' for the word count. For example:
Label1.Text = "Word Count: " & wordcount
Related
I would like to know how can I add or type a number in a textbox, then this number gets saved, then add other numbers, and save them so at the end I can order them from larger to smaller and viceversa.
I got one textbox (where I type the numbers), one button (add button, that adds the typed number to the textbox2), another textbox2(where the numbers are being added simultaneously, so you can check them). There is a textbox3 (where the numbers must appear ordered from larger to smaller) and a textbox4 (where the numbers must appear ordered from smaller to larger).
Can someone help me?
Not perfect but it works. Try it.:)
//index count
int index=0;
//array declaration
string [] numbers=new string[10];
//method displaying array's content
string arrayDisplay() {
string str="";
for (int i = 0; i < numbers.Length; i++)
{
if (!(numbers[i]== "") )
{
str += numbers[i];
}
}
return str;
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text += textBox1.Text;
index++;
if (numbers.Length >=index )
{
numbers[index] = textBox1.Text;
textBox1.Text = "";
}
//Regular sort and display
Array.Sort(numbers);
textBox3.Text = arrayDisplay();
//Reverse sort and display
Array.Reverse(numbers);
textBox4.Text = arrayDisplay();
}
I have text in textBox1 "Aplle,Orandge,Nuts,Cherries". There can be more words added in text box, even 30.
I need to find combination like this:
First click - I get textBox2.Text = "Aplle" AND textBox3.Text = "Orandge,Nuts,Cherries"
Second click textBox2.Text = "Aplle,Orandge" AND textBox3.Text = "Nuts,Cherries"
Third textBox2.Text = "Aplle,Orandge,Nuts" AND textBox3.Text = "Cherries"
First I understand that I need counter which count commas, if CommaCounter=1, than split after first comma, if CommaCounter=2, split after second comma, if CommaCounter=100, than split after comma 100.
Than I need splitter, I guess, but and don't understand how to really make it.
Why I need find every combination click by click because every combination will be used by another click witch makes another function.
I insert a pictures how it should look.
here is code I started, of course unfinished, incorrect...
int CommaCounter = 0;
string Part1, Part2;
string Vards = textBox1.Text;
char[] delimiterChars = { ',' };
string[] words = Vards.Split(delimiterChars);
string searchTerm = ",";
var matchQuery = from word in words
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
select word;
int wordCount = matchQuery.Count();
foreach (string s in words)
{
textBox2.Text = Part1.ToString();
textBox3.Text = Part2.ToString();
}
First, declare a variable (let's say its name is counter) in the form. The initial value is 0. It stores how many clicks have you done.
// stores how many clicks you have done
int counter = 0;
In the button's event handler, split the text in the first textbox using a comma.
// split the first textbox using a comma
var words = textBox1.Text.Split(',');
Then compare the counter variable with the number of words in the first textbox. If counter is lesser than the number of words, split the words into the second textbox and the third textbox.
if (counter < words.Count())
{
// increase the counter
counter++;
// take n words, where n = counter
textBox2.Text = string.Join(",", words.Take(counter));
// skip n words, then take the rest (n = counter)
textBox3.Text = string.Join(",", words.Skip(counter).Take(words.Count() - counter));
}
You can get the value of the second textbox using Take() method. The Take() method get some element from the words. For the third textbox, you need to skip some words before you take the words. You skip the words using Skip() method.
Full source.
using System;
using System.Windows.Forms;
using System.Linq;
namespace Sample
{
public partial class MainForm : Form
{
// stores how many clicks you have done
int counter = 0;
public MainForm()
{
InitializeComponent();
}
void Button1Click(object sender, EventArgs e)
{
// split the first textbox using a comma
var words = textBox1.Text.Split(',');
if (counter < words.Count())
{
// increase the counter
counter++;
// take n words, where n = counter
textBox2.Text = string.Join(",", words.Take(counter));
// skip n words, then take the rest (n = counter)
textBox3.Text = string.Join(",", words.Skip(counter).Take(words.Count() - counter));
}
}
}
}
Keeping it simple:
void Button1Click(object sender, EventArgs e)
{
string divider = ",";
if(textBox2.Text == "") // first click.
{
textBox3.Text = textBox1.Text;
divider = "";
}
int pos = textBox3.Text.IndexOf(",");
if(pos > -1)
{
textBox2.Text = textBox2.Text + divider + textBox3.Text.Substring(0, pos);
textBox3.Text = textBox3.Text.Substring(pos + 1);
}
else if (textBox3.Text != "" ) // last one
{
textBox2.Text = textBox2.Text + divider + textBox3.Text;
textBox3.Text = "";
}
}
write a loop that prints a number of lines the user entered into the text box
for example, this is using c# windows application
user inputs 10, then in another textbox counts from 0 to 10 on different lines
Result
0 \r\n
1 \r\n
2 \r\n
3 \r\n
4 \r\n
5 \r\n
6 \r\n
7 \r\n
8 \r\n
9 \r\n
10 \r\n
i have tried to incorporate a for loop and for each but it is only printing out one value, and i have to go into the array and print each iteration of the array like so textboxOutput.text = Array[0] + Array[1] , but i want it to print out all of the list without accessing the single ints in the array
private void button1_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
int Number;
bool Anumber = Int32.TryParse(input, out Number);
int newNumber = Number;
int[] List = new int[newNumber];
for (int i = 0; i < List.Length; i++ )
{
List[i] = i;
//textBox2.Text =List[0] + "\r\n" + List[1] + "\r\n" + List[2];
}
foreach (int num in List)
{
textBox2.Text = num.ToString() ;
}
}
}
}
I have managed to figure out my own question
private void button1_Click(object sender, EventArgs e)
{
int OutputNumber;
bool number = Int32.TryParse(inputtxt.Text, out OutputNumber);
string[] array = new string[OutputNumber];
int put = 0;
for (int i = 0; i < array.Length; i++)
{
array[i] = put.ToString();
put++;
string result = ConvertStringArrayToString(array);
string result1 = result + OutputNumber.ToString();
outputtxt.Text = result1;
}}
static string ConvertStringArrayToString(string[] array)
{
StringBuilder buildingstring = new StringBuilder();
foreach (string value in array)
{
buildingstring.Append(value);
buildingstring.Append("\r\n");
}
return buildingstring.ToString();
}
I have gone around a different way, by creating a string array adding each value to the string array and using a separate function to join all of the string array data together and separating each value with a return
I have some kind of logical error in my program. Whenever I enter a phrase with 1 letter I get a ArgumentOutOfRange Exception, and whenever I enter a multiple letter word the textbox clears, displays "Apple" (the first value in my array) and does nothing else. Can anybody see the logical error in this?
string[] d = { "Apple", "Bass", "Cat", "Dog", "Ear", "Flamingo", "Gear", "Hat", "Infidel", "Jackrabbit", "Kangaroo", "Lathargic", "Monkey", "Nude", "Ozzymandis", "Python", "Queen", "Rat", "Sarcastic", "Tungston", "Urine", "Virginia", "Wool", "Xylophone", "Yo-yo", "Zebra", " " };
string var;
int len = 0;
private void button1_Click(object sender, EventArgs e)
{
var = textBox2.Text;
textBox1.Text = "";
for (int y = 0; y < var.Length; y++)
{
for (int x = 0; x < d.Length; x++)
{
if (d[x].ToUpper().Substring(0, 0) == var.ToUpper().Substring(len, len))
{
len = len + 1;
textBox1.Text = textBox1.Text + "\n" + d[x];
}
}
}
}
Substring(0, 0) is really pointless. This will always be an empty string.
Substring(len, len) also is a bad idea, because it will return a string of length len starting at index len. This is where you get your exception.
I assume, what you really want is the second parameter to be 1 in both cases. And that can be further simplified to an access via index:
d[x].ToUpper()[0] == var.ToUpper()[len]
You can do the same quite easily using LINQ:
private void button1_Click(object sender, EventArgs e)
{
var dict = d.ToDictionary(x => x.First(), x => x);
textBox1.Text = string.Join(Environment.NewLine, textBox2.Text.Select(x => dict[char.ToUpper(x)]));
}
To do it without LINQ I would suggest following:
for (int y = 0; y < input.Length; y++)
{
for (int x = 0; x < d.Length; x++)
{
if (char.ToUpper(d[x][0]) == char.ToUpper(input[y]))
{
result = result + "\n" + d[x];
}
}
}
Changes are:
you don't need len variable. Use y instead.
you don't need whole string uppercased. Use char.ToUpper static method instead.
you don't need string.Substring method. Use indexers instead.
This line explains the behavior.
if (d[x].ToUpper().Substring(0, 0) == var.ToUpper().Substring(len, len))
Second parameter of substring is string length. So on the left you always have an empty string. On the right you also have empty string when len==0 (that's why your code always picks Apple).
After that you change len, and repeat the loop. Then expression on the right is var.Substring(1,1) which gives you the error if your string is 1 character long. Because this reads - 1 symbol starting with 1 (which is second character of the string)
Apart from that, the purpose of the code is complete mystery, so there are definitely other errors.
I have a problem while trying to replace all text matching a particular word in a rich text box. This is the code i use
public static void ReplaceAll(RichTextBox myRtb, string word, string replacer)
{
int index = 0;
while (index < myRtb.Text.LastIndexOf(word))
{
int location = myRtb.Find(word, index, RichTextBoxFinds.None);
myRtb.Select(location, word.Length);
myRtb.SelectedText = replacer;
index++;
}
MessageBox.Show(index.ToString());
}
private void btnReplaceAll_Click(object sender, EventArgs e)
{
Form1 text = (Form1)Application.OpenForms["Form1"];
ReplaceAll(text.Current, txtFind2.Text, txtReplace.Text);
}
This works well but i have noticed a little malfunction when i try to replace a letter with itself and another letter.
For example i want to replace all the e in Welcome to Nigeria with ea.
This is what i get Weaalcomeaaaaaaa to Nigeaaaaaaaaaaaaaaria.
And the message box shows 23 when there are only three e. Pls what am i doing wrong and how can i correct it
Simply do this:
yourRichTextBox.Text = yourRichTextBox.Text.Replace("e","ea");
If you want to report the number of matches (which are replaced), you can try using Regex like this:
MessageBox.Show(Regex.Matches(yourRichTextBox.Text, "e").Count.ToString());
UPDATE
Of course, using the method above has expensive cost in memory, you can use some loop in combination with Regex to achieve some kind of advanced replacing engine like this:
public void ReplaceAll(RichTextBox myRtb, string word, string replacement){
int i = 0;
int n = 0;
int a = replacement.Length - word.Length;
foreach(Match m in Regex.Matches(myRtb.Text, word)){
myRtb.Select(m.Index + i, word.Length);
i += a;
myRtb.SelectedText = replacement;
n++;
}
MessageBox.Show("Replaced " + n + " matches!");
}