when create new windows form, visual studio automatically provides me 2 files:
myform.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace hardest_game { public partial class myform : form { public myform() { initializecomponent(); } } }
myform.designer.cs
namespace hardest_game { partial class myform { /// <summary> /// required designer variable. /// </summary> private system.componentmodel.icontainer components = null; /// <summary> /// clean resources being used. /// </summary> /// <param name="disposing">true if managed resources should disposed; otherwise, false.</param> protected override void dispose(bool disposing) { if (disposing && (components != null)) { components.dispose(); } base.dispose(disposing); } #region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> private void initializecomponent() { this.components = new system.componentmodel.container(); this.autoscalemode = system.windows.forms.autoscalemode.font; this.text = "myform"; } #endregion } }
my first question is, why creating 2 files? why not define methods in 1 file?
second question, , more important one, why using system.componentmodel.icontainer
here, dispose()
methods , such? actually do, msdn.microsoft.com doesn't provide information, explains they're used contain stuff.
this code seems work fine:
using system.windows.forms; using system.drawing; namespace hardest_game { class mainwindow : form { public mainwindow() { this.initializecomponent(); } // controls button mybutton { set; get; } void initializecomponent() { mybutton = new button(); mybutton.text = "my button"; this.controls.add(mybutton); } } }
it looks cleaner, uses less stuff, etc. why use method microsoft wants me use?
edit: okay sorry guys, second question might not have been best one, let me try again: if programmatically create program (as in third piece of code), including of ui etc, benefit using icontainer
, dispose()
? that's why asked what's purpose of them, know if should use them myself, or if they're autogenerated code.
the class split 2 files (partial classes) can keep auto-generated designer code separate own logic. winforms designer in visual studio autogenerate code in designer file, adds , positions of controls. (and should not manually modified because of this)
the container
object used holding non ui components, such timers. dispose method automatically implemented clean these resources when application/window closes. see what's purpose of components icontainer generated winforms designer?