4

I have custom descendant of DataGridViewComboBoxColumn with the following value of Items property set in design mode:

    ItemA
    ItemB
    ItemC

During the runtime, I'm changing this Items property like this (example):

for (int i = 0; i < Items.Count; i++) {
    Items(i) = Convert.ToString(Items(i)) + " CHANGED";
}

After I close the form and return to the design mode, I can see property value set to

    ItemA CHANGED
    ItemB CHANGED
    ItemC CHANGED

I wouldn't believe this is possible but this is what I'm getting. After 5 launches, I can see each item suffixed with CHANGED CHANGED CHANGED CHANGED CHANGED.

I'm changing the items only once, in method called from Paint event handler of descendant of DataGridViewComboBoxCell. (There was no better place to hook it, because in constructor, initialization is not finished yet so Items.Count = 0).

miroxlav
  • 11,796
  • 5
  • 58
  • 99

1 Answers1

2

Most likely you have created a new component and used it in the designer.

When you design the component itself, or forms, code in that component or in the form does not execute. Event handlers, method overrides, none of that executes.

However, if you use the component in a designer, like in a form, then code in that component will execute.

Most likely you have code in the component that runs in the designer inside Visual Studio. The fact that it seems that launching the app leaves remnants of its changes behind is probably more a coincidence.

For instance, a typical way to get this to happen, is to override the paint method in the component, and not check if the component is in design mode. Whenever the component needs to be painted, and this includes it being in the designer of a different form or control, then that code will execute.

To check if the component is in design mode you should do this:

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);

See also this question for more details: Detecting design mode from a Control's constructor.

Community
  • 1
  • 1
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825