title: How to create a line break with Listbox using C# with Asp.net?
body: <p>I want to create a line break within a listbox so that the text does not drag all the way to which the user is unable to read the whole content of the text. </p>
  
  <p>This is how my current output looks like: 
  <a href="https://i.stack.imgur.com/JjF5f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JjF5f.png" alt="enter image description here"></a></p>
  
  <p>I want it such that when the text reaches the end of the listbox it goes down to the next line, in simple terms, word wrapping. I have went online to search and found out that it is impossible to implement a wrap text feature using listbox.  I managed to find a Word Wrap algorithm online and decided to use it, however I am unsure of how I can implement it into the listbox that I want it to be in. </p>
  
  <p>Here is the codes that I found: </p>
  
  <p>// <a href="https://www.codeproject.com/Articles/51488/Implementing-Word-Wrap-in-C" rel="nofollow noreferrer">https://www.codeproject.com/Articles/51488/Implementing-Word-Wrap-in-C</a></p>
  
  <pre><code>public static string WordWrap(string text, int width)
  {
      int pos, next;
      StringBuilder sb = new StringBuilder();
  
      // Lucidity check
      if (width &lt; 1)
          return text;
  
      // Parse each line of text
      for (pos = 0; pos &lt; text.Length; pos = next)
      {
          // Find end of line
          int eol = text.IndexOf(Environment.NewLine, pos);
          if (eol == -1)
              next = eol = text.Length;
          else
              next = eol + Environment.NewLine.Length;
  
          // Copy this line of text, breaking into smaller lines as needed
          if (eol &gt; pos)
          {
              do
              {
                  int len = eol - pos;
                  if (len &gt; width)
                      len = BreakLine(text, pos, width);
                  sb.Append(text, pos, len);
                  sb.Append(Environment.NewLine);
  
                  // Trim whitespace following break
                  pos += len;
                  while (pos &lt; eol &amp;&amp; Char.IsWhiteSpace(text[pos]))
                      pos++;
              } while (eol &gt; pos);
          }
          else sb.Append(Environment.NewLine); // Empty line
      }
      return sb.ToString();
  }
  
  /// &lt;summary&gt;
  /// Locates position to break the given line so as to avoid
  /// breaking words.
  /// &lt;/summary&gt;
  /// &lt;param name="text"&gt;String that contains line of text&lt;/param&gt;
  /// &lt;param name="pos"&gt;Index where line of text starts&lt;/param&gt;
  /// &lt;param name="max"&gt;Maximum line length&lt;/param&gt;
  /// &lt;returns&gt;The modified line length&lt;/returns&gt;
  private static int BreakLine(string text, int pos, int max)
  {
      // Find last whitespace in line
      int i = max;
      while (i &gt;= 0 &amp;&amp; !Char.IsWhiteSpace(text[pos + i]))
          i--;
  
      // If no whitespace found, break at maximum length
      if (i &lt; 0)
          return max;
  
      // Find start of whitespace
      while (i &gt;= 0 &amp;&amp; Char.IsWhiteSpace(text[pos + i]))
          i--;
  
      // Return length of text before whitespace
      return i + 1;
  }
  </code></pre>
  
  <p>Currently I put it as a method on its own, should i put this method directly in the listbox method itself?
  <a href="https://i.stack.imgur.com/JeyY7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JeyY7.png" alt="enter image description here"></a></p>
  
  <p>If yes, how can I modify the above codes to make it work? 
  By the way descLb is the name of my listbox</p>
  
  <p>Please dont suggest me to change my listbox to another form (eg. textbox), I only know how to extract text from the database to input into the listbox and to keep it simple for me I would like to use listbox. </p>