Search code examples
c#winformsconstructorpropertiesonload

Property values defined in Property Window stll have their default values in OnLoad and constructor of my UserControl


I would like to be able to use the properties I set in the Properties Window of Visual Studio in my UserControl, but I get the default value of the property back, such as 0 or null. I tried this in the constructor and the Onload of my UserControl in C#.

SGscrollBoxSyncFusionThumbnailsBase is based on a UserControl
I tried this:

public abstract class SGscrollBoxSyncFusionThumbnailsBase : SGscrollBoxSyncFusion
{
    protected SGscrollBoxSyncFusionThumbnailsBase()
    {
        MessageBox.Show(Size.ToString());
    }

and this

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    MessageBox.Show(Size.ToString());
}

But this both returns (Width=0, Height =0) and it should be (Width=252, Height =126)

When I try this in my Form:

private void Form1_Load(object sender, EventArgs e)
{
    MessageBox.Show(sgScrollBoxSyncFusionThumbnailsPhotoRoll.Size.ToString());            
}

this returns (Width=252, Height =126) and that is what I want.

How do I get the expected result with code inside of my UserControl?
I use C# with WinForms.

Edit: You can think of the classes as regular UserControls. SyncFusion means that there are SyncFusion scroll bars on the controls.

SGscrollBoxSyncFusion is a custom control based on a UserControl. The derived class is an SGscrollBoxSyncFusionThumbnailsBase which is abstract. The class sgScrollBoxSyncFusionThumbnailsPhotoRoll is derived from this class and has a public constructor.

This is the class definition:

public class SGscrollBoxSyncFusionThumbnailsPhotoRoll : SGscrollBoxSyncFusionThumbnailsBase 

This is the constructor:

public SGscrollBoxSyncFusionThumbnailsPhotoRoll() 
{ ...... }

Edit 2: To Simplify my problem I have created a UserControl class from scratch.

namespace UserControls
{
    public partial class UserControl1 : UserControl
    {
        public int Number { get; set; }
        public UserControl1()
        {
            InitializeComponent();
            MessageBox.Show(Number.ToString());
        }
    }
}

I placed this control on my form and set the Number property in the Properties Window of the form designer to 100. Still the result is 0. How is this possible?


Solution

  • Thank you Jimi. That was the answer I need. It works now!

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        MessageBox.Show(Number.ToString()); //Gives 100 and that is correct
    }
    

    In my question I used OnLoad and it didn't work while OnLoad is called later than OnhandleCreated. The reason I guess is that de Size was set in the baseclass to 0. Thanks for the help.

    Edit:

    The form designer in my real project with the SGscrollBoxSyncFusionThumbnailsPhotoRoll class puts the add of the control before the setting of the properties of the control. And that was a reason it didn't work.

    This is code from InitializeComponent:

    this.tabPageAdvPhotoRoll.Controls.Add(this.sgScrollBoxSyncFusionPictureBoxForPhotoRoll);
    ...
    this.sgScrollBoxSyncFusionThumbnailsPhotoRoll.LinkedZoomedImageScrollBoxSyncFusionPictureBox = this.sgScrollBoxSyncFusionPictureBoxForPhotoRoll;
    

    I switched this to:

    this.sgScrollBoxSyncFusionThumbnailsPhotoRoll.LinkedZoomedImageScrollBoxSyncFusionPictureBox = this.sgScrollBoxSyncFusionPictureBoxForPhotoRoll;
    ...
    this.tabPageAdvPhotoRoll.Controls.Add(this.sgScrollBoxSyncFusionPictureBoxForPhotoRoll);
    

    And now it works. The Add method triggers the OnHandleCreated EventHandler. In the first situation the LinkedZoomedImageScrollBoxSyncFusionPictureBox property was not set when OnHandleCreated was triggered.

    In the second situation it is set before the OnHandleCreated was triggered.

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        if (LinkedZoomedImageScrollBoxSyncFusionPictureBox != null)
        {
            AddSubComponents(new Size(200, 200), 3, LinkedZoomedImageScrollBoxSyncFusionPictureBox, "G:\\Path\\To\\Images");
        }
    }