2

I was trying to create a custom form designer with this article: http://www.developerfusion.com/article/4351/hosting-windows-forms-designers/ that lets you create a custom form, visual studio alike. but I can't figure out how to get all the controls in the created form, does someone know how to do this?

I used this code from the article:

using System;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Data;

namespace Hosting
{
    public class frmMain : System.Windows.Forms.Form
    {
        private System.Windows.Forms.PropertyGrid propertyGrid;
        private System.Windows.Forms.Splitter splitter1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel pnlViewHost;
        private System.Windows.Forms.Splitter splitter2;
        private ToolboxService lstToolbox;
        private System.Windows.Forms.Label lblSelectedComponent;
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem mnuDelete;
        private MenuItem menuItem2;
        private IContainer components;

        public frmMain()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            Initialize();

        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.propertyGrid = new System.Windows.Forms.PropertyGrid();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.panel1 = new System.Windows.Forms.Panel();
            this.splitter2 = new System.Windows.Forms.Splitter();
            this.lblSelectedComponent = new System.Windows.Forms.Label();
            this.pnlViewHost = new System.Windows.Forms.Panel();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.mnuDelete = new System.Windows.Forms.MenuItem();
            this.lstToolbox = new Hosting.ToolboxService();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // propertyGrid
            // 
            this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.propertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
            this.propertyGrid.Location = new System.Drawing.Point(0, 182);
            this.propertyGrid.Name = "propertyGrid";
            this.propertyGrid.Size = new System.Drawing.Size(224, 312);
            this.propertyGrid.TabIndex = 0;
            // 
            // splitter1
            // 
            this.splitter1.Dock = System.Windows.Forms.DockStyle.Right;
            this.splitter1.Location = new System.Drawing.Point(596, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(4, 518);
            this.splitter1.TabIndex = 1;
            this.splitter1.TabStop = false;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.lstToolbox);
            this.panel1.Controls.Add(this.splitter2);
            this.panel1.Controls.Add(this.propertyGrid);
            this.panel1.Controls.Add(this.lblSelectedComponent);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
            this.panel1.Location = new System.Drawing.Point(600, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(224, 518);
            this.panel1.TabIndex = 2;
            // 
            // splitter2
            // 
            this.splitter2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.splitter2.Location = new System.Drawing.Point(0, 178);
            this.splitter2.Name = "splitter2";
            this.splitter2.Size = new System.Drawing.Size(224, 4);
            this.splitter2.TabIndex = 1;
            this.splitter2.TabStop = false;
            // 
            // lblSelectedComponent
            // 
            this.lblSelectedComponent.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.lblSelectedComponent.Location = new System.Drawing.Point(0, 494);
            this.lblSelectedComponent.Name = "lblSelectedComponent";
            this.lblSelectedComponent.Size = new System.Drawing.Size(224, 24);
            this.lblSelectedComponent.TabIndex = 3;
            this.lblSelectedComponent.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // pnlViewHost
            // 
            this.pnlViewHost.BackColor = System.Drawing.SystemColors.Window;
            this.pnlViewHost.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnlViewHost.Location = new System.Drawing.Point(0, 0);
            this.pnlViewHost.Name = "pnlViewHost";
            this.pnlViewHost.Size = new System.Drawing.Size(596, 518);
            this.pnlViewHost.TabIndex = 3;
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuDelete,
            this.menuItem2});
            this.menuItem1.Text = "&Edit";
            // 
            // mnuDelete
            // 
            this.mnuDelete.Index = 0;
            this.mnuDelete.Shortcut = System.Windows.Forms.Shortcut.Del;
            this.mnuDelete.Text = "&Delete";
            this.mnuDelete.Click += new System.EventHandler(this.mnuDelete_Click);
            // 
            // lstToolbox
            // 
            this.lstToolbox.BackColor = System.Drawing.SystemColors.Control;
            this.lstToolbox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.lstToolbox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lstToolbox.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lstToolbox.IntegralHeight = false;
            this.lstToolbox.ItemHeight = 16;
            this.lstToolbox.Location = new System.Drawing.Point(0, 0);
            this.lstToolbox.Name = "lstToolbox";
            this.lstToolbox.SelectedCategory = null;
            this.lstToolbox.Size = new System.Drawing.Size(224, 178);
            this.lstToolbox.Sorted = true;
            this.lstToolbox.TabIndex = 2;
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 1;
            this.menuItem2.Text = "Save";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // frmMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(824, 518);
            this.Controls.Add(this.pnlViewHost);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.panel1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Menu = this.mainMenu1;
            this.Name = "frmMain";
            this.Text = "Designer Hosting Sample";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new frmMain());
        }

        private ServiceContainer serviceContainer = null;
        private MenuCommandService menuService = null;
        Control view;
        private void Initialize()
        {
            IDesignerHost host;
            Form form;
            IRootDesigner rootDesigner;

            // Initialise service container and designer host
            serviceContainer = new ServiceContainer();
            serviceContainer.AddService(typeof(INameCreationService), new NameCreationService());
            serviceContainer.AddService(typeof(IUIService), new UIService(this));
            host = new DesignerHost(serviceContainer);

            // Add toolbox service
            serviceContainer.AddService(typeof(IToolboxService), lstToolbox);
            lstToolbox.designPanel = pnlViewHost;
            PopulateToolbox(lstToolbox);

            // Add menu command service
            menuService = new MenuCommandService();
            serviceContainer.AddService(typeof(IMenuCommandService), menuService);

            // Start the designer host off with a Form to design
            form = (Form)host.CreateComponent(typeof(Form));
            form.TopLevel = false;
            form.Text = "Form1";

            // Get the root designer for the form and add its design view to this form
            rootDesigner = (IRootDesigner)host.GetDesigner(form);
            view = (Control)rootDesigner.GetView(ViewTechnology.Default);

            view.Dock = DockStyle.Fill;
            pnlViewHost.Controls.Add(view);

            // Subscribe to the selectionchanged event and activate the designer
            ISelectionService s = (ISelectionService)serviceContainer.GetService(typeof(ISelectionService));
            s.SelectionChanged += new EventHandler(OnSelectionChanged);
            host.Activate();
        }

        private void PopulateToolbox(IToolboxService toolbox)
        {
            toolbox.AddToolboxItem(new ToolboxItem(typeof(Button)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(ListView)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(TreeView)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(TextBox)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(Label)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(TabControl)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(OpenFileDialog)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(CheckBox)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(ComboBox)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(GroupBox)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(ImageList)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(Panel)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(ProgressBar)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(ToolBar)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(ToolTip)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(StatusBar)));
        }

        private void OnSelectionChanged(object sender, System.EventArgs e)
        {
            ISelectionService s = (ISelectionService)serviceContainer.GetService(typeof(ISelectionService));

            object[] selection;
            if (s.SelectionCount == 0)
                propertyGrid.SelectedObject = null;
            else
            {
                selection = new object[s.SelectionCount];
                s.GetSelectedComponents().CopyTo(selection, 0);
                propertyGrid.SelectedObjects = selection;
            }

            if (s.PrimarySelection == null)
                lblSelectedComponent.Text = "";
            else
            {
                IComponent component = (IComponent)s.PrimarySelection;
                lblSelectedComponent.Text = component.Site.Name + " (" + component.GetType().Name + ")";
            }
        }

        private void mnuDelete_Click(object sender, System.EventArgs e)
        {
            menuService.GlobalInvoke(StandardCommands.Delete);
        }

    }
}

