将选定的组合框项目放入标签 [英] Put selected combobox item into label

查看:55
本文介绍了将选定的组合框项目放入标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我遇到错误,无法解决。我试图在组合框中选择一个对象并将其名称显示在标签中。

Hello I am having an error and I can't figure it out. I am trying to select an object in my combobox and display the objects name into a label.

这是我的课程:

class Film
    {
        public string  Naam { get; set; }
        public Film(string naam)
        {
            Naam = naam;
        }
        public override string ToString()
        {
            return $"{Naam}";
        }
    }

这是我表格的代码

public partial class Form1 : Form
    {
        List<Film> filmlijst;
        public Form1()
        {
            InitializeComponent();
            filmlijst = new List<Film>();
        }
        private void button1_Click(object sender, EventArgs e)
        {  
            Film film = new Film(textBox1.Text);

                filmlijst.Add(film);
                comboBox1.DataSource = null;
                comboBox1.DataSource = filmlijst;
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                label1.Text = ((Film)comboBox1.SelectedItem).Naam;
        }
    }

所以我创建了一个带有文本框的Film对象给它起个名字。
如果我想添加多部电影,没有以下代码行,我的组合框将不会重新加载:

So I create an object of Film with a Textbox to give it a name. If I want to add multiple Films my combobox won't reload without this line of code :

comboBox1.DataSource = null;

没有这一行代码,我可以在TextLabel中看到对象的名称。
但是我不能添加更多对象,因为它不会重新加载。

Without this line of code I can see the name of my object in the TextLabel. But then I can't add more object because it doesn't reload.

推荐答案

您需要使用 BindingSource 以便使组合知道列表的更改,并在添加项目时将其添加到 BindingSource

You need to use a BindingSource to let the combo be aware of the changes to your list and when add items add them to the BindingSource

首先声明在类级别上的BindingSource 变量

public partial class Form1 : Form
{
     BindingSource bs = new BindingSource();
     ....

然后在表单构造函数中

public Form1()
{
    filmlijst = new List<Film>();
    bs.DataSource = filmlijst;
    comboBox1.DataSource  = bs;
}

最终将项目添加到 BindingSource 中,而不是直接添加到您的列表中

finally add items to the BindingSource and not directly to your list

    private void button1_Click(object sender, EventArgs e)
    {  
        Film film = new Film(textBox1.Text);
        bs.Add(film);
    }

这是从文档中有关BindingSource的备注部分

This is from the remark section of the docs about the BindingSource


BindingSource组件有很多用途。首先,它通过在Windows窗体控件
和数据源之间提供货币管理,
更改通知和其他服务,简化了表单上
到数据的绑定控件。这是通过使用DataSource属性将BindingSource
组件附加到数据源来实现的。

The BindingSource component serves many purposes. First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources. This is accomplished by attaching the BindingSource component to your data source using the DataSource property.

这篇关于将选定的组合框项目放入标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