4

I'm making a winform application. I'm trying to add a keydown event to a combo box so I prepared the event implementation and then I added inside the form.designers document the line:

this.cmboxSearchPath.KeyDown += cmboxSearchPath_KeyDown;

for some reason this line always get deleted from the document.. do you know why and how can I stop it from being deleted?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

2 Answers2

4

for some reason this line always get deleted from the document.. do you know why and how can I stop it from being deleted?

Don't put it in the designer generated code. You can include this in your constructor after the InitializeComponent() call, or allow the designer to generate this for you by wiring up the event via the designer itself.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • The designer just seems like the perfect place for everything.. I believe it should be there and not in the constructoe... What do you mean about "allow the designer to generate this for you by wiring up the event via the designer itself" ? – CodeMonkey May 27 '13 at 19:55
  • 5
    @YonatanNir If you click on the combo box in the designer (not the .cs file, but the visual designer), and go to the properties window, then choose "events" at the top, and double click on "KeyDown". The designer will take care of adding this for you. – Reed Copsey May 27 '13 at 19:59
  • one of the dumbest thing I have to deal with working in winform. If you create event from the designer it puts the code in the designer.cs. But if you manually add the event in there somehow it doesn't like it anymore. – SZT Jun 10 '20 at 16:34
0

Its not a good idea to modify the desginer.cs file. Attach the event in the form designer or you can attach the event in the code behind (form.cs). It is probably disappearing if you are loading the form in the designer and you are getting the message to ignore changes and reload design.

Habib
  • 219,104
  • 29
  • 407
  • 436