.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 13th, 2009, 12:36 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
Is it possible to make an autosizing textbox?

I have a textbox that starts off a certain size. But, if the user types in enough text to require scrolling in the textbox, then I want to resize the textbox to contain all the text so that scrolling isn't necessary.

For most controls, I would simply use the Control.DisplayRectangle.Size property, but when used for a textbox, this is just the size of the textbox itself, regardless of whether the text extends beyond the bounds of the textbox or not.

I have tried using Graphics.MeasureString to get the size I need, and I get close but not quite what I need for professional results. And I know the textbox already knows the size I'm looking for, otherwise, it couldn't produce proper scrolling behavior when the text goes beyond the bounds of the textbox.

Any ideas?

--webdunce

Last edited by WebDunce : March 15th, 2009 at 07:25 PM. Reason: better title

Reply With Quote
  #2  
Old March 13th, 2009, 02:57 PM
ArekBulski ArekBulski is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Location: Lodz, Poland
Posts: 31 ArekBulski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 19 m 57 sec
Reputation Power: 5
A possible solution and spooky idea

I can only suggest using that Graphics.MeasureString method with some additional margin. I scratched this code specially for you, so please tell me what you think.

Uploaded the class, along with runnable example:
speedyshare.com/181911114.html

An idea, if it's worth anything. When you make a class based on TextBox, you can override (and access?) some hidden properties. One such property is AutoSize, but this one is not relevant in this case. If you find a relevant member for solution, then create a class derived from Textbox (like AutosizeTextbox : TextBox).

C# Code:
Original - C# Code
  1.  
  2. /// <summary>
  3. /// Use this method only once, per textbox. Created event-handler will keep
  4. /// track of text changes, and autosize textbox automaticaly.
  5. /// The +6 makes a margin of error. You can customize the number if needed.
  6. /// </summary>
  7. public static void KeepAutoresizingThis(TextBox textbox)
  8. {
  9.     Graphics graphics = textbox.CreateGraphics();
  10.     Size originalSize = textbox.Size;
  11.  
  12.     textbox.TextChanged += new EventHandler(delegate
  13.         {
  14.             int suggestedWidth =
  15.                 (int)graphics.MeasureString(textbox.Text, textbox.Font).Width
  16.                 + 6;
  17.  
  18.             if (suggestedWidth < originalSize.Width)
  19.                 textbox.Size = originalSize;
  20.             else
  21.                 textbox.Width = suggestedWidth;
  22.         });
  23. }

Last edited by ArekBulski : March 13th, 2009 at 03:49 PM. Reason: a note of overriding

Reply With Quote
  #3  
Old March 13th, 2009, 03:57 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
thanks so much for the reply.

I am so sorry. I neglected to mention that the textbox will be of fixed width. So, I need to resize the height. I am currently using Graphics.MeasureString and I do add a bottom padding because Graphics.MeasureString seems to be getting a different height than I need (sometimes a bit smaller, sometimes a bit taller...depending on the actual text).

currently I'm using a method that returns a Size object (from which i get the Height property)
Code:
        Size GetTextSize(TextBox tb)
        {
            // initialize some variables
            Graphics g = tb.CreateGraphics();
            SizeF size = g.MeasureString(tb.Text, tb.Font, tb.Width - 6); // the -6 should account for the diff between the textbox.clientrectangle.width and the textbox.width
            int bottomPadding = 20;
            int desiredHeight = (int)size.Height + 7 + bottomPadding; // the +7 should account for the diff between the textbox.clientrectangle.height and textbox.height
            int heightOfSingleLineOfText = tb.Font.Height;

            // if there are any characters at all...
            if (tb.TextLength > 0)
            {
                // and if the last char is a newline...
                if (tb.Text[tb.TextLength - 1] == '\n')
                {
                    // add an extra line
                    desiredHeight += heightOfSingleLineOfText;
                }
            }

            // cycle through the characters
            for (int i = 0; i < tb.TextLength; i++)
            {
                // if the char is a newline
                // and is preceded by a newline...
                if (tb.Text[i] == '\n' && tb.Text[i - 1] == '\n')
                {
                    // add an extra "line"
                    desiredHeight += heightOfSingleLineOfText;
                }
            }

            size.Height = desiredHeight;
            return new Size((int)size.Width, (int)size.Height);
        }


