Tatami
Tatami

Reputation: 105

Transferring a value to another class, changing it and returning it back to the main class

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);
}

Upvotes: 2

Views: 77

Answers (1)

golakwer
golakwer

Reputation: 373

If you are reusing RTB for multiple controls then you are clobbering (overwriting) the value of Name each time. The event handler still gets registered to each but when the event handler runs it is in the context of whatever the current value of Name is because Name is static.

If you dont like the 2nd version that you've listed and want you code closer together you could create a closure to handle the event assignment and Maximum calculation. A closure would maintain separate state each time it is called. However your 2nd version seems alright to me.

pubilc void setup(RichTextBox name){
     int heightContentRtb = 0;
     int heightContainerRtb = 0;

     void SetMaxVScroll() {
        heightContainerRtb = Name.ClientSize.Height;
        Maximum = Math.Abs(heightContentRtb - heightContainerRtb + 1);
    }

    name.Resize += (s, e) => SetMaxVScroll();
    Name.ContentsResized += (s, e) => {
        heightContentRtb = e.NewRectangle.Height;
        SetMaxVScroll();
    };
}

Note: It is not clear to me what the purpose of Maximum is or where it should live. And in the above code it would get overwritten each time a particular RichTextBox control is resized.

Upvotes: 1

Related Questions