How to render a multiline text box so that it displays the last line of text ?

I am a rookie in , C# and using Visual Studio 2008 . In my program I am using a multi-line text box to display a chat conversation . Its working fine. But when the text in it overruns the size of the textbox set at design time , the textbox is rendered (on click of a button) such that it displays only the first few lines of textbox . And i have to scroll down to see the latest line in the conversation. Is there any way that when i press the button(send) the textbox opens such that it is displays the last lines of the text and i dont have to scroll down ?

Is it possible or would i have to make a custom control?
LVL 59
Saurabh Singh TeotiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use Javascript to do that.
<script type="text/javascript">
function scrollTextBoxDown(elementId)
 var textRange = document.getElementById(elementId).createTextRange();

Open in new window

Saurabh Singh TeotiaAuthor Commented:
Pardon my ignorance but where do i put this code in the .cs file or the html source file ? I am  using a button(Button1) and a textbox(textbox 1) with its type set to multiline  . So should i put this in the Button1_Click function in the .cs file. Besides how do I identify the Textbox in this code : by the element id ?

Also i think my earlier question was a bit unclear . I need to display a chat conversatio n in a multiline textbox. So whenver the textbox is rendered/refreshed i want the cursor to be at the last line of text box , so that i dont have to scroll down to see the latest message.

In your page add this JS function

<script type="text/javascript">
        function scrollTextBoxDown(elementId) {
            var textRange = elementId.createTextRange();

On your <form> tag, add the "defaultfocus" property and set it to your textbox id
<form defaultfocus="Textbox_Where_Your_Chat_Conversation_Is" id="form1" runat="server">

On code-behing, add this line to your page_load
Textbox_Where_Your_Chat_Conversation_Is.Attributes.Add("onfocus", "scrollTextBoxDown(" + Textbox_Where_Your_Chat_Conversation_Is.UniqueID + ");");

Saurabh Singh TeotiaAuthor Commented:
sorry but it didnt work . i am uploading the files here so perhaps you can pinpoint where the fault lies. I am using the ajax controls provided by the visual studio 2008 .
thanks .
I've change your <form> tag to <form defaultfocus="TextBox1" id="form1" runat="server">

And i add TextBox1.Attributes.Add("onfocus", "scrollTextBoxDown(" + TextBox1.UniqueID + ");"); to your Page_Load and it works well.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.