Results 1 to 8 of 8

Thread: UC how to set default property only for the first time

  1. #1

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    135

    Question UC how to set default property only for the first time

    Hi guys! Im doing a User Control that can display its Icons in Black or in White. I created a property for it and on the constructor I set the default value to White (so from the start it loads the icons otherwise nothing will be visible). If I put the UC on a Form and change the propery to black and run the code it just run fine. Values are saved, icons are shown in black. Seems ok but here is the problem...

    On execution when the form create my UC it enters the constructor, and set the icons white, so it loads all the white images... then the form set the property to black, so now it loads all the black images... The images are loadad twice.

    Is there a way to set propertys values that only run when placing the UC on a form and never more? (like Initialize properties in VB6)


    Im doing it on C# but I guess its the same for all .Net languages.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,573

    Re: UC how to set default property only for the first time

    You have posted in the CodeBank forum, which is for sharing working code snippets. I have asked the mods to move this thread to the VB.NET forum, which is for asking question about VB.NET code.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,573

    Re: UC how to set default property only for the first time

    Quote Originally Posted by shagratt View Post
    Im doing it on C# but I guess its the same for all .Net languages.
    I didn't read this until after asking the mods to move the thread to the VB.NET forum. Now I find that it actually needs to be moved to the C# forum. If you actually think a .NET question is language-agnostic then there's a .NET Architecture & Design for that. VB.NET forums are for VB.NET questions, not for people to put any .NET questions because they want to get the most views.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,573

    Re: UC how to set default property only for the first time

    As for the issue, can you show us the code for the property and the relevant part(s) of the constructor? Some testing will likely be required so we should be able to test with as close to your actual code as possible.

  5. #5

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    135

    Re: UC how to set default property only for the first time

    Quote Originally Posted by jmcilhinney View Post
    I didn't read this until after asking the mods to move the thread to the VB.NET forum. Now I find that it actually needs to be moved to the C# forum. If you actually think a .NET question is language-agnostic then there's a .NET Architecture & Design for that. VB.NET forums are for VB.NET questions, not for people to put any .NET questions because they want to get the most views.
    I apology. I got several windows open trying to find the solution and did not realize I was on the wrong section of the forum before posting. Is not that I wanted to get most views like you imply.

  6. #6

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    135

    Re: UC how to set default property only for the first time

    Quote Originally Posted by jmcilhinney View Post
    As for the issue, can you show us the code for the property and the relevant part(s) of the constructor? Some testing will likely be required so we should be able to test with as close to your actual code as possible.
    I put in Bold the part of the code I want only to run on initialization (when dragging the control on the form).
    Right now it has 2 problems. Load the icons twice and if the user change the docking to none it gets overwritten to dock on top again.


    Code:
        public partial class ucWindowButtons : UserControl {
    
    
            private static int mIconPadding;
            private static eIconColor mIconColors;
    
    
            //Constructor
            public ucWindowButtons() {
                InitializeComponent();
    
    
                //initial default values 
    //
    // THIS IS THE CODE I WANT TO RUN ONLY WHEN PUT ON THE FORM AND NEVER RUN AGAIN
    //
                IconColors = eIconColor.Blancos; //Set White icons as default
                this.Dock = DockStyle.Top;
                IconPadding = 15;
            }
            
            public enum eIconColor {
                Blancos=0,  // White
                Negros=1    // Black
            }
    
            /// <summary>
            /// Load the icon set for the selected color
            /// </summary>
            public eIconColor IconColors {
                get { return mIconColors; }
                set {
                    mIconColors = value;
                    if (value == eIconColor.Blancos) {
                        btnClose.Image = ResWindows.WinCloseW.ToBitmap();
                        btnMax.Image = ResWindows.WinMaxW.ToBitmap();
                        btnRes.Image = ResWindows.WinRestW.ToBitmap();
                        btnMin.Image = ResWindows.WinMinW.ToBitmap();
                    } else {
                        btnClose.Image = ResWindows.WinClose.ToBitmap();
                        btnMax.Image = ResWindows.WinMax.ToBitmap();
                        btnRes.Image = ResWindows.WinRest.ToBitmap();
                        btnMin.Image = ResWindows.WinMin.ToBitmap();
                    }
                }
            }
    
    
            public int IconPadding {
                get { return mIconPadding; }
                set {
                    mIconPadding = value;
                    ReCalc();
                }
            }
    
    
            /// <summary>
            /// Recalculate button position and container
            /// </summary>
            private void ReCalc() {
                int PadLateral = 8;
                int w = PadLateral; //Pad Right inicial
    
    
                btnClose.Left = pnlBotones.Width - (btnClose.Width + w);
                w += btnClose.Width + mIconPadding;
    
    
                btnMax.Left = pnlBotones.Width - (btnMax.Width + w);
                w += btnMax.Width + mIconPadding;
    
    
                btnMin.Left = pnlBotones.Width - (btnMin.Width + w);
                w += btnMin.Width + PadLateral;
    
    
                //Adjust width of the button container
                pnlBotones.Width = w;
                pnlBotones.Left = pnlBotones.Parent.Width - w;
    
    
            }
    
    }

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,573

    Re: UC how to set default property only for the first time

    This appears relevant:

    https://stackoverflow.com/questions/...ls-constructor

    For the record, that was the first result in a Bing search for "c# detect when control is in designer".

  8. #8

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    135

    Re: UC how to set default property only for the first time

    Quote Originally Posted by jmcilhinney View Post
    This appears relevant:

    https://stackoverflow.com/questions/...ls-constructor

    For the record, that was the first result in a Bing search for "c# detect when control is in designer".
    Read it and doesnt seem to solve my problem.
    I cannot detect the first time the control is on the form with this because when the constructor run the instance properties (saved on the form) are still not applied so I dont know if I need to apply the default ones and load the white icon set or the black icon set. Also "when CustomControl is located in a Form in the designer, this CustomControl is running in runtime mode"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width