为了账号安全,请及时绑定邮箱和手机立即绑定

C# 设计模式和代码页面的切换疑问?

C# 设计模式和代码页面的切换疑问?

陪伴而非守候 2018-12-06 22:01:30
这样一段代码为什么设计页面要报错?   1 using System.Windows.Forms; 2 using System.Collections.Generic; 3 namespace WFAListViewer 4 { 5 partial class Form1 6 { 7 /// 8 /// Required designer variable. 9 /// 10 private System.ComponentModel.IContainer components = null; 11 12 /// 13 /// Clean up any resources being used. 14 /// 15 /// true if managed resources should be disposed; otherwise, false. 16 protected override void Dispose(bool disposing) 17 { 18 if (disposing && (components != null)) 19 { 20 components.Dispose(); 21 } 22 base.Dispose(disposing); 23 } 24 25 #region Windows Form Designer generated code 26 27 /// 28 /// Required method for Designer support - do not modify 29 /// the contents of this method with the code editor. 30 /// 31 private void InitializeComponent() 32 { 33 this.SuspendLayout(); 34 // 35 // Form1 36 // 37 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 38 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 39 this.ClientSize = new System.Drawing.Size(284, 262); 40 this.Name = "Form1"; 41 this.Text = "Form1"; 42 this.ResumeLayout(false); 43 44 this.ListView1 = new System.Windows.Forms.ListView(); 45 this.ListView1.BackColor = System.Drawing.SystemColors.Control; 46 this.ListView1.Dock = System.Windows.Forms.DockStyle.Top; 47 this.ListView1.Location = new System.Drawing.Point(0, 0); 48 this.ListView1.Name = "ListView1"; 49 this.ListView1.Size = new System.Drawing.Size(292, 130); 50 this.ListView1.TabIndex = 0; 51 this.ListView1.View = System.Windows.Forms.View.Details; 52 this.ListView1.MultiSelect = true; 53 this.ListView1.HideSelection = false; 54 this.ListView1.HeaderStyle = ColumnHeaderStyle.Clickable; 55 56 ColumnHeader columnHeader1 = new ColumnHeader(); 57 columnHeader1.Text = "Breakfast Item"; 58 columnHeader1.TextAlign = HorizontalAlignment.Left; 59 columnHeader1.Width = 146; 60 61 ColumnHeader columnHeader2 = new ColumnHeader(); 62 columnHeader2.Text = "Price Each"; 63 columnHeader2.TextAlign = HorizontalAlignment.Center; 64 columnHeader2.Width = 142; 65 66 this.ListView1.Columns.Add(columnHeader1); 67 this.ListView1.Columns.Add(columnHeader2); 68 69 string[] foodList = new string[] { "Juice", "Coffee", "Cereal & Milk", "Fruit Plate", "Toast & Jelly", "Bagel & Cream Cheese" }; 70 string[] foodPrice = new string[] { "1.09", "1.09", "2.19", "2.49", "1.49", "1.49" }; 71 72 for (int count = 0; count < foodList.Length; count++) 73 { 74 ListViewItem listItem = new ListViewItem(foodList[count]); 75 listItem.SubItems.Add(foodPrice[count]); 76 ListView1.Items.Add(listItem); 77 } 78 this.Controls.Add(ListView1); 79 80 81 } 82 83 #endregion 84 85 private System.Windows.Forms.ListView ListView1; 86 private System.Windows.Forms.TextBox TextBox1; 87 } 88 } 主要就是插入foodList和foodprice那块,我理解是Form1是静态类?所以不允许实例化? 报的错是    The designer cannot process the code at line 72: for (int count = 0; count < foodList.Length; count++) { ListViewItem listItem = new ListViewItem(foodList[count]); listItem.SubItems.Add(foodPrice[count]); ListView1.Items.Add(listItem); } The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.      我印象中学C#的时候和vb差不多,设计页面里改什么代码页也会变什么,怎么现在会这样?还出来个 InitializeComponent啥啥的。。
查看完整描述

3 回答

?
杨魅力

TA贡献1811条经验 获得超6个赞

因为那个循环吧,在开发的时候,其实IDE是会运行load的代码的,貌似是这个意思。。。
查看完整回答
反对 回复 2018-12-09
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

Form1不是静态类,Winform分为设计时和运行时,默认地,designer.cs中的代码是由DesignerSerializer按字母的顺序生成的,当你拖控件时会执行序列化,当你打开Designer的时候,它会去反序列化,你自己写的无关代码并不能被反序列化,所以打开Designer出错,但是,编译可能会通过,运行也OK。尽量不要改动Designer.cs中的代码。
查看完整回答
反对 回复 2018-12-09
?
烙印99

TA贡献1829条经验 获得超13个赞

应该是循环的问题,现实的问题是拖动控件并不能满足所有的需求,所以才动手去修改Designer.cs对于我们这些新手来说,最好全都可以图形化处理,减少手动代码。
查看完整回答
反对 回复 2018-12-09
  • 3 回答
  • 0 关注
  • 474 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信