ITPub博客

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

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

作者:germo 时间:2019-08-30 11:56:30 0 删除 编辑

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

 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啥啥的。。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1868/viewspace-3122785/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-17

  • 博文量
    209
  • 访问量
    8226