I'm not sure what's wrong here, but i'm trying to count words in a richtext box, and display that with a label.
I put a richtextbox in a tab control so I can have a tabbed text box. Which seems to make this a lot harder then it should
also this isn't the whole program, I took the parts relating to the richtextbox and word counter
Any help is appreciated :)
public RichTab()
{
InitializeComponent();
TabPage tp = new TabPage("Document");
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
WordCount();
}
public RichTextBox RTTB()
{
RichTextBox rtb = null;
TabPage tp = tabControl1.SelectedTab;
if (tp != null)
{
rtb = tp.Controls[0] as RichTextBox;
}
return rtb;
}
private void WordCount()
{
MatchCollection wordColl = Regex.Matches(RTTB().Text, #"[\W]+");
label2.Text = wordColl.Count.ToString();
}
I would probably just wire up the TextChanged event of the RichTextBox, and count the words there:
rtb.TextChanged += rtb_TextChanged;
Then count the words (using Giorgio Minardi's regex):
private void rtb_TextChanged(object sender, EventArgs e) {
label2.Text = Regex.Matches(((RichTextBox)sender).Text, #"[\S]+").Count.ToString();
}
What's the actual issue ?
Here's a simple routine to count words:
[Test]
public void CountWords()
{
const string sample = "How you doing today ?";
MatchCollection collection = Regex.Matches(sample, #"[\S]+");
var numberOfWords = collection.Count;
//numberOfWords is 5
Assert.IsTrue(numberOfWords == 5);
}
Related
When I click a button with a word e.g "cat" I want this word in richTextBox1 change to color red. Of course I made it wrong, but I would like to learn how to change it.
private void btn1(object sender, EventArgs e)
{
Button button = sender as Button;
string wordToColor = button.Text;
ChangeColorOfText(richTextBox1, wordToColor);
}
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
{
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
int index;
do
{
index = richTextBox1.Find(word);
if (index >= 0)
{
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = ColorDialog.Red;
}
}
while (index >= 0);
}
I had few things that may be a solution for you:
It was a problem for me but it's not a must:
private void button1_Click(object sender, EventArgs e)
{
//Your code for finding and selecting your text
if (!String.IsNullOrEmpty(myRichTextBox.Text))
{
//Your code for selecting your text
myRichTextBox.SelectionStart = 0;
myRichTextBox.SelectionLength = myRichTextBox.Text.Length;
}
myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
myRichTextBox.SelectionColor = Color.Red;
}
I think your problem was not setting the font before you set the color
(When I added the line myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold); everything worked)
The Second thing I did in the code was to use
myRichTextBox.SelectionColor = Color.Red;
insted of
richTextBox1.SelectionColor = ColorDialog.Red;
it's easier and more efficient.
At the line
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
you dont have to add the RichTextBox richTextBox1 element. (worked fine for me without it).
I deleted ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
and changed richTextBox1.SelectionColor = ColorDialog.Red; torichTextBox1.SelectionColor = Color.Red;
I thought it will work but just when I write the same word in the text box that is in the button and click on the button the form freezes.
I want to search a word in rich edit control. I will write a word in a text box and then click on a button named search button, and then the searched word will be highlighted in the rich edit control.
How can I solve this problem?
In rich text box we use RichTextBox.TextLength, but rich edit control doesn't support this.
private void button2_Click(object sender, EventArgs e) //Search button
{
int index = 0;
while(index <= richEditControl1.Text.LastIndexOf(textBox1.Text))
{
richEditControl1.Find(textBox1.Text, index, richEditControl1.TextLength, RichTextBoxFinds.None);
richEditControl1.SelectionBackColor = Color.IndianRed;
index = richEditControl1.Text.IndexOf(textBox1.Text, index) + 1;
}
}
Try with StartSearch method. Something like this.
private void SearchRTF(string mytext) {
var result = myRichEditControl.Document.StartSearch(mytext);
if (result.FindNext()) {
var section = myRichEditControl.Document.BeginUpdateCharacters(result.CurrentResult);
section.ForeColor = System.Drawing.Color.White;
section.BackColor = System.Drawing.Color.Blue;
myRichEditControl.Document.EndUpdateCharacters(section);
}
}
I have a ComboBox with two resolution (1600x900 and 1280x720), I want them to replace "-screen-width XXXX -screen-height YYY" by the resolution chosen , in my TXT file when I press the "Save and Close" boutton, for the moment I've tried anything because I'm a real beginner at coding, it's my first Program I ever made.
Basically, my program will be an easy way to edit launch options for guys who don't know them
This is what I have in "InitializeComponent();"
public Window1()
{
InitializeComponent();
listResolution.Add("1600x900");
listResolution.Add("1280x720");
widthChoose = 1280;
heightChoose = 720;
windowed = true;
foreach (String item in listResolution)
{
ResolutionBox.Items.Add(item);
}
}
This is what I have for my "Save and Close" boutton (The text replace doesn't work)
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
if (Windowed.IsChecked == true)
windowed = true;
else
windowed = false;
string text = File.ReadAllText(#"Resources\arguments.txt");
text = text.Replace("-screen-fullscreen 1", "-screen-fullscreen 0");
File.WriteAllText("arguments.txt", text);
this.Close();
And I have no event for my comboBox
private void ResolutionBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
Content of my "arguments.txt"
-screen-fullscreen 0 -screen-width 1600 -screen-height 900
Right before
File.WriteAllText("arguments.txt", text);
Add these :
First we take the selected resolution (not sure the purpose of listResolution there)
var selectedResolution = ResolutionBox.SelectedItem.ToString();
split it to width and height
var split = selectedResolution.Split('x');
widthChoose = split[0];
heightChoose = split[1];
then replace 1600 & 900 with the new values :
text = text.Replace("1600",widthChoose)
continue with height.
I have a rich text box that i am using to display an example of the Hello World program and want the keywords such as 'using' 'namespace' 'class' 'static' 'void' 'string' to display blue.
I have got 'using' to display blue but only when the user starts to type or types 'using'
I want the richtextbox to be read only not allowing an user input into it
Here is my code:
private void rtb_TextChanged(object sender, EventArgs e)
{
string find = "using";
if (rtb.Text.Contains(find))
{
var matchString = Regex.Escape(find);
foreach (Match match in Regex.Matches(rtb.Text, matchString))
{
rtb.Select(match.Index, find.Length);
rtb.SelectionColor = Color.Blue;
rtb.Select(rtb.TextLength, 0);
rtb.SelectionColor = rtb.ForeColor;
};
}
}
I am having trouble figuring out how to do it for readonly rtb.
How could i edit my code to display the blue text on initialize in a readonly rtb
Thanks for your help
There's no need to subscribe to the TextChanged event.
Place your code in a separate method:
private void ApplySyntaxHighlite()
{
string find = "using";
if (richTextBox1.Text.Contains(find))
{
var matchString = Regex.Escape(find);
foreach (Match match in Regex.Matches(richTextBox1.Text, matchString))
{
richTextBox1.Select(match.Index, find.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.Select(richTextBox1.TextLength, 0);
richTextBox1.SelectionColor = richTextBox1.ForeColor;
};
}
}
And call it after you set the text in the RichTextBox:
richTextBox1.Text =
"using System;\r\nusing System.IO;\r\n\r\nConsole.WriteLine(\"Hello world.\");";
ApplySyntaxHighlite();
I'm working on alittle writing game and I encountered a problem. I have a richtextbox which contains one paragraph with alot of runs in it, and I want it to scroll to some run i have. I have a List object which contains all the inlines in the RTB, and the Select() Method doesn't work for some reason, maybe because it's a ready-only rtb. Any ideas for a way to scroll to selected word?
My Code:
private bool isKey = false;
private Paragraph p;
private List<Inline> inlineList;
private int inlineIndex = 0, wpm = 0, wordIndex = 0, lineIndex = 0;
private string[] words;
public MainWindow()
{
InitializeComponent();
p = new Paragraph();
foreach (string s in words)
{
p.Inlines.Add(s);
p.Inlines.Add(" ");
}
WordBox.Document.Blocks.Clear();
WordBox.Document.Blocks.Add(p);
inlineList = p.Inlines.ToList();
inlineList[0].Background = Brushes.LightGray;
this.Activate();
InputBox.Focus();
}
//The Method I want to put the scrolling feature in:
private void MoveWord()
{
if (inlineIndex + 2 < inlineList.Count)
{
inlineList[inlineIndex].Background = Brushes.Transparent;
inlineIndex += 2;
inlineList[inlineIndex].Background = Brushes.LightGray;
WordBox.Selection.Select(inlineList[inlineIndex].ContentStart, inlineList[inlineIndex].ContentEnd);
}
else
MessageBox.Show(wpm.ToString());
}
For Example:
the rtb contains:
Hey Hello what's up
word schnitzel hey
And I want it to scroll to the word "hey".
I've tried to use Select() method, which didn't work...
Create runs and add runs (not inlines)
Save a reference to the runs (e.g. a List)
And then just call Runs[x].BringIntoView()
Have not tested on RTB but I have done this with FlowDocument and FlowDocumentViewer
BringIntoView