TextBox auto scroll to end when text is added

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

textBox += someData + "\r\n";
does not scroll the visible text to the end. How do I perform that ?

Also this way seems to me a lot of reallocating memory. Is there better way
to add text to a textbox ?
 
Howdy just came accross this myself
if you use appendtext this should do the trick, as long as the textbox has
focus,
here is my solution, i used a richtextbox because i wanted to use some of
it's additional reatures, but basically i send the richtextbox a scroll
message,


usage
/// <summary>
/// Adds some status information
/// </summary>
/// <param name="strInfo"></param>
public void AddStatusInfo(string strInfo)
{
tbStatus.AppendText(strInfo);
ScrollTextBoxEnd(tbStatus);
}

#region Native

/// <summary>
/// Scrolls the textbox to the end
/// </summary>
/// <param name="tb"></param>
private void ScrollTextBoxEnd(RichTextBox tb)
{
const int WM_VSCROLL = 277;
const int SB_BOTTOM = 7;

IntPtr ptrWparam = new IntPtr(SB_BOTTOM);
IntPtr ptrLparam = new IntPtr(0);
SendMessage(tb.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
}


[DllImport("User32.dll", CharSet=CharSet.Auto, EntryPoint="SendMessage")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
#endregion

#endregion //Private
 
Howdy just came accross this myself
if you use appendtext this should do the trick, as long as the textbox has
focus,
here is my solution, i used a richtextbox because i wanted to use some of
it's additional reatures, but basically i send the richtextbox a scroll
message,


usage
/// <summary>
/// Adds some status information
/// </summary>
/// <param name="strInfo"></param>
public void AddStatusInfo(string strInfo)
{
tbStatus.AppendText(strInfo);
ScrollTextBoxEnd(tbStatus);
}

#region Native

/// <summary>
/// Scrolls the textbox to the end
/// </summary>
/// <param name="tb"></param>
private void ScrollTextBoxEnd(RichTextBox tb)
{
const int WM_VSCROLL = 277;
const int SB_BOTTOM = 7;

IntPtr ptrWparam = new IntPtr(SB_BOTTOM);
IntPtr ptrLparam = new IntPtr(0);
SendMessage(tb.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
}


[DllImport("User32.dll", CharSet=CharSet.Auto, EntryPoint="SendMessage")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
#endregion

#endregion //Private
 
Howdy just came accross this myself
if you use appendtext this should do the trick, as long as the textbox has
focus,
here is my solution, i used a richtextbox because i wanted to use some of
it's additional reatures, but basically i send the richtextbox a scroll
message,


usage
/// <summary>
/// Adds some status information
/// </summary>
/// <param name="strInfo"></param>
public void AddStatusInfo(string strInfo)
{
tbStatus.AppendText(strInfo);
ScrollTextBoxEnd(tbStatus);
}

#region Native

/// <summary>
/// Scrolls the textbox to the end
/// </summary>
/// <param name="tb"></param>
private void ScrollTextBoxEnd(RichTextBox tb)
{
const int WM_VSCROLL = 277;
const int SB_BOTTOM = 7;

IntPtr ptrWparam = new IntPtr(SB_BOTTOM);
IntPtr ptrLparam = new IntPtr(0);
SendMessage(tb.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
}


[DllImport("User32.dll", CharSet=CharSet.Auto, EntryPoint="SendMessage")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
#endregion

#endregion //Private
 
Howdy just came accross this myself
if you use appendtext this should do the trick, as long as the textbox has
focus,
here is my solution, i used a richtextbox because i wanted to use some of
it's additional reatures, but basically i send the richtextbox a scroll
message,


usage
/// <summary>
/// Adds some status information
/// </summary>
/// <param name="strInfo"></param>
public void AddStatusInfo(string strInfo)
{
tbStatus.AppendText(strInfo);
ScrollTextBoxEnd(tbStatus);
}

#region Native

/// <summary>
/// Scrolls the textbox to the end
/// </summary>
/// <param name="tb"></param>
private void ScrollTextBoxEnd(RichTextBox tb)
{
const int WM_VSCROLL = 277;
const int SB_BOTTOM = 7;

IntPtr ptrWparam = new IntPtr(SB_BOTTOM);
IntPtr ptrLparam = new IntPtr(0);
SendMessage(tb.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
}


[DllImport("User32.dll", CharSet=CharSet.Auto, EntryPoint="SendMessage")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
#endregion

#endregion //Private
 
Back
Top