Adding Scroll Bars at Run-Time

From RAD Studio
Jump to: navigation, search

Go Up to Working with Text in Controls


Rich edit and memo components can contain horizontal or vertical scroll bars, or both, as needed. When word wrapping is enabled, the component needs only a vertical scroll bar. If the user turns off word wrapping, the component might also need a horizontal scroll bar, since text is not limited by the right side of the editor.

To add scroll bars at run time

  1. Determine whether the text might exceed the right margin. In most cases, this means checking whether word wrapping is enabled. You might also check whether any text lines actually exceed the width of the control.
  2. Set the rich edit or memo component's ScrollBars property to include or exclude scroll bars.

The following example attaches an OnClick event handler to a Character > WordWrap menu item:

 procedure TForm.WordWrap1Click(Sender: TObject);
 begin
   with Editor do
   begin
     WordWrap := not WordWrap;  { toggle word wrapping }
     if WordWrap then
       ScrollBars := ssVertical  { wrapped requires only vertical }
     else
       ScrollBars := ssBoth;  { unwrapped might need both }
       WordWrap1.Checked := WordWrap;  { check menu item to match property }
   end;
 end;
void __fastcall TEditForm::WordWrap1Click(TObject *Sender) {
	Editor->WordWrap = !(Editor->WordWrap); // toggle word wrapping
	if (Editor->WordWrap)
		Editor->ScrollBars = ssVertical; // wrapped requires only vertical
	else
		Editor->ScrollBars = ssBoth; // unwrapped can need both
	WordWrap1->Checked = Editor->WordWrap; // check menu item to match property
}

The rich edit and memo components handle their scroll bars in a slightly different way. The rich edit component can hide its scroll bars if the text fits inside the bounds of the component. The memo always shows scroll bars if they are enabled.

See Also