Click here to Skip to main content
15,891,677 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a textbox with changing width in percent.so WIDTH IS NOT SAME.
i want to set height of the textbox according to content.how to do that?
Posted
Updated 14-Dec-17 19:33pm

Set the TextMode property of the TextBox to MultiLine and adjust the width and height accordingly.

I hope this helps you well.

[EDITED]

Ok. I gave you a prototype that will help you to finish up by yourself.But there are certain factors that you should consider. Such as

1.The type of font size, font family..., used in the textbox.
2.Is it underlined ?
3.When calculating the width or height, try not to break the word, and many more factors.

Prototype.

C#
/// <summary>
/// TextBox Resizer extension class
/// </summary>
public static class ResizeExtension
{
    /// <summary>
    /// Resize TextBox according to input value
    /// </summary>
    /// <param name="textBox">TextBox instance</param>
    /// <param name="resizeFactor">ResizeFactor value</param>
    /// <param name="maxCharacterPerLine">Max Character Per line</param>
    public static void Resize(this TextBox textBox, int resizeFactor = 8, int maxCharacterPerLine = 20)
    {
        string textValue = textBox.Text;
        if (string.IsNullOrEmpty(textValue))
            return;

        // Note: It is not completed, It just a prototype
        // ---------------------------------------------
        int width = textValue.Length * resizeFactor; // Calculate width
        int calculateHeight = textValue.Length % maxCharacterPerLine;

        // CalculateHeight should be incremental value

        int height = Convert.ToInt32(textBox.Height.Value) * (calculateHeight > 0 ? calculateHeight : 1);// Assuming that the default height > 0
        // ---------------------------------------------
        // Note: It is not completed, It just a prototype

        textBox.Width = Unit.Pixel(width); // Done
        textBox.Height = Unit.Pixel(height); // Done
    }
}


How to use it? Just call this method in your TextBox instance.E.g if your WebForm contain textbox1 instance then call the Resize method.

C#
protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        textBox1.Resize();
    }


I hope this helps you well.
 
Share this answer
 
v3
Comments
mridul samadder 19-May-11 8:03am    
U did not get wat i meant.already set to multiline.height will not be fixed it will grow according to content.if i set height according to lines(by splitting with \n in code behind file) in textbox then if there are lines which users typed without pressing enter then for those lines scroll bar will appear.i dont want that
Sergey Alexandrovich Kryukov 19-May-11 14:59pm    
Wonde, I voted 5 for the answer. That wasn't your fault that you did not know what OP wanted.
Now I know -- that was bad idea.

Please see my solution.
--SA
Wonde Tadesse 19-May-11 20:39pm    
Thanks --SA.
To do this you need to use JavaScript. You need to handle the resize event of the window and then set the height of your textbox accordingly. Search Google for: 'JavaScript Browser Resize Event' and 'JavaScript Set Textarea Height'. This is a very simple solution - no one is going to do it for you. :P So don't try reposting ;)

Hope this helps,

Ed :)
 
Share this answer
 
If you don't want a scroll bar to appear, it means you don't need TextBox.

Be logical.

If it is a text box, it will be rendered as textarea element to the client side. It only serves the purpose if you allow the user to type in it; so the user call always type enough text to get scroll bar shown. If you need to keep it read-only at all times it means… you don't need a TextBox at all, use static text instead! It may change on the page postbacks but in between it will be static — with no scroll bars.

—SA
 
Share this answer
 
v2
Comments
Ed Nutting 19-May-11 15:35pm    
I think you've missed the point a bit. Please see my answer :P The OP wants to resize a textbox to stop scrollbars. Not sure why he'd want to do this - there's a reason scrollbars are used but anyway... OP wants what OP wants :P
Sergey Alexandrovich Kryukov 19-May-11 16:22pm    
No, I did not miss it. You answer is correct. My point is: the OP's request makes no sense, change the design. "OP wants what OP wants" -- please, how do you know that? You need to see a difference between what one wants and what she or he thinks she or he wants.
We don't know either, but this case looks apparent...
--SA
Ed Nutting 19-May-11 16:25pm    
I know the difference, it was meant sarcastically, suggesting that the OP had no idea what they were actually asking for. I always forget that sarcasm doesn't work through internet forums/texts and other written forms communication :P
Sergey Alexandrovich Kryukov 20-May-11 16:55pm    
Strange, my sarcasm detector did not betrayed me so far even on Internet. Perhaps you need to turn attenuator knob to the right just a bit :-)
--SA
mridul samadder 19-May-11 15:45pm    
@EdMan good understanding
keep in mind this is dnn module so i place it many pages and the width of the textbox varies in contentpane.so i used percentage to fit the textbox width(so the characters in one line also varies).what i want is to make height of my textbox according to content
Alternatively use a div, that will change according to content.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900