我动态了一个label控件,代码如下Label lb1 = new Label();
lb1.Text = comboBox2.Text;
lb1.Size = new System.Drawing.Size(84, 14);
pictureBox1.Controls.Add(lb1);
lb1.BackColor = Color.Yellow;
lb1.Location = new Point(115, 250);
lb1.Update();
我单步调试,发现label中的text值有改变,但是在界面上显示则没变化,请问这是什么问题?
lb1.Text = comboBox2.Text;
lb1.Size = new System.Drawing.Size(84, 14);
pictureBox1.Controls.Add(lb1);
lb1.BackColor = Color.Yellow;
lb1.Location = new Point(115, 250);
lb1.Update();
我单步调试,发现label中的text值有改变,但是在界面上显示则没变化,请问这是什么问题?
2. 如果Label是固定要显示的,那最简单的办法是在设计时就在指定位置放一个Label。然后在Form_Load时设置其Visiable为false,从而隐藏它。之后在这个Button的Click事件里设置Label显示,并设置其他属性即可。
第二,我一开始也是使用固定的label来显示,但是combobox绑定了数据库,而label又要跟combobox同步,那这样的话,把label写死了就达不到预期的效果了
可以,因为每次Label的Text都是从ComboBox.Text取出来的第二,我一开始也是使用固定的label来显示,但是combobox绑定了数据库,而label又要跟combobox同步,那这样的话,把label写死了就达不到预期的效果了
1. 你上面的代码是点Button后才把Label的文本设置为ComboBox的。如果要Combox改变选项,Label的文本就改变,请映射ComboBox的SelectedChanged事件。
2. 你的Label出现在固定位置,固定大小,文本也是固定取自ComboBox。本身就是固定的
private void readDB_Load(object sender, EventArgs e)
{
using (NorthwindEntities db = new NorthwindEntities())
{
List<string> customernames = db.Customers.Select(c => c.ContactName).Distinct().ToList<string>();
this.comboBox1.DataSource = customernames;
this.comboBox1.SelectedIndex = 0;
}
}这是按钮点击事件的代码:
private void button1_Click(object sender, EventArgs e)
{
using (NorthwindEntities db = new NorthwindEntities())
{
Customer customer = db.Customers
.Where(c => c.ContactName == this.comboBox1.SelectedValue)
.FirstOrDefault(); this.label1.Text = customer.Country;
}
}