如何使用.Designe.cs文件或.resx文件创建表单设计图面? [英] How to create form design surface using .Designe.cs file or .resx file?

查看:61
本文介绍了如何使用.Designe.cs文件或.resx文件创建表单设计图面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建使用.Designe.cs文件或.resx文件生成设计表面的应用程序?
我通常使用service来做:

How to create an application to generate design suface by using .Designe.cs file or .resx file ?
I do it normally using service:

public Form1()
		{
			InitializeComponent();
			Initialize();
		}
		private ServiceContainer serviceContainer = null;

		private void Initialize()
		{
			IDesignerHost host;
			Form form;
			IRootDesigner rootDesigner;
			Control view;

			// 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);

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

			// 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.WindowsForms);
			view.Dock = DockStyle.Fill;
			pnlLoadDesing.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();
		} 


但我想使用任何Windows窗体应用程序的.Designe.cs文件或.resx文件执行此操作.

谢谢


but I want to do this using .Designe.cs file or .resx file of any windows form application.

Thanks

推荐答案

要构建表单,您需要具有form.cs和form.Designer.cs文件.
不要理会resources(resx)文件.

  • 创建一个将代码写入TextWriter的应用程序,文件的编写方式应声明一个控件并将其添加,将属性设置为表单并将其添加至表单.
In order to build a form,you need to have form.cs and form.Designer.cs files.
Do not bother about resources(resx) file.

  • Create an application that writes code into a TextWriter,the file should be written in such a way that you declare a control and add the control,set properties to the form and add it to the form.
<br />
<pre><br />
textWriter.WriteLine("using System;");<br />
textWriter.WriteLine("using System.Collections.Generic;");<br />
textWriter.WriteLine("using System.ComponentModel;");<br />
textWriter.WriteLine("using System.Data;");<br />
textWriter.WriteLine("using System.Drawing;");<br />
textWriter.WriteLine("using System.Linq;");<br />
textWriter.WriteLine("using System.Text;");<br />
textWriter.WriteLine("using System.Windows.Forms;");<br />
textWriter.WriteLine("using System.IO;");<br />
textWriter.WriteLine("namespace MyFramework.Gui.LoadItems");<br />
textWriter.WriteLine("{");<br />
textWriter.WriteLine("    public partial class Form1 : Form");<br />
textWriter.WriteLine("    {");<br />
textWriter.WriteLine("        public Form1()");<br />
textWriter.WriteLine("        {");<br />
textWriter.WriteLine("            InitializeComponent();");<br />
textWriter.WriteLine("        }");<br />
textWriter.WriteLine("}");<br />
</pre><br />



  • 创建表单构建器的一个更好的选择是读取我们正在使用的数据库的架构,并根据数据库中使用的数据类型使用表单中的相应控件.例如,如果名称" '是类型为varchar的表中的一列,在表单中添加一个名称为txtName和标签为lblName的文本框并设置两个控件的属性.可以在设计器代码中设置诸如top,Left等的各种属性以放置控件正确.


    • A better option for creating a form builder is to read the schema of the database that we are using and according to the datatypes used in the database use corresponding controls in the form.For eg,if ''Name'' is a column in table of type varchar, add a corresponding textbox with name txtName and a label lblName to the form and set properties for both controls.The various properties like top,Left etc can be set in the designer code for placing the controls properly.
      • 可以使用相同的过程来构建form.cs并从Form类继承,使两个类都成为局部类.
      • 现在,如果将designer和form.cs都添加到项目中,它们将正常工作为使用工具箱和手动编码构建的表单.


      这篇关于如何使用.Designe.cs文件或.resx文件创建表单设计图面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