word wrap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • humdum
    New Member
    • May 2007
    • 7

    #1

    word wrap

    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,
    Code:
    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
    			
    		}
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Wouldn't it be easier to just use the properties of the textbox to show the characters around?
    They support word-wrapping and the amount of characters per line(in a way)

    Comment

    Working...