Not able to count line number in richtextbox - c#

here is my code
private void Allocation_Matrix_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
int Total_Row_Check;
Total_Row_Check = getRow();
Textbox1.Text = Convert.ToString(Total_Row_Check);
if (Total_Row_Check >= Convert.ToInt32(Total_Processes.Text))
{
MessageBox.Show("rows cannot exceed from total number of processess");
}
}
}
public int getRow()
{
int Row = 0;
string[] arLines;
int i;
arLines = Allocation_Matrix.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (i = 0; i < arLines.Length; i++)
{
Row++;
}
return Row;
}
i want to update TextBox1 as i hit ENTER in richtextbox from keyboard...but Textbox keeps show only FirstRow and shows One(1)

How about using RichTextBox.GetLineFromCharIndex Method? It returns, the zero-based line number in which the character index is located.

Is Allocation_Matrix your RichTextBox?
Note that RichTextBox.Text returns the text with line feed ("\n") for the line breaks, where most all other Windows controls use a carriage return, line feed ("\r\n").
So if you are calling Split on the string returned by the Text property, it won't contain any Evironment.NewLine.

try Allocation_Matrix.Text.Lines.Count() method

try this
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}

Related

how to display the number of letters in alphabet in label while typing in the textbox

I'm trying to make a program that whenever you type a letter in the textbox the number of the letter in alphabet would appear in the label......
I've tried some codes like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string userInput = textBox1.Text;
char charCount;
charCount = userInput[0];
label1 = charCount.Length.ToString();
}
But I can't find my solution for my problem.....
I'll appreciate the help I can get....
Show letter position in alphabet.
private void textBox1_TextChanged(object sender, EventArgs e)
{
string userInput = textBox1.Text; //get string from textbox
if(string.IsNullOrEmpty(userInput)) return; //return if string is empty
char c = char.ToUpper(userInput[userInput.Length - 1]); //get last char of string and normalize it to big letter
int alPos = c-'A'+1; //subtract from char first alphabet letter
label1 = alPos.ToString(); //print/show letter position in alphabet
}
First of all, you need to find an event, that is triggered, when you text in the textbox is changed. For example KeyUp. Then you need to register a function to it by using code like this.
your_textbox.KeyUp += your_textbox_KeyUp;
Visual Studio will help you by creating an empty function.
The function should look like this:
private void your_textbox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
your_label.Content = your_textbox.Text.Length.ToString();
}
your_label.Content is the Property that will be displayed in the label and the term on the right will get the length of the text in your textbox.
If you want the label not only to say the number, but wrap it in a text, use String.Format like this:
your_label.Content = String.Format("The text is {0} characters long", your_textbox.Text.Length);
My answer is aiming for WPF though. If you are using WinForms some keywords might be different.
I believe that you want only the number of letters (alphabet) written in the textbox, here's a simple code :
private void textbox_TextChanged(object sender, EventArgs e)
{
int i = 0;
foreach (char c in textbox.Text)
{
int ascii = (int)c;
if ((ascii >= 97 && <= 122) || (ascii >= 65 && ascii <= 90)) // a to z or A to Z
i++;
}
label1.Text = i.ToString();
}
More simple code :
private void textbox_TextChanged(object sender, EventArgs e)
{
int i = 0;
foreach (char c in textbox.Text)
if (char.IsLetter(c))
i++;
label1.Text = i.ToString();
}
If you're looking for the number of distinct letters in the textbox, you could use:
textbox.Text.ToUpper().Where(char.IsLetter).Distinct().Count();

Insert data in RichTextBox C#

My code:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text = infos;
}
timer4.Stop();
}
I want to insert all of 10 src values in RichTextBox, while my code do it only once.
You can use AppendText
Replace
richTextBox1.Text = infos;
with
richTextBox1.AppendText(infos);
OR
richTextBox1.Text += infos + Environment.NewLine;
This line is wrong.
richTextBox1.Text = infos;
This is right.
richTextBox1.AppendText= infos;
What your code is doing is setting the text to equal each infos, 10 times over.
So I'm guessing your output would be the last infos variable? What you might want to do instead is this:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text += infos; // the "+=" will add each infos to the textbox
}
timer4.Stop();
}
As you can see, if you use the += instead of just =, it will add each iteration to the whole, instead of just overriding the whole value each time.

