3 Replies - 282 Views - Last Post: 13 June 2013 - 01:53 PM Rate Topic: -----

#1 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

C# Deleting unneeded methods

Posted 13 June 2013 - 07:21 AM

Hey,
As I develop my applications further, I got so many things like this

        private void groupBox10_Enter(object sender, EventArgs e)
        {

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void label21_Click(object sender, EventArgs e)
        {

        }



Tried to delete it, when I go back to the form designer, my program will broke :helpsmilie: I had to delete the object first then after that I can delete the codes. I dont want to delete the objects, just the unneeded methods that go all over my codes. Any fast way to do that?

Ty!

Is This A Good Question/Topic? 0
  • +

Replies To: C# Deleting unneeded methods

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5508
  • View blog
  • Posts: 11,812
  • Joined: 02-June 10

Re: C# Deleting unneeded methods

Posted 13 June 2013 - 07:39 AM

Hard to understand exactly what you're describing.
But I will guess that you deleted the methods, but not the subscription to the method. If you look in the form designer code you will see where each of these methods were subscribed to by the control.

  • Double-click the error in the error pallet.
  • It will take you to the line in the designer where the subscription is.
  • Delete the line.


Attached Image

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9191
  • View blog
  • Posts: 34,504
  • Joined: 12-June 08

Re: C# Deleting unneeded methods

Posted 13 June 2013 - 07:44 AM

Assume once you deleted the methods the compiler was throwing a fit? Perhaps double clicking on the compiler and it will take you the point of the error.. which, in the case you outlined, is likely the designer file had a problem that it was adding event handlers to your GUI objects and the methods itwas counting on are now gone.
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: C# Deleting unneeded methods

Posted 13 June 2013 - 01:53 PM

Adding on to tlhIn`toq advice, if OP is unfamiliar with modifying the designer file i would recommend that he uses the properties window in the Visual studio then go to Events icon, right click on event he want to delete and select the Reset option. It does the same thing but without the possibility screwing something up

Attached image(s)

  • Attached Image

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1