Click here to Skip to main content
14,241,236 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.

/// <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.

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


I hope this helps you well.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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 :)
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
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
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
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
Sergey Alexandrovich Kryukov 19-May-11 16:24pm
   
Sorry, I think you lack some understanding. Think what the users need to see, not that what you want. Ask yourself -- why keeping TextBox according to content? You're not really improving user experience following this goal.
--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
mridul samadder 19-May-11 17:05pm
   
! SAK why i try to keep height according to content is that viewers can get to see whole content.so it should not be ur headache.see a example dnn module - (link deleted for some reason).so always try to give solution not trying to know too much detail on it.i wanted to know the solution what i wrote.
Wonde Tadesse 19-May-11 20:47pm
   
Answer updated.
mridul samadder 20-May-11 7:56am
   
thanks wonde for ur updating codes.but there is no Resize method appears for my textbox! in code behind file as u wrote - textBox1.Resize(); is there any assembly need to include?
Wonde Tadesse 20-May-11 8:12am
   
mridul samadder, Resize() is an extension method that I created.How to use it ? Create a class file called ResizeExtension and copy and past the code that I provided in ResizeExtension code block. For more please read http://msdn.microsoft.com/en-us/library/bb383977.aspx
mridul samadder 20-May-11 8:29am
   
but no result.it have some calculation of maximum characters per line which i don't think right coz characters per line varies according to variable width
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
Sergey Alexandrovich Kryukov 20-May-11 23:13pm
   
Listen to a good friendly advice: give up. You won't get perfect solution no matter how hard you try. Either make static text or make it editable but leave some reasonable size; there is nothing wrong with scroll if this is textarea.
Ed Nutting 21-May-11 5:51am
   
Thank you but looking at your sample page it seems that SA may be write, or even Dalek Dave. Those boxes aren't done with text boxes - or if they are that is the wrong thing to use. What you want is a div or p element that contains text and make it look like a box. This would mean you don't have to use any javascript at all :P
   
Thanks for understanding, Ed.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 4

Alternatively use a div, that will change according to content.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100