Auto formatting a textbox text

I want to auto format a text entered in a textbox like so:
If a user enters 2 characters, like 38, it automatically adds a space. so, if I type 384052
The end result will be: 38 30 52.
I tried doing that, but it's ofr some reason right to left and it's all screwed up.. what I'm doing wrong?
static int Count = 0;
private void packetTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
Count++;
if (Count % 2 == 0)
{
packetTextBox.Text += " ";
}
}
Thanks!
It's much nicer if you just let the user type and then modify the contents when the user leaves the TextBox.
You can do that by reacting not to the KeyPress event, but to the TextChanged event.
private void packetTextBox_TextChanged(object sender, EventArgs e)
{
string oldValue = (sender as TextBox).Text.Trim();
string newValue = "";
// IF there are more than 2 characters in oldValue:
// Move 2 chars from oldValue to newValue, and add a space to newValue
// Remove the first 2 chars from oldValue
// ELSE
// Just append oldValue to newValue
// Make oldValue empty
// REPEAT as long as oldValue is not empty
(sender as TextBox).Text = newValue;
}
On TextChanged event:
int space = 0;
string finalString ="";
for (i = 0; i < txtbox.lenght; i++)
{
finalString = finalString + string[i];
space++;
if (space = 3 )
{
finalString = finalString + " ";
space = 0;
}
}
I used
int amount;
private void textBox1_TextChanged(object sender, EventArgs e)
{
amount++;
if (amount == 2)
{
textBox1.Text += " ";
textBox1.Select(textBox1.Text.Length, 0);
amount = 0;
}
}
Try this..
on TextChanged event
textBoxX3.Text = Convert.ToInt64(textBoxX3.Text.Replace(",", "")).ToString("N0");
textBoxX3.SelectionStart = textBoxX3.Text.Length + 1;

How to access the indexes of all listbox items?

I am creating an application to search the user typed word from list box. I want to Show only that items in listbox which are matched with the character typed by the user. I am unable to find the exact syntax for this.
private void textBox1_TextChanged(object sender, EventArgs e)
{
string a=textBox1.Text;
for (int i = 0; i < listBox1.Items.Count; i++)
{
if(a[0]==listBox1.Items(i).char[0])//how to do this?
{........
}
}
}
if you want to check the char of a do something like this
also if you are not getting the "Text / String Value.. add the .ToString(); after listBox1.Items[i].ToString();
if(a[i]== listBox1.Items[i])
{
//i is the incremented value here..
}
foreach (char valchar in a)
{
// do your logic.. 'X' single quotes for Char
}
if you want to check for a string in a do
foreach (string valString in a)
{
// do your logic for a string check if valString = "X" for example "" double quotes for
}
Like this:
string a = textBox1.Text;
for (int i = 0; i < listBox1.Items.Count; i++)
{
if( a[0] == listBox1.Items[i].Text)
{
//Do Something...
}
}

How to color some words in differents lines in the RichTextBox?

I'm in trouble, today I tried to color some diffents words in differents lines clicked on a button. Can You explain me how to do this? I was able to do only this:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Select(int start , int length); //It's wrong but It explains How to use .Select if you know start and length...
richTextBox1.SelectionColor = Color.Blue;
}
But how can I work on a line I know the number of, having already the text into the RichTextBox?
Thanks.
If lines are separated by \n, your problem is then to count the number of \n characters before getting to the wanted line.
For that, you can use an extension method:
public static int NthIndexOf(this String str, String match, int occurence) {
int i = 1;
int index = 0;
while (i <= occurence && ( index = str.IndexOf(match, index + 1) ) != -1) {
if (i == occurence) {
// Occurence match found!
return index;
}
i++;
}
// Match not found
return -1;
}
Now, you can find start and end values to color the selection:
private void button1_Click(object sender, EventArgs e) {
int lineNb = 13; // I assume you get this value initialized somewhere,
// I wrote 13 for the example
int start = richTextBox1.Text.NthIndexOf("\n", lineNb);
int length = richTextBox1.Text.NthIndexOf("\n", lineNb + 1);
richTextBox1.Select(start , length);
richTextBox1.SelectionColor = Color.Blue;
}

Categories