Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I change the properties of a Control after the resource file has been applied?

I need to add a scroll bar for a component when a user changes their font size to 125% or 150%. To do this I added a method in the component, which sets the AutoScroll property to true.

protected override void OnSizeChanged(EventArgs e)
{
    if (SystemFonts.DefaultFont.Size < 8)
    {
        this.AutoScroll = true;
    }
    if (this.Handle != null)
    {
        this.BeginInvoke((MethodInvoker) delegate
        {
            base.OnSizeChanged(e);
        });
    }
}

This works well, but one of the components should not get a scrollbar.

The above method will be triggered when initializing the controllers like this:

this.ultraExpandableGroupBoxPanel1.Controls.Add(this.pnlViewMode);
this.ultraExpandableGroupBoxPanel1.Controls.Add(this.ucASNSearchCriteria);
resources.ApplyResources(this.ultraExpandableGroupBoxPanel1, "ultraExpandableGroupBoxPanel1");
this.ultraExpandableGroupBoxPanel1.Name = "ultraExpandableGroupBoxPanel1";

The method will be triggered when adding into Controls and after this, the resource will be applied. The component which I don't want to change belongs to ucASNSearchCriteria in above code.

Now I want to set the AutoScroll property of 'ucASNSearchCriteria' to false after the resource has been applied. I have little knowledge about the rendering process of c# ui controls. Is it possible to dynamically change properties after applying?

like image 250
michael wu Avatar asked Nov 01 '22 02:11

michael wu


1 Answers

I'd create a derived control of the desired type and add a property AllowAutoScroll or anything like that with the default value true.

With this, you can change that property in the WinForms designer easily and react on that property as the size gets changed.

So the designer will add this line of code for you if you change it to be non-default (false):

this.ucASNSearchCriteria.AllowAutoScroll = false;

... and you can react on that new property like this:

protected override void OnSizeChanged(EventArgs e)
{
    if (AllowAutoScroll)
    {
        if (SystemFonts.DefaultFont.Size < 8)
        {
            this.AutoScroll = true;
        }
        if (this.Handle != null)
        {
            this.BeginInvoke((MethodInvoker) delegate
            {
                base.OnSizeChanged(e);
            });
        }
    }
}
like image 167
Waescher Avatar answered Nov 15 '22 03:11

Waescher