首页 新闻 会员 周边

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

0
悬赏园豆:15 [已解决问题] 解决于 2015-11-17 10:39

这样一段代码为什么设计页面要报错?

 

 1 using System.Windows.Forms;
 2 using System.Collections.Generic;
 3 namespace WFAListViewer
 4 {
 5     partial class Form1
 6     {
 7         /// <summary>
 8         /// Required designer variable.
 9         /// </summary>
10         private System.ComponentModel.IContainer components = null;
11 
12         /// <summary>
13         /// Clean up any resources being used.
14         /// </summary>
15         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
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         /// <summary>
28         /// Required method for Designer support - do not modify
29         /// the contents of this method with the code editor.
30         /// </summary>
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啥啥的。。
是绵羊在漫步的主页 是绵羊在漫步 | 初学一级 | 园豆:59
提问于:2015-11-13 14:24
< >
分享
最佳答案
0

因为那个循环吧,在开发的时候,其实IDE是会运行load的代码的,貌似是这个意思。。。

收获园豆:13
顾晓北 | 专家六级 |园豆:10844 | 2015-11-13 14:37

谢谢,是的呢。

是绵羊在漫步 | 园豆:59 (初学一级) | 2015-11-17 10:38

你们豆子这么多,全都是答题得来的吗?

是绵羊在漫步 | 园豆:59 (初学一级) | 2015-11-17 10:39

@张生抛了崔莺莺: 理论上是的,园子默认会有200的园豆吧,然后回复博问得到的,不知道是不是其他途径也可以,你可以在闪存里@dudu一下,他是园长,不知道规则,只知道回复问题是肯定可以得到园豆的,其他的,比如博客有多少人点赞就不知道了。。。

顾晓北 | 园豆:10844 (专家六级) | 2015-11-17 10:58

@顾晓北: 谢谢:)

是绵羊在漫步 | 园豆:59 (初学一级) | 2015-11-17 13:08
其他回答(1)
0

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

收获园豆:2
jello chen | 园豆:7306 (大侠五级) | 2015-11-13 17:39

应该是循环的问题,现实的问题是拖动控件并不能满足所有的需求,所以才动手去修改Designer.cs对于我们这些新手来说,最好全都可以图形化处理,减少手动代码。

支持(0) 反对(0) 是绵羊在漫步 | 园豆:59 (初学一级) | 2015-11-17 10:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册