Click to See Complete Forum and Search --> : Plz help me


Humdum
September 19th, 2007, 04:46 AM
the below code is for word wrap where user can specify no of char per line
on text_change event

there is problem if anyone can help me to remove this, plz plz


if user enter 3-4 lines:
"This is trial message plz ignore.
This is trial message plz ignore.
This is trial message plz ignore.
This is trial message plz ignore."

and now when user comes back to first line and enter some data in between, then the problem starts,

private void txtBody_TextChanged(object sender, System.EventArgs e)
{
#region "wrap"
string[] tempArray = new string [txtBody.Lines.Length];
tempArray = txtBody.Lines;

int PrevSel = txtBody.SelectionStart;
string Op = "";

//No. of chracter user can specify
int maxLength = Convert.ToInt32(textBox1.Text);
bool change= false;

for(int cou = 0; cou < tempArray.Length ; cou++)
{
if (tempArray[cou].Length > maxLength)
{
string[] words = tempArray[cou].Split(' ');
int currentLineLength = 0;
string currentLine = "";
foreach(string currentWord in words)
{
if(currentWord.Length >= 0)
{
if(currentWord.Length >= maxLength)
{
Op += currentWord.Insert(maxLength, "\r\n");
break;
}
if(currentLineLength + currentWord.Length + 1 < maxLength)
{
currentLine += currentWord + " ";
currentLineLength += currentWord.Length +1;

}
else
{
Op +=currentLine.Insert(currentLineLength, "\r\n");
currentLine = currentWord + " ";
currentLineLength = currentWord.Length;
}
}
}
if(currentLine !="")
Op += currentLine;

PrevSel++;
change = true;
}
else
{
Op += tempArray[cou] + "\r\n";
}
if(change)
{
txtBody.Text = Op;
txtBody.SelectionStart = PrevSel;
}
}
#endregion

}

Thread1
September 19th, 2007, 06:42 AM
can't you just set the margin of the textbox? :D

JonnyPoet
September 19th, 2007, 03:54 PM
Hi !

I cannot understand this line

int maxLength = Convert.ToInt32(txtBody.Text);
My progarm also doesn't understand . :D it crashes here :lol: :lol:
You cannot transform text to a number What do you want to get here ? Maybe you want to read out another textbox where only numbers are allowed for input and there the size of your lines is defined ?

So for testing I did this and entered 25 in textBox1 and changed the above line so it reads maxLength to 25.

Also when I tested this way then, inserting text it doesn't find the correct position of the actual cursor after breaking the line so this causes troubles when typing So it seems me to have troubles in counting letters or maybe you need to do a look on your full logic here, because this logic does also not look for not to break words into parts looking for grammer rules.