How to deal with the wrong order of code in InitializeComponent()

alikim

Member
Joined
Oct 30, 2023
Messages
15
Programming Experience
Beginner
This is auto generated code inside InitializeComponent(), it doesn't work because first, pLeft is added to panel before ControlAdded event is added to panel; second - Panel_ControlAdded uses the name of component, which is set after the component is added.

C#:
        //
        // panel
        //
        panel.BackColor = Color.FromArgb(196, 0, 0, 0);
        panel.Controls.Add(pLeft);
        panel.Dock = DockStyle.Bottom;
        panel.Location = new Point(0, 390);
        panel.Name = "panel";
        panel.Size = new Size(400, 210);
        panel.TabIndex = 2;
        panel.ControlAdded += Panel_ControlAdded;
        //
        // pLeft
        //
        pLeft.BackColor = Color.FromArgb(0, 0, 0, 0);
        pLeft.Location = new Point(0, 3);
        pLeft.Name = "pLeft";
        pLeft.Size = new Size(131, 207);
        pLeft.TabIndex = 0;
        pLeft.TabStop = false;

here is the right code, but it will not auto generate this way:

C#:
        //
        // pLeft
        //
        pLeft.BackColor = Color.FromArgb(0, 0, 0, 0);
        pLeft.Location = new Point(0, 3);
        pLeft.Name = "pLeft";
        pLeft.Size = new Size(131, 207);
        pLeft.TabIndex = 0;
        pLeft.TabStop = false;
        //
        // panel
        //
        panel.BackColor = Color.FromArgb(196, 0, 0, 0);
        panel.ControlAdded += Panel_ControlAdded;
        panel.Controls.Add(pLeft);
        panel.Dock = DockStyle.Bottom;
        panel.Location = new Point(0, 390);
        panel.Name = "panel";
        panel.Size = new Size(400, 210);
        panel.TabIndex = 2;

how do I solve this problem?
I don't want to fight auto generation and I want it run as it does but also probably move some assignments into a different method, not sure what's the best way to do this.
 
So what's the actual issue here? What is it that you want to happen that isn't and/or don't want to happen that is? If we know what you want to achieve then we can determine the best way to achieve it. The designer code is generated the way it's generated.
 
The real question is why do you need to have your Panel_ControlAdded() event handler? What is is to doing to the "pLeft" panel that gets added? Why is need to know the name of the control?

If you are truly doing something custom, it's time to switch over to writing a CustomControl instead of trying to simulate one.
 
The real question is why do you need to have your Panel_ControlAdded() event handler? What is is to doing to the "pLeft" panel that gets added? Why is need to know the name of the control?

If you are truly doing something custom, it's time to switch over to writing a CustomControl instead of trying to simulate one.

Apart from pLeft, there will be pRight and pMiddle, the event handler is supposed to set their properties, which are different for each of the three, so I wanted to use their names to ID them.
 
Solution
The designer code doesn't work for the reasons I explained. How do I make it work?

Except you didn't explain. You said that the code is in the "wrong" order. The order it's in works just fine based on everything you've said and shown. Obviously the actual problem is in something you haven't said or shown. Explain the actual problem.
 
Apart from pLeft, there will be pRight and pMiddle, the event handler is supposed to set their properties, which are different for each of the three, so I wanted to use their names to ID them.

Why would you need an event handler to set properties of something that you have added in the designer? Why haven't you set those properties in the designer too? It seems like you are doing something wrong but, as you refuse to provide all the relevant information, we can't determine what that might be.
 

Latest posts

Back
Top Bottom