额 我在一个groupbox中动态添加了几个combobox,在界面中下拉选择一个 为什么其他的combobox也会跟着动 ?求指教 是不是某些属性没有设置好啊
?
代码如下
string[] TitleNames = new string[] { "报告来源", "报告分类", "行业分类", "作者", "推荐" }; int intConuts = 5; private void InitContorl() { //RadioButton[ ] rb = new RadioButton[intConuts]; Label[] lb = new Label[intConuts]; ToolTip toolTip1 = new ToolTip(); toolTip1.AutoPopDelay = 5000; toolTip1.InitialDelay = 1000; toolTip1.ReshowDelay = 500; toolTip1.ShowAlways = true; ComboBox[] cb = new ComboBox[intConuts]; for (int i = 0; i < intConuts; i++) { lb[i] = new Label(); lb[i].Name = "titleLable" + i.ToString(); lb[i].Text = "标题字2222222222222222222222222222222222222222222222222222222222222222222222222段" + (i + 1) + ":"; lb[i].AutoSize = true; toolTip1.SetToolTip(lb[i], lb[i].Text); lb[i].MaximumSize = new System.Drawing.Size(200, lb[i].Height); lb[i].AutoEllipsis = true; lb[i].Location = new Point(12, 20 + i * 30); cb[i] = new ComboBox(); cb[i].DataSource = TitleNames; cb[i].Name = "comboBoxName_" + i; cb[i].DisplayMember = "DisplayMember" + i; cb[i].ValueMember = "ValueMember" + i; cb[i].TabIndex = i; cb[i].FormattingEnabled = true; if (i == 0) cb[i].Location = new Point(lb[i].Location.X + 200, lb[i].Location.Y); else cb[i].Location = new Point(lb[i - 1].Location.X + 200, lb[i].Location.Y); } Button btnbBuildXml = new Button(); btnbBuildXml.Click += new EventHandler(btnbBuildXml_Click); this.groupBox1.Width = lb[0].Location.X + lb[0].Width + 250; this.groupBox1.Height = 15 + 30 * intConuts; this.groupBox1.Controls.AddRange(cb); this.groupBox1.Controls.AddRange(lb); }
可能是因为这5个checkbox绑定了同一个数据源吧,试试绑定不同的五个数据源。
但是我就是要实现五个数据源都一样的combobox,总不能我去new五个一样的对象吧,那多浪费内存?
楼上说的对,你把获得数据单独写成一个方法,然后使用的时候调用那个方法
楼上正解,数据源必须不是同一个对象才行,否则会联动。
支持