scrollbars in RichTextBox

U

user

Hello
When i add new lines of text to my RichTextBox, and when vertical scroll
bar appear (there is more text than RichTextBox can display),
RichTextBox always display "the oldest text" (vertical scrollbar is
always rolled to the top).
And i want to see "fresh text" (vertical scrollbar should be rolled to
bottom position).
How can i do that ?


Thanx
 
M

Martin Dew

check out my thread from yesterday titled...

"going to last line of text in a RichTextBox" Mattin Dechev supplied me an answer that works perfectly.
 
U

user

it does not work for me. Program compiles, and run but when text should
be displayed program hangs and thru error:
Additional information: Can not marshal parameter #4: The type
definition of this type has no layout information.


Why ?
 
M

Martin Dew

This is a test app I just wrote in VS.Net 2003 and it works fine for me..

it has one RichTextBox called richTextBox1 and a time that fires an
AppenText() method to the RichTextBox. here is the code of my form.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

namespace Temp1

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.RichTextBox richTextBox1;

private System.Windows.Forms.Timer timer1;

private System.ComponentModel.IContainer components;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.richTextBox1 = new System.Windows.Forms.RichTextBox();

this.timer1 = new System.Windows.Forms.Timer(this.components);

this.SuspendLayout();

//

// richTextBox1

//

this.richTextBox1.Location = new System.Drawing.Point(8, 8);

this.richTextBox1.Name = "richTextBox1";

this.richTextBox1.Size = new System.Drawing.Size(248, 168);

this.richTextBox1.TabIndex = 0;

this.richTextBox1.Text = "richTextBox1";

this.richTextBox1.TextChanged += new
System.EventHandler(this.richTextBox1_TextChanged);

//

// timer1

//

this.timer1.Enabled = true;

this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(264, 190);

this.Controls.Add(this.richTextBox1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void timer1_Tick(object sender, System.EventArgs e) {

richTextBox1.AppendText("Added Line of Text");

}

#region external declarations for making output box always show last line.

const int SB_VERT = 1;

const int EM_SETSCROLLPOS = 0x0400 + 222;

[DllImport("user32", CharSet=CharSet.Auto)]

static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos,

out int lpMaxPos);

[DllImport("user32", CharSet=CharSet.Auto)]

static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, POINT

lParam);

[StructLayout(LayoutKind.Sequential)]

class POINT {

public int x;

public int y;

public POINT() { }

public POINT(int x, int y) {

this.x = x;

this.y = y;

}

}

#endregion

private void richTextBox1_TextChanged(object sender, System.EventArgs e) {

int min, max;

GetScrollRange(richTextBox1.Handle, SB_VERT, out min, out max);

SendMessage(richTextBox1.Handle, EM_SETSCROLLPOS, 0, new POINT(0, max -

richTextBox1.Height));

}

}

}
 
U

user

Thanx a lot.
i did not inlude [StructLayout(LayoutKind.Sequential)]....
Now it works fine :)
 
U

user

by the way:
There is TextChanged event in TextBox class, what is appriopriate event
in ListView class ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top