Results 1 to 10 of 10

Thread: My first c# error

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    My first c# error

    I don't know what to select in Prefix ComboBox.

    I tried to start work in C#.

    I just added a button on a Windows Form and decided to work on something in load event of the form. I double clicked on form and get the load event. Then i quit to write something in load event and removed the load event code and added a new button and placed the following code on it.

    Code:
      private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("My First Message");
            }
    Now the button1_click is underline with an error i.e.
    Name button1_click does not match rule method, properties and events. Suggested name is Button1Click
    "My First Message" also underlined with a message that:
    Localizable String " My First Message "
    Note: I'm also using JetBrain ReSharper with VS 2010.

    Now When I run the project so it gives an error message that:

    Error 1 'WindowsFormsApplication1.Form1' does not contain a definition for 'Form1_Load' and no extension method 'Form1_Load' accepting a first argument of type 'WindowsFormsApplication1.Form1' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Abid Durrani\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs 52 55 WindowsFormsApplication1
    Please tell me that how to remove this error.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: My first c# error

    For the last error, I would double click on the error message, and remove the line that is high-lighted, which is trying to access the Form_Load handler that no longer exists.

    Maybe that will fix the first errors, I don't know.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: My first c# error

    The first two are just warnings that you can basically ignore if you want.

    1. ReSharper's default rules for method names are that it should start with an upper-case letter and not contain underscores. By default, creating an event handler from the designer will prefix the method name with the component name and separate that from the event name with an underscore. If you don't want to see those warnings then you must do one of two things:

    A. Change the method name to conform to ReSharper's rules.
    B. Change ReSharper's rules to allow that format.

    If you click on the method name and then click on the icon in the margin, you should see items for both those options. If you choose B then you can add multiple formats so that something like Button1Click is the default but button1_Click is allowed.

    2. ReSharper will always warn you when you use a String literal that it is localisable. You only need to take notice if you want to localise your app, in which case you must extract the literal into a resource and use a different resource for each supported culture.

    3. That error message can't be ignored. It's important to realise that event handlers are handled differently in C# to VB. In VB, the method itself includes the Handles clause and that's what registers it as an event handler. Delete the method and you delete the Handles cluase so you delete the registration. In C#, the registration is separate to the method so deleting the method does not delete the registration, in which case you have a line of code referring to a method that does exist. To fix it, you can go into the designer code file and delete the line manually. It will look something like this:
    Code:
    someComponent.SomeEvent += new EventHandler(SomeMethod)
    Alternatively, you can use the Properties window. Just as in VB, there is an Events button that shows the selected components events instead of its properties. There you can clear the handler for an event and that will automatically delete the corresponding line of code.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: My first c# error

    John. Is it necessary to use ReSharper with C#? I mean i was suggested by one of my friend to ReShaper with C#. But he is very old in C#. So what you advise me as I'm baby to this language.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: My first c# error

    Quote Originally Posted by ADQUSIT View Post
    John. Is it necessary to use ReSharper with C#? I mean i was suggested by one of my friend to ReShaper with C#. But he is very old in C#. So what you advise me as I'm baby to this language.
    It's certainly not "necessary" to use ReSharper and the vast majority of developers don't. It does provide lots of useful functionality though, so you can certainly benefit from doing so.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: My first c# error

    When i double click on the error the control took me here:

    Name:  Error1.jpg
Views: 480
Size:  50.8 KB

    Should i remove the entire form1 code block? Please guide me.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: My first c# error

    Just remove the one line. You don't have an event handler named Form1_Load any longer.

    Quote Originally Posted by passel View Post
    For the last error, I would double click on the error message, and remove the line that is high-lighted, which is trying to access the Form_Load handler that no longer exists.
    ....
    Quote Originally Posted by jmcilhinney View Post
    ... delete the line manually. It will look something like this:
    Code:
    someComponent.SomeEvent += new EventHandler(SomeMethod)
    ....
    jmc also mentioned
    "Alternatively, you can use the Properties window. Just as in VB, there is an Events button that
    shows the selected components events instead of its properties. There you can clear the
    handler for an event and that will automatically delete the corresponding line of code."

    but, that assumes you didn't already delete the code from the source file.
    Since you deleted the code from the source file, the Load Event entry would be blank in the Events window.
    You could always re-double click on the form, to re-generate the Load event handler code, then go to the Event list and delete the event handler and it will then clean up the code in both places as jmc said.
    Last edited by passel; Apr 8th, 2014 at 11:55 AM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: My first c# error

    Quote Originally Posted by ADQUSIT View Post
    Should i remove the entire form1 code block? Please guide me.
    Argh! You've already been guided. Why ask a question if you're not going to read the answers?
    Quote Originally Posted by jmcilhinney View Post
    To fix it, you can go into the designer code file and delete the line manually. It will look something like this:
    Code:
    someComponent.SomeEvent += new EventHandler(SomeMethod)
    Alternatively, you can use the Properties window. Just as in VB, there is an Events button that shows the selected components events instead of its properties. There you can clear the handler for an event and that will automatically delete the corresponding line of code.
    I told you what the line would look like and you found it. I told you specifically to delete that line. Why are you now asking if you should delete the whole code block? Simply follow the instructions you've already got.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: My first c# error

    Thanks to you JMC. But i told you earlier that i'm new to this. I'm not that much picky picky that you said somthing and i get the exact point. You said to remove the line of code, but there was other related data so i was confuse that whether to delete the entire line or block. That's it.

    Thank you Passel. and Once again thanks to you John.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: My first c# error

    Quote Originally Posted by ADQUSIT View Post
    Thanks to you JMC. But i told you earlier that i'm new to this. I'm not that much picky picky that you said somthing and i get the exact point. You said to remove the line of code, but there was other related data so i was confuse that whether to delete the entire line or block. That's it.

    Thank you Passel. and Once again thanks to you John.
    I'm by no means perfect and I do get some things wrong but, more often than not, if I give advice then it's going to solve your problem. I said find the line and delete it. You found the line. Confused or not, you should just followed my instruction and deleted it. That would have solved your problem. If it hadn't, you could have always pressed Ctrl+Z to undelete it and then asked more questions. Coming back and asking whether you should do something other than what you've already been told to do is going to be a waste of everyone's time more often than not. If you're given a solution, do it. If it works then that's all she wrote. Only if what you've been told doesn't work do you need to ask for further advice. We should never have to tell you to do what we've already told you to do because you should have already done it.

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