but this is just not as nice as it could be if i could access the same rectangle that the textbox itself has already calculated and painted the text in.

thanks.

Last edited by WebDunce : March 13th, 2009 at 04:04 PM.

Reply With Quote
  #4  
Old March 13th, 2009, 04:08 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
Also, I definitely thought about deriving a custom textbox control to access the protected properties and methods but i couldn't seem to find a property or method that seemed to do what I need


there might be one....i just couldn't find it.

Thanks again. All suggestions and ideas are definitely appreciated.

Reply With Quote
  #5  
Old March 13th, 2009, 04:25 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
I have found a workaround....

RichTextBox has a ContentsResized event that provides the rectangle I am seeking.

However, I must use a textbox because it is already a heavily customized textbox and the functionality that I added to the textbox I have no intention of spending the time to so customize a RichTextBox object.

But, I can add a RichTextBox to the Form's class (but not add it to the form's controls, see, so it is not visible).

Anytime the textbox's text changes, i will update the richtextbox and anytime the richtextbox fires the ContentsChanged event, i will use that to update the size of the textbox...it works...i tried it already.

Code:
// the form was designed in the designer so the 
// textbox1 object was created in the designer and
// it's code is not shown here
public partial class Form1 : Form
    {
        RichTextBox rtb = new RichTextBox();

        public Form1()
        {
            InitializeComponent();
            rtb.Size = textBox1.Size;            
            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
            rtb.ContentsResized += new ContentsResizedEventHandler(rtb_ContentsResized);
        }

        void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            textBox1.Height = e.NewRectangle.Height + 7; // +7 accounts for some diff between the textbox's clientsize and actual size.
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            rtb.Text = this.textBox1.Text;
        }

Reply With Quote
  #6  
Old March 13th, 2009, 05:20 PM
ArekBulski ArekBulski is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Location: Lodz, Poland
Posts: 31 ArekBulski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 19 m 57 sec
Reputation Power: 5
Excellent solution, but incomplete?

I am impressed. You seem to be very efficient with finding solutions. However I wasn't able to make your code working, without adding few lines. Also it needs to change RichTextBox.ScrollBars, or more text will cause over-grow of that e.NewRectangle. Here is my code, based on your excellent solution:

Code:
public partial class Form1 : Form
    {
        RichTextBox rtb = new RichTextBox();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            rtb.Parent = this;  /// needed to add this!
            rtb.Visible = false;  /// needed as well!
            rtb.Size = textBox1.Size;
            rtb.ScrollBars = RichTextBoxScrollBars.None; /// also this is important!

            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
            rtb.ContentsResized += new ContentsResizedEventHandler(rtb_ContentsResized);
        }

        void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            textBox1.Height = e.NewRectangle.Height + 7;
        }

        void textBox1_TextChanged(object sender, EventArgs e)
        {
            rtb.Text = textBox1.Text;
        }
    }


Would you want some help with putting this into a custom control? So you can add this custom-textbox from Designer Toolbox, without re-writing this code for every new textbox.

Reply With Quote
  #7  
Old March 13th, 2009, 06:58 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
Hey ArekBulski,

Thanks for your code. Your improvements to my code are very good and I will use them.

Reply With Quote
  #8  
Old March 13th, 2009, 07:16 PM
ArekBulski ArekBulski is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Location: Lodz, Poland
Posts: 31 ArekBulski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 19 m 57 sec
Reputation Power: 5
Quote:
Originally Posted by WebDunce
Thanks for your code. Your improvements to my code are very good and I will use them.

Ah, thanks! Me feels appreciated...

So what is your project all about? I am really curious. Besides I think I got too much spare time, so why not to spend some time here, working on some more solutions for you?

Reply With Quote
  #9  
