There are 2 classes Beauty_VScrollBar.cs and RTB.cs.
From the first class the name of the element, converted to RichTextBox, is passed. The second class binds event handlers to the received name, performs calculations and writes the value to the variable Maximum. The resulting value is passed back to the first class.
Problem: The value is calculated only once. When you try to change the dimensions of a component (internal or external), the calculation is not done again.
Beauty_VScrollBar.cs
private Control bindingContainer;
public Control BindingContainer {
get { return bindingContainer; }
set {
bindingContainer = value;
Updates(); Invalidate();
}
}
private void Updates() {
if (bindingContainer is RichTextBox) {
RTB.Name = (RichTextBox)bindingContainer;
RTB.BindEventHandlers(); Maximum = RTB.Maximum;
} else if (bindingContainer is Panel) {
PNL.Name = (Panel)bindingContainer;
PNL.BindEventHandlers(); Maximum = PNL.Maximum;
} else {
bindingContainer = null;
MessageBox.Show("The selected item is not a RichTextBox");
}
}
RTB.cs
public static class RTB {
public static RichTextBox Name { get; set; }
public static int Maximum { get; private set; }
private static int heightContentRtb;
private static int heightContainerRtb;
public static void BindEventHandlers() {
Name.Resize += (s, e) => SetMaxVScroll();
Name.ContentsResized += (s, e) => {
heightContentRtb = e.NewRectangle.Height; SetMaxVScroll();
};
}
private static void SetMaxVScroll() {
heightContainerRtb = Name.ClientSize.Height;
Maximum = Math.Abs(heightContentRtb - heightContainerRtb + 1);
}
}
If you do everything in one class - everything works, but it is not convenient to update.
Note: In addition to RichTextBox, other types will be used, so we need transformations (... as RichTextBox).
private RichTextBox targetCtrl;
public RichTextBox TargetCtrl {
get { return targetCtrl; }
set {
targetCtrl = value;
Updates(); Invalidate();
}
}
private void Updates() {
if (targetCtrl != null) targetCtrl.Resize += TargetCtrl_Resize;
if (targetCtrl != null) targetCtrl.ContentsResized += TargetCtrl_ContentsResized;
}
int heightContentRtb;
private void TargetCtrl_Resize(object sender, EventArgs e) {
SetMaxVScroll();
}
private void TargetCtrl_ContentsResized(object sender, ContentsResizedEventArgs e) {
heightContentRtb = e.NewRectangle.Height; SetMaxVScroll();
}
private void SetMaxVScroll() {
int heightContainerRtb = targetCtrl.ClientSize.Height;
Maximum = Math.Abs(heightContentRtb - heightContainerRtb + 1);
}