110,534
社区成员
发帖
与我相关
我的任务
分享
一时手误把 mycombox 的创建拖到在了显示 combobox 的循环之外~
因为我当时想,在循环之外就是一次创建多次使用,比每次都要创建新的要省资源~
这样实际上每次加入进去的都是同一个mycombox对象实例,只是当时的值不一样~
不过还有一个问题让我觉得很困惑:
1、既然是同一个对象实例,为何可以多次ADD到combobox里?
2、而且每一次ADD进去之后,值还不一样?ADD是把对象复制进去还是 关联 进去?
3、既然显示在combobox里的的内容项是各不相同的,为何重新转换成 mycombox 类时,又变成了有重复的?
望各位帮忙解惑,谢谢~~~
foreach (object obj in comboBox1.Items)
{
textBox1.Text += obj.value.ToString() + "\r\n";
}
foreach (object obj in comboBox1.Items)
{
textBox1.Text += obj.value.ToString() + "\r\n";
}
0 ◆ 作为顶级模块
1 ◆ 首页
2 ◆ 关于我们
3 ◆ 新闻中心
8 └◆ 公司新闻
9 └◆ 行业新闻
4 ◆ 产品中心
12 └◆ JQ28型
13 └◆ JQ35型
16 └◆ JQ41型
17 └◆ JQ45型
18 └◆ JQ48型
19 └◆ JQ57型
20 └◆ JQ66型
21 └◆ JQ86型
22 └◆ 开关电源型
23 └◆ 线架
5 ◆ 公司相册
10 └◆ 企业文化
11 └◆ 工厂设施
6 ◆ 企业论坛
7 ◆ 留言反馈
14 ◆ 经营范围
0 ◆ 作为顶级模块
14 ◆ 经营范围
14 ◆ 经营范围
14 ◆ 经营范围
9 └◆ 行业新闻
9 └◆ 行业新闻
14 ◆ 经营范围
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
23 └◆ 线架
14 ◆ 经营范围
11 └◆ 工厂设施
11 └◆ 工厂设施
14 ◆ 经营范围
14 ◆ 经营范围
14 ◆ 经营范围
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ceshi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "mycomboxclass mycomboboxclass1 = new mycomboxclass();\r\nfor (int i = 0; i < 10; i++)\r\n{\r\n mycomboboxclass1.text = i.ToString();\r\n mycomboboxclass1.value = i;\r\n textBox1.Text += \"[\" + mycomboboxclass1.text + \"]\";\r\n comboBox1.Items.Add(mycomboboxclass1);\r\n}\r\n---------------------\r\n";
mycomboxclass mycomboboxclass1 = new mycomboxclass();
for (int i = 0; i < 10; i++)
{
mycomboboxclass1.text = i.ToString();
mycomboboxclass1.value = i;
textBox1.Text += "[" + mycomboboxclass1.text + "]";
comboBox1.Items.Add(mycomboboxclass1);
}
textBox1.Text += "\r\n";
textBox1.Text += "foreach (object obj in comboBox1.Items)\r\n{\r\n textBox1.Text += obj.ToString()+\"\\r\\n\";\r\n}\r\n";
foreach (object obj in comboBox1.Items)
{
textBox1.Text += obj.ToString()+"\r\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
textBox1.Text = "comboBox1.Items.Clear();\r\nfor (int i = 0; i < 10; i++)\r\n{\r\n mycomboxclass mycomboboxclass1 = new mycomboxclass();\r\n mycomboboxclass1.text = i.ToString();\r\n mycomboboxclass1.value = i;\r\n textBox1.Text += \"[\" + mycomboboxclass1.text + \"]\";\r\n comboBox1.Items.Add(mycomboboxclass1);\r\n}\r\n---------------------\r\n";
for (int i = 0; i < 10; i++)
{
mycomboxclass mycomboboxclass1 = new mycomboxclass();
mycomboboxclass1.text = i.ToString();
mycomboboxclass1.value = i;
textBox1.Text += "[" + mycomboboxclass1.text + "]";
comboBox1.Items.Add(mycomboboxclass1);
}
textBox1.Text += "\r\n";
textBox1.Text += "foreach (object obj in comboBox1.Items)\r\n{\r\n textBox1.Text += obj.ToString()+\"\\r\\n\";\r\n}\r\n";
foreach (object obj in comboBox1.Items)
{
textBox1.Text += obj.ToString() + "\r\n";
}
}
}
public class mycomboxclass
{
public Int32 value;
public string text;
public override string ToString()
{
return text;
}
}
}
comboBox1.Items.Clear();
mycomboxclass mycomboboxclass1 = new mycomboxclass();
for (int i = 0; i < 10; i++)
{
mycomboboxclass1.text = i.ToString();
mycomboboxclass1.value = i;
textBox1.Text += "[" + mycomboboxclass1.text + "]";
comboBox1.Items.Add(mycomboboxclass1);
}
---------------------
[0][1][2][3][4][5][6][7][8][9]
foreach (object obj in comboBox1.Items)
{
textBox1.Text += obj.ToString()+"\r\n";
}
9
9
9
9
9
9
9
9
9
9
comboBox1.Items.Clear();
for (int i = 0; i < 10; i++)
{
mycomboxclass mycomboboxclass1 = new mycomboxclass();
mycomboboxclass1.text = i.ToString();
mycomboboxclass1.value = i;
textBox1.Text += "[" + mycomboboxclass1.text + "]";
comboBox1.Items.Add(mycomboboxclass1);
}
---------------------
[0][1][2][3][4][5][6][7][8][9]
foreach (object obj in comboBox1.Items)
{
textBox1.Text += obj.ToString()+"\r\n";
}
0
1
2
3
4
5
6
7
8
9