Old March 13th, 2009, 07:23 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
(anyone thinking of using this code should first read post #13)

also, here is my attempt at a custom control. It seems to work good. You may improve it....

Code:
    public partial class AutoSizingTextBox : UserControl
    {
        RichTextBox rtb = new RichTextBox();
        TextBox tb = new TextBox();

        [Browsable(true)]
        public new int Height
        {
            get { return this.tb.Height; }
            set { this.tb.Height = value; }
        }

        [Browsable(true)]
        public new int Width
        {
            get { return this.tb.Width; }
            set { this.tb.Width = value; }
        }

        public AutoSizingTextBox()
        {
            InitializeComponent();
            this.Size = this.tb.Size;

            this.rtb.Location = new Point(0, 0);
            this.rtb.ScrollBars = RichTextBoxScrollBars.None;
            this.rtb.Size = tb.Size;
            this.rtb.ContentsResized += new ContentsResizedEventHandler(rtb_ContentsResized);
            this.Controls.Add(rtb);

            this.tb.Size = this.Size;
            this.tb.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            this.tb.Location = new Point(0, 0);
            this.tb.Multiline = true;
            this.tb.TextChanged += new EventHandler(tb_TextChanged);
            this.tb.SizeChanged += new EventHandler(tb_SizeChanged);
            this.Controls.Add(tb);
            this.tb.BringToFront();
        }       

        void tb_SizeChanged(object sender, EventArgs e)
        {
            if (this.rtb.Width != this.tb.Width)
            {
                this.rtb.Width = this.tb.Width;
                this.rtb.Height = this.tb.Height - 8;
            }

            this.Size = this.tb.Size;
        }

        void tb_TextChanged(object sender, EventArgs e)
        {
            this.rtb.Text = this.tb.Text;
        }

        void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            this.rtb.Height = e.NewRectangle.Height;            
            this.tb.Height = this.rtb.Height + 8;
        }
    }

Last edited by WebDunce : March 18th, 2009 at 10:35 PM.

Reply With Quote
  #10  
Old March 13th, 2009, 07:28 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
Quote:
Originally Posted by ArekBulski
Ah, thanks! Me feels appreciated...

So what is your project all about? I am really curious. Besides I think I got too much spare time, so why not to spend some time here, working on some more solutions for you?


Several years ago, I designed a website for a friend of mine. I built the site in such a way that he cannot easily edit it with Dreamweaver or Frontpage or a Flash editor.

I am trying to create an editor that will let my friend edit the website. An autosizing textbox will be an important part of the editor program (which I have spent years working on... )

Reply With Quote
  #11  
Old March 13th, 2009, 08:26 PM
ArekBulski ArekBulski is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Location: Lodz, Poland
Posts: 31 ArekBulski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 19 m 57 sec
Reputation Power: 5
Quote:
Originally Posted by WebDunce
I am trying to create an editor that will let my friend edit the website. An autosizing textbox will be an important part of the editor program (which I have spent years working on... )

I am even more amazed now. Wish you good luck, and of course I will be here, trying to advise when you need it...

Quote:
also, here is my attempt at a custom control. It seems to work good. You may improve it....


As for this class... it really works! I tried to make a class derived from TextBox, instead from UserControl. It is smaller, but I won't hide that it does not work. wtf

C# Code:
Original - C# Code
  1.  
  2. public class AutoSizingTextBox : TextBox
  3.     {
  4.         RichTextBox rtb = new RichTextBox();
  5.  
  6.         public AutoSizingTextBox()
  7.         {
  8.             this.Multiline = true;
  9.             this.TextChanged += new EventHandler(AutoSizingTextBox_TextChanged);
  10.             this.SizeChanged += new EventHandler(AutoSizingTextBox_SizeChanged);
  11.             //this.FindForm().Controls.Add(this);
  12.  
  13.             rtb.Location = new Point(0, 0);
  14.             rtb.Parent = this.Parent;
  15.             rtb.Visible = false;
  16.             rtb.ScrollBars = RichTextBoxScrollBars.None;
  17.             rtb.Size = this.Size;
  18.             rtb.ContentsResized += new ContentsResizedEventHandler(rtb_ContentsResized);
  19.             //this.FindForm().Controls.Add(rtb);
  20.         }
  21.  
  22.         void AutoSizingTextBox_SizeChanged(object sender, EventArgs e)
  23.         {
  24.             rtb.Size = this.Size;
  25.         }
  26.  
  27.         void AutoSizingTextBox_TextChanged(object sender, EventArgs e)
  28.         {
  29.             rtb.Text = this.Text;
  30.         }
  31.  
  32.         void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)
  33.         {
  34.             rtb.Height = e.NewRectangle.Height;
  35.             this.Height = e.NewRectangle.Height + 8;
  36.         }
  37.     }

