locked
Form designer keeps removing the ItemChecked event handler for ListView controls RRS feed

  • Question

  • Hi,

    I manually add an event handler for ItemChecked for ListView controls on my form in the form's designer text file, however for some reason it automatically gets removed now and then.

    Example: In FormMain.Designer.cs, I add this to the bottom of the properties declarations for "lstGroupsFiles":

    this.lstGroupsFiles.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.lstMapsGroups_ItemChecked);
    
    This allows me to run some code when an item is checked/unchecked in the list.

    Is there a way to prevent Visual Studio from automatically removing such manual event handlers? I sometimes forget they are removed until too late.


    Thanks, Adam Reece.
    Saturday, February 19, 2011 11:20 PM

Answers

  • Microsoft strictly say not to edit designer.cs file manually. It runs serialization/deserialization while designer is loaded. I suggest to create the handler by double clicking the event from properties window.

    There is another option I frequently use when creating event handlers, Create the handler method lstMapsGroups_ItemChecked in code file (not in designer.cs file. Go to designer, select the event, you'll see a combobox and will see the method that you added in the selection list (if it matches the signatures of the handler). Select the method from combobox and save the form. This should retain the code in designer.cs.

     

    Hope this helps.


    Please mark this post as answer if it solved your problem.
    • Proposed as answer by Matthew Watson Monday, February 21, 2011 9:56 AM
    • Marked as answer by Adambean Tuesday, February 22, 2011 10:55 AM
    Monday, February 21, 2011 9:53 AM
  • Hi Adam,

    Base on my experience, you may write a method to collect the all declared event handlers and execute the method in the Load function.

     private void Form1_Load(object sender, EventArgs e)
    {
      EventHandleCollection();
    }
    private void EventHandleCollection()
    {
      this.lstGroupsFiles.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandle(this.lstMapsGroups_ItemChecked);

      //add more Handles
    }
     

    Best Regards,


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

     

    • Marked as answer by Adambean Tuesday, February 22, 2011 10:55 AM
    Tuesday, February 22, 2011 8:14 AM

All replies

  • It would surely help if you would provide us some code you have there. 

    thx

    Mitja

    Sunday, February 20, 2011 12:40 AM
  • Microsoft strictly say not to edit designer.cs file manually. It runs serialization/deserialization while designer is loaded. I suggest to create the handler by double clicking the event from properties window.

    There is another option I frequently use when creating event handlers, Create the handler method lstMapsGroups_ItemChecked in code file (not in designer.cs file. Go to designer, select the event, you'll see a combobox and will see the method that you added in the selection list (if it matches the signatures of the handler). Select the method from combobox and save the form. This should retain the code in designer.cs.

     

    Hope this helps.


    Please mark this post as answer if it solved your problem.
    • Proposed as answer by Matthew Watson Monday, February 21, 2011 9:56 AM
    • Marked as answer by Adambean Tuesday, February 22, 2011 10:55 AM
    Monday, February 21, 2011 9:53 AM
  • That sounds like a good solution prakash_jda, I'll give this ago.

    As a workaround I put the += event handler in the form loading event, though I'd certainly rather the event handler be declared in the designer :)


    Thanks, Adam Reece.
    Monday, February 21, 2011 10:56 AM
  • Hi Adam,

    Base on my experience, you may write a method to collect the all declared event handlers and execute the method in the Load function.

     private void Form1_Load(object sender, EventArgs e)
    {
      EventHandleCollection();
    }
    private void EventHandleCollection()
    {
      this.lstGroupsFiles.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandle(this.lstMapsGroups_ItemChecked);

      //add more Handles
    }
     

    Best Regards,


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

     

    • Marked as answer by Adambean Tuesday, February 22, 2011 10:55 AM
    Tuesday, February 22, 2011 8:14 AM