none
Does a new button control in Form1.cs[Design] add "complete" code to Form1.Designer.cs and Form1.cs? RRS feed

  • Question

  • Good Afternoon,

    After using the VS2008-C# IDE  (Standard Edition) to design a form with one button and then adding my code in Form1.cs to handle the button_click and adding my code to Program.cs to do the "work" (and both sets of code do work and produces expected output to a diskFile); then,

    In Form1.cs[Design] I add a second button to the form by dragging a button-control from the toolbox to Form1.cs[Design]. 
    This action will place "some" code in the Form1.Designer.cs file; however, it does not add the last/final line of code to provide a click event:
         this.btnByHalfHour.Click += new System.EventHandler EventHandler btnByHalfHour_Click btnByHalfHour_Click ClickbtnByHalfHour EventHandlerEventHandlerbtnByHalfHour );

    Also, no code is added to the Form1.cs file tbtnByHalfHourEventArgsbutton object:
         private void btnByHalfHour EventArgs _Click( object sender, EventArgs e )
         {
         }


    We are cautioned in Form1.Designer.cs: "do not modify the contents of this method with the code editor."

    Why, then, is the "click event code line" not added to the Form1.Designer.cs file by the IDE?

    Question(s):
    Is this expected/correct behavior by the IDE?
    If not, do I have the "options" of the IDE incorrectly configured?
    Or is there another explanation?

    Thank you for your time and assistance.
    Best Regards,
    Thomas
    Sunday, July 19, 2009 9:37 PM

Answers

  • By default, when you add a button using the designer, that's all that's done - the button is added to the designer. Any event handlers have to be explicitly added - which is easily achieved via the designer/properties box. You can also just double click on the button and Click being its default designer event, you will get the Click handler generated for you.
    http://blog.voidnish.com
    • Marked as answer by justMe-TLS Monday, July 20, 2009 2:20 AM
    Sunday, July 19, 2009 11:28 PM
    Moderator

All replies

  • By default, when you add a button using the designer, that's all that's done - the button is added to the designer. Any event handlers have to be explicitly added - which is easily achieved via the designer/properties box. You can also just double click on the button and Click being its default designer event, you will get the Click handler generated for you.
    http://blog.voidnish.com
    • Marked as answer by justMe-TLS Monday, July 20, 2009 2:20 AM
    Sunday, July 19, 2009 11:28 PM
    Moderator
  • It is totally consistent with the design of Windows and basic logic that the code to handle an event is not automatically generated. In the beginning of Windows programming, controls such as buttons did nothing until events were handled. Logically, it is not possible to generate an event handler; the IDE does not know what you need to do.
    Sam Hobbs; see my SimpleSamples.Info
    Monday, July 20, 2009 12:03 AM
  • Mr. Hobbs,

    Thank you for your response to my conundrum.  A little history on my part:  I began my career in 'computers' in 1970 submitting 80 column worksheets to be turned into punched cards.  Most of my effort in the next 30 plus years fell into two areas, the first was in developing 'procedure driven' end user business support software.  The second was in developing software for my own "customer support desk" to support our software in the field.  I will be the first to acknowledge that end-users can sometimes be confused, non-responsive to suggestions, and sometimes just plain dumb.  After a number of years away from programming and just recently beginning to do some pro-bono work for nonprofit organizations, I chose the Visual Studio environment to work in.  This has been my first experience in OO programming.

    For myself and perhaps others it is difficult to ask for help on these 'boards' simply because - I/we do not have enough knowledge of VS/.NET OO or any of the other bells-and-whistles that surround the product and the IDE - and lacking that basic knowledge - find it very awkward to form/phrase questions that define/describe the programming problem in front of us.

    I sincerely appreciate your valuable (NO snark intended) time and the historical perspective, however your answer told me only that I was doing something wrong and honestly your comment: "Logically, it is not possible to generate an event handler; the IDE does not know what you need to do." leaves me more confused than before I asked my question.  It was my understanding that the IDE VS "system" "did that stuff for me" .  Were it not for Mr. Sivakumar' svakumar's s response I would still be going in circles.

    Again, am I to heed/obey that admonition to NOT modify the "event handler" code in the Form1.Designer.cs file?  How/where am I supposed to enter the code?

    Again, thank you for your willingness to share your knowledge and I would not have made this "personal" post had I sincerely not wanted to learn how to jump through new (h)OOP(s).

    Best Regards,
    Thomas L Shipley
    Monday, July 20, 2009 3:14 AM