Reply With Quote
  #12  
Old March 16th, 2009, 09:53 PM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
just as a note to myself and anyone else who may be trying to create an autosizing textbox...a google search has turned up a bit of code that uses windows messages and centers around the EM_GETLINECOUNT message.

i haven't had a chance to really look into this but it looks promising...

Click here for the code example.

Reply With Quote
  #13  
Old March 17th, 2009, 04:41 AM
WebDunce WebDunce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Northwest Florida
Posts: 208 WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level)WebDunce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
so i rewrote my AutosizeableTextbox based on the code i referenced in the above post that i found in a google search. it uses the SendMessage() method and is much more efficient than the original usercontrol i made by combining a textbox and a richtextbox. You have to link to the user32.dll and use the EM_GETLINECOUNT message

c# Code:
Original - c# Code
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using System.ComponentModel;
  8. using System.Runtime.InteropServices;
  9.  
  10.  
  11. namespace AutoSizeableTextBox
  12. {
  13.     public class AutosizeableTextbox : TextBox
  14.     {
  15.         // link to the SendMessage function in the user32.dll
  16.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  17.         static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
  18.  
  19.         // set up some variables
  20.         const int TEXTBOX_PADDING = 7;
  21.         int minWidth = 10// during instantiation, this is set to the width of a double-u
  22.         int minHeight = 20;
  23.         AutoSizeMode autosizeMode = AutoSizeMode.GrowOnly;
  24.         bool autosize = true;
  25.  
  26.         // set up some useful properties
  27.         public int MinimumWidth
  28.         { get { return this.minWidth; } }
  29.  
  30.         public int MinimumHeight
  31.         { get { return this.minHeight; } }
  32.  
  33.         [Browsable(true), Category("Autosize Properties")]
  34.         public AutoSizeMode AutoSizeMode2 // see note by AutoSize2
  35.         {
  36.             get { return autosizeMode; }
  37.             set
  38.             {
  39.                 autosizeMode = value;
  40.                 ResizeMe();
  41.             }
  42.         }
  43.  
  44.         [Browsable(true), Category("Autosize Properties")]
  45.         public bool AutoSize2 // when i called it AutoSize, whether i used "new" or "override," it would always reset itself to false at runtime.
  46.         {
  47.             get { return autosize; }
  48.             set
  49.             {
  50.                 autosize = value;
  51.                 ResizeMe();
  52.             }
  53.         }
  54.  
  55.  
  56.         // constructor
  57.         public AutosizeableTextbox()
  58.         {
  59.             this.minWidth = TextRenderer.MeasureText("W", this.Font).Width;
  60.             this.minHeight = (int)(this.FontHeight + TEXTBOX_PADDING);
  61.             //this.MinimumSize = new Size(this.MinimumSize.Width, minHeight);
  62.             this.FontChanged += new EventHandler(AutosizeableTextbox_FontChanged);
  63.             this.TextChanged += new EventHandler(AutosizingTextbox_TextChanged);
  64.             this.SizeChanged += new EventHandler(AutosizeableTextbox_SizeChanged);
  65.            
  66.         }
  67.  
  68.         void AutosizeableTextbox_FontChanged(object sender, EventArgs e)
  69.         {
  70.             this.minWidth = TextRenderer.MeasureText("W", this.Font).Width;
  71.             this.minHeight = (int)(this.FontHeight + TEXTBOX_PADDING);
  72.             ResizeMe();
  73.         }
  74.  
  75.         // public methods
  76.         public void ForceResize()
  77.         {
  78.             ResizeMe();
  79.         }
  80.  
  81.         public void ForceResize(int minWidth, int minHeight, int width, int height)
  82.         {
  83.             this.MinimumSize = new Size(minWidth, minHeight);
  84.             this.Size = new Size(width, height);
  85.             ResizeMe();
  86.         }
  87.  
  88.         // event handlers
  89.         void AutosizeableTextbox_SizeChanged(object sender, EventArgs e)
  90.         {
  91.             ResizeMe();
  92.         }
  93.  
  94.         void AutosizingTextbox_TextChanged(object sender, EventArgs e)
  95.         {
  96.             ResizeMe();
  97.         }
  98.  
  99.         // resize method
  100.         void ResizeMe()
  101.         {
  102.             if (this.MinimumSize.Height < this.minHeight)
  103.                 this.MinimumSize = new Size(this.MinimumSize.Width, this.minHeight);
  104.  
  105.             if (this.MinimumSize.Width < this.minWidth)
  106.                 this.MinimumSize = new Size(this.minWidth, this.MinimumSize.Height);
  107.  
  108.             if (autosize)
  109.             {
  110.                 if (this.Multiline)
  111.                     ResizeWithMultilineOn();
  112.                 else
  113.                     ResizeWithMultilineOff();
  114.             }
  115.         }
  116.  
  117.         // resize when multiline is true
  118.         void ResizeWithMultilineOn()
  119.         {
  120.             double lineHeight = this.FontHeight;
  121.             int lineCount = this.GetLineCount();
  122.             int newHeight = (int)(lineCount * lineHeight) + TEXTBOX_PADDING;
  123.  
  124.             if (newHeight < this.Height)
  125.             {
  126.                 if (autosizeMode == AutoSizeMode.GrowAndShrink)
  127.                     this.Height = newHeight;
  128.             }
  129.             else
  130.             {
  131.                 this.Height = newHeight;
  132.             }
  133.         }
  134.  
  135.         // resize when multiline is false
  136.         void ResizeWithMultilineOff()
  137.         {
  138.             if (this.TextLength > 0)
  139.             {
  140.                 int newWidth = TextRenderer.MeasureText(this.Text, this.Font).Width + this.minWidth;
  141.                 if (newWidth < this.Width)
  142.                 {
  143.                     if (autosizeMode == AutoSizeMode.GrowAndShrink)
  144.                     {
  145.                         this.Width = newWidth;
  146.                     }
  147.                 }
  148.                 else
  149.                 {
  150.                     this.Width = newWidth;
  151.                 }
  152.             }
  153.             else
  154.             {
  155.                 if (autosizeMode == AutoSizeMode.GrowAndShrink)
  156.                 {
  157.                     this.Width = this.minWidth; // width of a double-u
  158.                 }
  159.             }
  160.         }
  161.  
  162.         // gets the number of lines in the textbox
  163.         int GetLineCount()
  164.         {
  165.             const Int32 EM_GETLINECOUNT = 186;
  166.             return (int)SendMessage(this.Handle, EM_GETLINECOUNT, IntPtr.Zero, IntPtr.Zero);
  167.         }
  168.     }
  169. }
  170.  


Then you have to multiply the number of lines by the height of the textbox's fontheight and add a little extra for padding etc, but it works just fine.

click here for a demo project using the autosizing textbox (the link will not work after 7 days of no downloads...so some may find the link broken in the future)

Last edited by WebDunce : March 18th, 2009 at 08:32 AM.

Reply With Quote
  #14  
Old March 18th, 2009, 09:43 AM
ArekBulski ArekBulski is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Location: Lodz, Poland
Posts: 31 ArekBulski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 19 m 57 sec
Reputation Power: 5
I tested WebDunce's code and it really works! Congrats!
Thanks for uploading the whole solution too!

Best regards,
Arek Bulski.

Reply With Quote
  #15  
Old January 12th, 2012, 08:05 PM
JBobbins JBobbins is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 1 JBobbins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 4 sec
Reputation Power: 0
Thumbs up Very Nice! Thanks!

I registered with Dev Shed just so I could give kudos to WebDunce. I swapped your control in place of a standard textbox and voila: not a hitch. You may be a dunce at web, but not at desktop app development! Thanks very much for contributing this!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > How can I get the rectangle of the text in a Textbox?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap