0

Where does the code come from when one drops an item from a toolbox in a windows form application?

Steps involved are:

  1. Selecting the item
  2. Dragging and dropping to the location in a form
  3. After the item is dropped and located in the form

Where does the equivalent code come from?

Is it generated automatically every time you select an item or was it hard-coded by someone first and it is called every-time we select an item.

I wanted to include the images but its not allowed as I dont have 10 units of reputation.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
Zero Cool
  • 3
  • 3
  • The question is not clear. Do you want to know where the auto generated code appears that creates and initializes the controls? Look at the files that end with `designer.cs`. Or do you want to know how that works that this code is generated? – Tim Schmelter Apr 14 '15 at 09:51
  • I wanted to know about the code itself as in How is it done? – Zero Cool Apr 14 '15 at 09:52
  • But _what_ do you want to know about the code? – Tim Schmelter Apr 14 '15 at 09:52
  • if its auto generated, Can someone help me out with how it is done? – Zero Cool Apr 14 '15 at 09:52
  • 2
    Why do you want to know that? Maybe this helps to understand it: https://msdn.microsoft.com/en-us/library/ms973818.aspx – Tim Schmelter Apr 14 '15 at 09:53
  • In the case of the UI designer for Windows Forms, perhaps check out [this question](http://stackoverflow.com/questions/10272123/how-can-i-customize-the-code-generation-of-initializecomponent-more-specificall). It contains hints at how the Visual Studio IDE generates code. It involves Code DOM paired with reflection & custom attributes on the UI control types (see the MSDN reference for `DesignerSerializerAttribute`). See also [Customizing Code Generation in the .NET Framework Visual Designers](https://msdn.microsoft.com/en-us/library/ms973818.aspx#custcodegen_topic8) for more in-depth info. – stakx - no longer contributing Apr 14 '15 at 10:06
  • And note that code generation works a little differently with XAML-based UI frameworks (WPF and Silverlight) than for Windows Forms. But generally, the Visual Studio designers have inbuilt logic how to generate code for their respective framework; there's usually no hard-coded, pre-written code blocks. The visual designers rely mostly on reflection. – stakx - no longer contributing Apr 14 '15 at 10:07

1 Answers1

0

This is part of what an integrated development environment (IDE) like Visual Studio does for you. The Designer for Winform application consists of several parts:

  • The Toolbox
  • Graphical GUI designer
  • Code generator

It is this code generator which produces the code whenever you drag an item to the graphical designer surface, modify its properties or manipulate it in the designer.

The code generator is a very clever piece of software (it reads the generated code back in case you modified it) and such an animal is not easy to implement.

DrKoch
  • 9,556
  • 2
  • 34
  • 43