有人认为用写的动态创建控件很高端,其实我更喜欢拖放控件进行各种属性和事件设置很方便,而不是用写的进行各种属性和事件的设置,结合以前写的例子,对各类设置做了归类,比较方便集中写作。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 文字提取到数据文件 { public partial class 文字提取到数据文件 : Form { DataGridView 数据列表 = new DataGridView(); TextBox 录入框 = new TextBox(); ToolTip 控件说明 = new ToolTip(); TreeView 分类树 = new TreeView(); CheckBox 肝 = new CheckBox(), 心 = new CheckBox(), 脾 = new CheckBox(), 肺 = new CheckBox(), 肾 = new CheckBox(), 热 = new CheckBox(), 温 = new CheckBox(), 寒 = new CheckBox(), 凉 = new CheckBox(), 平 = new CheckBox(); public 文字提取到数据文件() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { 跟随(); 停靠(); 尺寸(); 布局(); 初值(); 注明(); 动态文本框(); 动态表格(new string[] { "名称", "别名", "性味", "归经", "主治" }); } //private void 动态选项框() //{ // int i = 分类树.Width + 5; // string[] 五脏五味 = { "肝(酸)", "心(苦)", "脾(甘)", "肺(辛)", "肾(咸)" }; // foreach (string 名 in 五脏五味) // { // CheckBox 选项框 = new CheckBox(); // 选项框.Text = 名; // 选项框.Name = 名.Remove(1); // 选项框.Location = new Point(i, 17); // 选项框.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right); // 选项框.Parent = this; // this.Controls.Add(选项框); // i += 105; // }; //} private void 动态文本框() { 录入框.Text = ""; 录入框.Name = string.Format("文本框{0}", 1); 录入框.AllowDrop = true; 录入框.Multiline = true; //this.Controls.Add(录入框); } private void 动态表格(string[] 数据格) { DataTable 数据表 = new DataTable("数据表"); foreach (string 格 in 数据格) 数据表.Columns.Add(格, typeof(string)); 数据列表.AllowUserToAddRows = false; 数据列表.AllowUserToDeleteRows = false; 数据列表.AllowUserToOrderColumns = false; 数据列表.AllowUserToResizeColumns = false; 数据列表.AllowUserToResizeRows = false; 数据列表.ReadOnly = true; 数据列表.RowHeadersVisible = false; 数据列表.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 数据列表.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 数据列表.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders; 数据列表.RowsDefaultCellStyle.WrapMode = (DataGridViewTriState.True); 数据列表.DefaultCellStyle.SelectionBackColor = Color.Transparent; 数据列表.DefaultCellStyle.SelectionForeColor = Color.Red;/*.Blue.Gold*/ 数据列表.GridColor = Color.Lime; 数据列表.DataSource = 数据表; } private void 文字提取到数据文件_Resize(object sender, EventArgs e) { //MessageBox.Show("最大化!"); //this.Invalidate(); //this.Refresh(); 跟随(); 停靠(); 尺寸(); 初值(); 布局(); 注明(); //System.IO.FileStream 打开 = new System.IO.FileStream(Application.StartupPath + @"\新建文本文档.txt", System.IO.FileMode.Open); //System.IO.StreamReader 读取 = new System.IO.StreamReader(打开, Encoding.GetEncoding("GB2312")); //while (!读取.EndOfStream) // 数据表.Rows.Add(读取.ReadLine().Split(' ')); //打开.Close(); } void 跟随() { 数据列表.Anchor = 录入框.Anchor = 肝.Anchor = 心.Anchor = 脾.Anchor = 肺.Anchor = 肾.Anchor = 热.Anchor = 温.Anchor = 寒.Anchor = 凉.Anchor = 平.Anchor = 分类树.Anchor = (AnchorStyles.Top | AnchorStyles.Left); } void 停靠() { 数据列表.Parent = 录入框.Parent = 肝.Parent = 心.Parent = 脾.Parent = 肺.Parent = 肾.Parent = 热.Parent = 温.Parent = 寒.Parent = 凉.Parent = 平.Parent = 分类树.Parent = this; } void 尺寸() { 分类树.Size = new Size(this.Width / 5, this.Height - 42); 数据列表.Size = new Size(this.Width - 分类树.Width - 24, this.Height / 2); 录入框.Size = new Size(this.Width - 分类树.Width - 25, this.Height / 2 - 75); } void 布局() { 分类树.Location = new Point(2, 2); 数据列表.Location = new Point(分类树.Right + 5, 2); 录入框.Location = new Point(分类树.Right + 5, 数据列表.Bottom + 5); 肝.Location = new Point(分类树.Width + 5, this.Height - 65); 心.Location = new Point(肝.Location.X + 65, this.Height - 65); 脾.Location = new Point(心.Location.X + 65, this.Height - 65); 肺.Location = new Point(脾.Location.X + 65, this.Height - 65); 肾.Location = new Point(肺.Location.X + 65, this.Height - 65); 热.Location = new Point(肾.Location.X + 65, this.Height - 65); 温.Location = new Point(热.Location.X + 35, this.Height - 65); 寒.Location = new Point(温.Location.X + 35, this.Height - 65); 凉.Location = new Point(寒.Location.X + 35, this.Height - 65); 平.Location = new Point(凉.Location.X + 35, this.Height - 65); } void 初值() { this.BackColor = Color.Red; string[] 五脏五味 = { "肝(酸)", "心(苦)", "脾(甘)", "肺(辛)", "肾(咸)", "热", "温", "寒", "凉", "平" }; int 啊 = 五脏五味.Length; Control.ControlCollection 找选项 = this.Controls; foreach (Control 选项 in 找选项) if (选项 is CheckBox) 选项.Text = 五脏五味[--啊]; } void 注明() { 控件说明.SetToolTip(录入框, "请输入数据文本以便进行分割到数据文件保存。"); 控件说明.SetToolTip(分类树, "显示选择的分类结构。"); } } }
---------------------
作者:智者知已应修善业
来源:CSDN
原文:https://blog.csdn.net/xianfajushi/article/details/83867006
版权声明:本文为作者原创文章,转载请附上博文链接!