Problem:

Put the parameters for TextBox Multiline = true; and WordWrap = true; . Everything is fine, transfers, but I do not understand: how to find out the number of lines in which my text breaks with the specified width parameters


ps main task

When changing the text, adjust the height of the TextBox to the required dimensions.

  • How about counting the number of hyphens in a line? - Sugar Sugar
  • @ Sugar as an option, but not sure that these characters will be in a line that was simply transferred by Multiline and WordWrap - IVsevolod

2 answers 2

Not the most pleasant task, to be honest ...

At one time, it was solved through Graphics.MeasureString

  • thanks, I'll try now :) - IVsevolod
  • I tried, it turns out too hard ... As a result, I solved the main task without counting the number of lines, using the GetPreferredSize method - IVsevolod

Found one method that solves my problem:

  this.textBox1.TextChanged += delegate { Size oldSize = this.textBox1.Size; Size newSize = textBox1.GetPreferredSize(oldSize); this.textBox1.Height = newSize.Height; }; 
  • 2
    In the case of the control, yes, I didn’t realize the right decision :) I’m not looking for easy ways, but my task was for the custom control, so it was much worse. - Spawn
  • @Spawn: In this case, WPF and its layout manager are probably the best solution ... - VladD
  • @VladD, I am also a big WPF supporter, but, alas, I have to do something under WinForms - Spawn