简体   繁体   English

C#如何刷新列表框中的项目

[英]c# how do i refresh items in my listbox

I have a method that adds items to my listbox called refreshInterface which is called as soon as the programe starts, adding names of homeforms in the listbox using the FormItems class, here is the rereshInterface method below 我有一种方法将项目添加到我的列表框中,称为refreshInterface,程序启动后即会立即调用该方法,并使用FormItems类在列表框中添加格式的名称,这是下面的rereshInterface方法

    public void refreshInterface()
    {
        //int number = 0;
        foreach (DataSet1.xspGetAnalysisUsageTypesRow homeForms in myDataSet.xspGetAnalysisUsageTypes)
        {
            var forms = new FormItems(homeForms);
            listBox1.Items.Add(forms);
        }
    }

The FormItems class is this below FormItems类如下

public class FormItems
{
    public DataSet1.xspGetAnalysisUsageTypesRow types { get; set; }

    public FormItems(DataSet1.xspGetAnalysisUsageTypesRow usageTypes)
    {
        types = usageTypes;
    }

    public override string ToString()
    {
        // returns the rows that are relating to types.xlib_ID
        var libtyps = types.GetxAnalysisUsageRows();
        var cnt = 0;

        foreach (DataSet1.xAnalysisUsageRow ty in libtyps)
        {
            //returns true if ty is null
            bool typeNull = ty.Isxanu_DefaultNull();

            // if its false, if xanu_Default is set
            if (!typeNull)
            {
                cnt += 1;
            }
        }

        var ret = String.Format("set {0} [Set: {1}]", types.xlib_Desc, cnt);

        //return this.types.xlib_Desc;

        return ret;
    }
}

Each listbox (the listbox is on the left of the homeform) item has a number of reports that can be added to it, so for instance, i select an homeform from my listbox, there are 12 textboxes on the right hand side and each textbox has a pair of buttons which are Browse and Clear. 每个列表框(列表框位于homeform的左侧)都有许多可以添加的报告,因此,例如,我从列表框中选择一个homeform,右侧有12个文本框,每个文本框有一对按钮,分别是“浏览”和“清除”。 If I click on the browse button a new form appears, and i select a report from that form and add it to a particular textbox, the count for that homeform should update, and i clear a textbox for a particular homeform, the count should also update. 如果单击“浏览”按钮,将出现一个新表单,并且从该表单中选择一个报告并将其添加到特定的文本框中,该homeform的计数应更新,并且我清除特定homeform的文本框,该计数也应更新。

At the moment when i debug the application, it shows me the count of each Homeform depending on the amount of reports added to the homeform, but while the programe is running, if i add a new report to a homeform, the count does not update until i restart the debug session. 在我调试应用程序的那一刻,它显示给我每个Homeform的数量,具体取决于添加到该Homeform中的报告的数量,但是在程序运行时,如果我将新报告添加到Homeform中,则该计数不会更新直到我重新启动调试会话。 I was told about using a Databinding method but not sure of how i could use it here 有人告诉我使用数据绑定方法,但不确定如何在这里使用它

How do i ge my listbox item to update ? 如何让我的列表框项目更新?

您应该将列表框组件源绑定到Observable Collection,对Observable Collection进行的每次更新都会更新List Box数据。

Might not be exact but should give you an idea.

public void refreshInterface()
{
    Dictionary<int,string> items = new Dictionary<int,string>();

    //int number = 0;
    foreach (DataSet1.xspGetAnalysisUsageTypesRow homeForms in myDataSet.xspGetAnalysisUsageTypes)
    {
        var formitem = new FormItems(homeForms);
        items.Add(formitem.someprop, formitem.toString());

    }
    listbox.DataSource = items;
    listbox.DisplayMember = "Value";
    listbox.ValueMember = "Key";
}

You should probably look into binding. 您可能应该考虑绑定。 Here is a good place to start: 这是一个不错的起点:

http://www.codeproject.com/Articles/140621/WPF-Tutorial-Concept-Binding http://www.codeproject.com/Articles/140621/WPF-Tutorial-Concept-Binding

If you want a GUI to respond to data changes then binding is your best friend. 如果您希望GUI响应数据更改,那么绑定是您最好的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM