.net - Can't view designer when coding a form in C# -


i'm following this tutorial on winforms, , far tutorial coding form without using toolbox. believe it'll introduce toolbox in more depth shortly.

following tutorial, i've made partial class in following 2 pieces of code:

first file:

using system; using system.windows.forms;  public class numeric : system.windows.forms.textbox {     public numeric()     {     } }  public partial class exercise {     private numeric txtbox;     system.componentmodel.container components; } 

second file:

using system; using system.windows.forms;  public partial class exercise : form {     private void initializecomponent()     {         txtbox = new numeric();         controls.add(txtbox);     }      public exercise()     {         initializecomponent();     } }  public class program {     public static int main()     {         application.run(new exercise());         return 0;     } } 

when run code f5, looks fine: form pops textbox.

but reason, when right-click on second file , choose "view designer", error says "the variable 'txtbox' either undeclared or never assigned". can choose "ignore , continue", leads me form no textbox on it.

why occur? know of think should use toolbox, , it's sensible thing do, still understand why happens.

when open form in windows forms designer, designer looks first class in file. if file has designer.cs containing other partial part of class, includes , tries deserialize file contents. in process of deserialization , loading design time of form, creates instance of base class of form , looks in files component declarations , initializecomponents method. if find them creates components , sets properties of them using deserialized codes , add components instance of base class created.

some useful facts:

  • codes in constructor of form not execute @ design-time, constructor of base class of form execute in design-time.
  • codes in initializecomponent not execute @ design-time, codes deserialized , used create designer of form.
  • the designer can not show form has abstract base class. (solution)
  • the designer can not show form has generic class. example can not show myform:someform<someclass>, can show someform<t>:form. (solution)
  • if define new property form, properties not show in properties window. properties window, shows properties of base class values of form.
  • when file contains 2 class, if form not first class designer can not load , receive warning says form should first class show in designer.
  • above rules apply usercontrols.

example

take @ below code, has serious problems:

  • the class has different constructor class name
  • the statement int i="x";
  • there no semicolons while c# class
  • the initializecomponent method didn't call in constructor

but interesting news can see form in designer, errors!

just create file in project , put below codes in file , save file , close it. without trying build solution, open form in designer. here code:

using system using system.windows.forms namespace sampleapplication {     public class myform:form     {         public notmyform()         {         }         public void initializecomponent()         {             int i="x";             textbox1 = new textbox()             textbox1.text = "hi"             this.controls.add(textbox1)         }         private textbox textbox1     } } 

and here screenshot of designer:

enter image description here

more information

to find more information, take @ link:

solution question

as solution, enough move private numeric txtbox; , put seccond file in exercise class.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -