1

I have need to add additional code to the Dispose() event of a VB .Net UserControl. How can I do this without coming into conflict with the designer-generated code?

Can the event be moved from usercontrol.designer.vb to usercontrol.vb..? Alternatively, is there a way to split the event between the two files, or tell the designer not to generate that event..? Or, override the override so to speak, between the partial class to the main one..?

spinjector
  • 3,121
  • 3
  • 26
  • 56
  • Firstly, it's a concern that you think that the `Dispose` method is an event. You don't add code to events. You add code to methods, or properties, which are just special methods anyway. – jmcilhinney Jan 12 '20 at 00:54
  • As for the question of moving the method, why do you need to ask us whether something can be done when you can simply try it and see? Create a test project, move the `Dispose` method from the designer code file to the user code file, make some changes to the code, make some changes in the designer and see what happens. If it works then obviously the answer to your question is "yes" and you never needed to ask it here. If it doesn't work then you still don't need to ask that question specifically because you've already answered it. Don't ask here what you can test for yourself. – jmcilhinney Jan 12 '20 at 00:59
  • 1
    I see; you're correct of course. Please forgive the incorrect verbiage, I'm somewhat new at this. It seems I became confused that `Dispose` is an event because it's called automatically. I've been experimenting and searching for a way to handle this for days with no solution. Your tone seems to indicate you have some knowledge of the issue, so perhaps you could elaborate a bit and tell me why it's ok to modify designer-generated code which is clearly commented with the phrase `'Do not modify it using the code editor`, and offer a possible solution in the direction which I described..? – spinjector Jan 12 '20 at 01:33
  • You already have a solution because you suggested it yourself. You just haven't bothered to try it, even though I just told you that you should do so. In programming, it is a good idea to follow the rules until you understand the rules about breaking them. The designer code file is one such case. It gets regenerated often and it is unsafe to modify it directly if you don't understand how that happens. If you'd bothered to try your own suggestion then you'd have seen that that includes generating a `Dispose` method if and only if there isn't one in the user code file. – jmcilhinney Jan 12 '20 at 01:48
  • For the record, I didn't know that prior. After reading your question, I searched the web and tested for myself. It took me a few minutes. If I can do that, one has to wonder why you hadn't done it already, particularly since you suggested the possibility yourself. You should never be posting a question here if you haven't searched and tested yourself first. SO should be a last resort, for the stuff you can't work out for yourself. This one obviously doesn't qualify. – jmcilhinney Jan 12 '20 at 02:07
  • I see. I wasn't aware of the part about methods already in user code, so in the end you did teach me something. Thank you. If you'd cut to the chase with: *"The designer code file ... gets regenerated often and ... that includes generating a `Dispose` method if and only if there isn't one in the user code file"*, it would have made the cadence of this OP much easier to endure. – spinjector Jan 12 '20 at 02:09
  • Well clearly your search methods are much more refined than mine, because I've been searching here and Google for days. Again, thank you. Have a good evening. – spinjector Jan 12 '20 at 02:11
  • I haven't compared lately but I use Bing as my search engine, largely because I always found it better for searches relating to .NET development. I assume that that's because Bing and .NET are both Microsoft properties. – jmcilhinney Jan 12 '20 at 02:24
  • I tend not to "cut to the chase" in such cases because that would encourage people to ask similar questions in the future, when my goal is to have you NOT ask similar questions in the future. Regardless of any issues searching, you could still have tried it for yourself. If you have to spend a bit of time reading that then, hopefully, it will sink in and you'll do it next time. If all you remember is that you got the answer you wanted immediately, what motivation is there to do anything different next time? – jmcilhinney Jan 12 '20 at 02:27
  • Thank you for the Bing suggestion; I'll try that in the future. Could you tell me what keywords you used, or point out something on this topic of "certain methods" in some documentation..? As I think this through, I assume `New` is possibly one of them as well...? Perhas *all* the base class methods..? This micro-issue has solved a larger one in a project I've been slowly working on for months, a VSTO application addin converted from Word to Access. I had another OP out there, and with your help I've just answered my own question there as well: https://stackoverflow.com/a/59700499/2705042 – spinjector Jan 12 '20 at 02:46
  • Regarding not asking similar questions, I wouldn't have posted my OP if I hadn't searched StackOverflow already. We're not all that way, you know, lol. I looked high & low for anything about this, but came up nill. I can only assume I was using incorrect terminology, so if you could help me a bit there as well, I would certainly appreciate it. – spinjector Jan 12 '20 at 02:49
  • Even if you searched and came up empty, you still didn't test. You asked us whether you could move the `Dispose` method from file to file when you could have just done it and seen what happened. If you're not "that way", i.e. think to try something to see what happens rather than asking others to tell you what happens, then I suggest you make effort to change that. There's very little cost to testing in programming. As for searching, I used "vb.net dispose user control", so no trick to that. In my experience, most searching fails occur because people use an essay instead of actual keywords. – jmcilhinney Jan 12 '20 at 03:21
  • 1
    I search in Google. Natural language search "when is ok to edit designer code in visual studio" produced https://stackoverflow.com/questions/16161994/manual-editing-of-designer-cs-file/22103504. Then I tried "can .dispose be moved from designer code in visual studio" and got several interesting results. – Mary Jan 12 '20 at 13:19
  • I used combinations of those exact words many times in the past week as I searched on this issue. Clearly I missed the good hits, or perhaps skimmed over them, not realizing what I was missing. – spinjector Jan 12 '20 at 16:50
  • The Dispose method is designer generated only in the sense that VS uses the Form Item Template files(`C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\VisualBasic\Windows Forms\1033\Form`) to create the files. The Dispose method could just as well have been placed in the _FormName_.vb file instead. The actual WinForm designer never touches the Dispose method. – TnTinMn Jan 13 '20 at 03:42

1 Answers1

2

In commenting with user @jmcilhinney on my OP, the answer is this:

The Dispose() method can be moved from usercontrol.designer.vb to usercontrol.vb without issue. The reason for this is as follows:

The designer code file ... gets regenerated often and ... that includes generating a Dispose method if and only if there isn't one in the user code file.

And so after moving, Dispose can be modified at will, and it will not be regenerated and/or overwritten by the designer by any subsequent changes to the UserControl.

spinjector
  • 3,121
  • 3
  • 26
  • 56
  • 1
    The Dispose method is not rewritten after it's generated (it's not part of the Form's design serialization, so it's never recreated). You can dispose of disposable objects in the `Form.FormClosed` event, possibly adding a *centralized* `CleanUp()` method. – Jimi Jan 12 '20 at 13:46
  • Yes, I know that now. Despite days of searching on here and Google, I did not find any information about this. – spinjector Jan 12 '20 at 16:39