Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to add controls to the designer surface during run time
 

How to add controls to the designer surface during run time

Hi,

Does any one has idea on how to add controls to the designer surface during run time.

Purpose: Controls that are added to the designer surface will be saved in a file and while loading the saved file,the same controls that has been saved should be redrawn on the designer surface.

Problem: Only the child level controls are drawn. I mean if there is a panel which contains TextBox and Label, then only the Textbox and Label are drawn but the panel is not drawn. and after the controls are drawn , I am not able to select the controls and also they are fixed.

Thanks in Advance.

lakshmi09  Thursday, March 20, 2008 9:58 AM

Hi lakshmi09,

Based on my understanding, you want to create an application with design time features supported and generate and save code for this, like we do in Visual Studio, right? If so, you need to use the CodeDom to generate source code. The CodeDom (Code Document Object Model) enables developers of programs that emit source code to generate source code in multiple programming languages at run time, based on a single model that represents the code to render. Here is a sample for your reference.

· http://support.microsoft.com/kb/813808/

For more information about CodeDOM, please check:
http://msdn2.microsoft.com/en-us/library/y2k85ax6.aspx
http://msdn2.microsoft.com/en-us/library/f1dfsbhc.aspx

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Wednesday, March 26, 2008 8:48 AM

In general to add any control to the form or panel or group box u need to follow the following ...

1. Make the instance of the control to be added.

2. Set the propertirs like size,location

3.Add it to the controls collection of form or panel or group box

Example to add label to form...

Dim member As New Label

member.BackColor = System.Drawing.Color.Red

member.Size = New System.Drawing.Size(12, 12)

member.Text = "Sreenath"

member.Top = 50

member.Left = 25

Me.Controls.Add(member) or if u want to add it to panel then use Me.Panel1.Controls.Add(member)

This will work....

Sreenath G V  Thursday, March 20, 2008 12:07 PM

Hi Sreenath,

Thank you. I added the controls to the form/panel in the same way as mentioned, but my problem is that the controls aredrawn while loading the file, but I am not able to select those controls and they are fixed in position.

Adding the controls to the designer form while design time and run time is same? If same, why am I not able to select the controls or move them (this behaviour is not seen dusring design time)

If any one has idea on this, please let me know. Thank you.

lakshmi09  Friday, March 21, 2008 6:31 AM

sorry to interrupt you but can u explain problem clearly regarding moving controls...wat does this mean actually...

Sreenath G V  Friday, March 21, 2008 6:37 AM

Dusring design time we select a partcular control to set its properties and we will be able to move it to change to desired location/resize it.

As I am saving the controls and drawing them when loading the saved file, I am not able to select those controls just the way when we run any application(where we cannot change its location or any other properties). But I want to behave the application the same way as it was while the controls are drawn initially(design time) at run time also.

lakshmi09  Friday, March 21, 2008 2:36 PM

Hi lakshmi09,

Based on my understanding, you want to create an application with design time features supported and generate and save code for this, like we do in Visual Studio, right? If so, you need to use the CodeDom to generate source code. The CodeDom (Code Document Object Model) enables developers of programs that emit source code to generate source code in multiple programming languages at run time, based on a single model that represents the code to render. Here is a sample for your reference.

· http://support.microsoft.com/kb/813808/

For more information about CodeDOM, please check:
http://msdn2.microsoft.com/en-us/library/y2k85ax6.aspx
http://msdn2.microsoft.com/en-us/library/f1dfsbhc.aspx

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Wednesday, March 26, 2008 8:48 AM

Assuming you are hosting the designer inside your application, I came up with this little code snippet to do progamatically add controls to a design surface.

Code Snippet

_designManager is a reference to DesignSurfaceManager

Dim idh As IDesignerHost = CType(_designManager.ActiveDesignSurface().GetService(GetType(IDesignerHost)), IDesignerHost)

Dim tbu As IToolboxUser = CType(idh.GetDesigner(CType(idh.RootComponent, System.ComponentModel.IComponent)), IToolboxUser)

tbu.ToolPicked(New ToolboxItem(GetType(LinkLabel)))

lakshmi09 wrote:

Hi,

Does any one has idea on how to add controls to the designer surface during run time.

Purpose: Controls that are added to the designer surface will be saved in a file and while loading the saved file,the same controls that has been saved should be redrawn on the designer surface.

Problem: Only the child level controls are drawn. I mean if there is a panel which contains TextBox and Label, then only the Textbox and Label are drawn but the panel is not drawn. and after the controls are drawn , I am not able to select the controls and also they are fixed.

Thanks in Advance.

Chris Myers  Thursday, April 17, 2008 1:47 PM

You can use google to search for other answers

Custom Search

More Threads

• My class in the property grid
• How to create lable with rounded corners ?
• CodeDom passing an enum to a method invoke
• Winforms controls get dislocated
• ImageList ImageStream property corrupted in the form's resx file
• Visual Studio just trashed my toolstrips!!
• Resize form
• Combobox does not access key stroke
• icons - what sizes do i need to cover te requirements of windows os
• Custom component that will create and add controls to a form