Here is what I tried:

        // And then i tried this to get all the controls and write them to a python tkinter file:
        private void menuItem2_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter("output.py");
            sw.WriteLine("from tkinter import *\n");
            sw.WriteLine("# Main form");
            sw.WriteLine(this.Name + " = Tk()\n");
            foreach (IComponent b in components.Components)
            {
                MessageBox.Show(b.GetType().ToString());
                if (b.GetType() == typeof(Button))
                {
                    Button but = (Button)b;
                    sw.WriteLine("# " + but.Name);
                    sw.WriteLine(but.Name + " = Button(" + this.Name + @"fg=""" + but.ForeColor.Name + @"""bg=""" + but.BackColor.Name + @"""" + ")");
                }
            }
            sw.Close();
       }

thanks in advance.

9
  • FormInstance.Controls ? Also, what happens if your article becomes a dead link? Write a short summary so we know what it's about. Commented Apr 28, 2016 at 13:35
  • No, that doesn't work... Commented Apr 28, 2016 at 13:36
  • What exactly do you want to do? This should give you all controls whose first parent is FormInstance. Commented Apr 28, 2016 at 13:40
  • I want to get the controls in the user created form Commented Apr 28, 2016 at 13:41
  • Edit the question and supply the code you wrote. It appears we have a misunderstanding here. Commented Apr 28, 2016 at 13:42

1 Answer 1

1

You get add control by use for loop.

foreach(Control c in form.Controls)
{
   //You can save all attribute for each control on file or database
   //c.Left, c.Top, c.Name,...
}

When you reload control in to form, use by code:

TextBox textbox = (TextBox)host.CreateComponent(typeof(TextBox));
textbox.Left = your value save before;
textbox.Top = your value save before;
textbox.Text = your text save before;
form.Controls.Add(textbox);

..Similar for orther controls

Sign up to request clarification or add additional context in comments.

1 Comment

Please put some more effort into your grammar, it's hard to understand what you are writing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.