First:
Richtextbox1.scrollbars = richtextboxscrollbars. None;
RichTextBox. contentsresized + = new contentsresizedeventhandler (richtextbox_contentsresized );
Private void richtextbox+contentsresized (Object sender, contentsresizedeventargs E)
{
Richtextbox1.height = E. newrectangle. height + 10;
}
Second:
1. Call the following method first:
[Dllimport ("user32.dll", entrypoint = "sendmessagea")]
Private Static extern int sendmessage (intptr hwnd, int wmsg, intptr wparam, string lparam );
2. Set RichTextBox:
This. richtextbox1 = new RichTextBox ();
This. richtextbox1.text = "contentcontentcontentcontentcontentcontentcontentcontentcontent ";
This. richtextbox1.width = This. ppanel. Width-15;
This. richtextbox1.scrollbars = richtextboxscrollbars. None;
This. richtextbox1.location = new point (0, 0 + this. lab1.height + 10 );
// This. richtextbox1.anchor = (anchorstyles. Right | anchorstyles. Left | anchorstyles. Top); this code has a problem.
// Obtain the RichTextBox height.
Int em_getlinecount = 0x00ba; // gets the message number of the total number of rows.
Int lc = sendmessage (this. richtextbox1.handle, em_getlinecount, intptr. Zero ,"");
Int Sf = This. richtextbox1.font. Height * (Lc + 1) + this. richtextbox1.location. Y;
This. richtextbox1.height = SF;
This. richtextbox1.resize + = new eventhandler (richtextbox+resize );
This. Controls. Add (this. richtextbox1 );
3. Set the resize of RichTextBox:
Void richtextbox1_resize (Object sender, eventargs E)
{
Int em_getlinecount = 0x00ba; // gets the message number of the total number of rows.
Int lc = sendmessage (this. richtextbox1.handle, em_getlinecount, intptr. Zero ,"");
Int Sf = This. richtextbox1.font. Height * (Lc + 1) + this. richtextbox1.location. Y;
This. richtextbox1.height = SF;
}