绑定列表框索引C# [英] bind listbox to index C#

查看:124
本文介绍了绑定列表框索引C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是可能的,但我要的是这样的事情
的WinForm ListBox1中有行

I dont know if it possible but what I want is something like that In WinForm listbox1 has a list of lines(copied from file)

列表(从文件复制)在另一个线程和I类在包含同一行,每行我解析和的DoSomething
一旦我完成与该行我想在列表框中的指数变化

In another Thread and class I run on a List that contains the same lines each line I parse and DoSomething once I finish with that line I want the index in the listbox to change

这是我的基本的和有限的理解我方法是用事件在形式和比可能使用调用(不交叉线)

from my basic and limited understanding my approach was with an Event to fire in form and than maybe using Invoke (for not to cross thread )

对于火有以某种方式结合到列表框不知何故与我的为/的foreach 循环的索引的方法吗?

Is there is a way to somehow bind to index of the listbox somehow with my for/foreach loop ?

class form
{
    listBoxScript.SetSelected(ScriptCounter, true);<--bind the ScriptCounter?
}

在另一个类

class RunScript
{
//..

    public void RunScriptList()
    {
        ScriptCounter = 0 ;
        foreach ( var cell in ScriptList)
        {
        ScriptCounter ++;
        //DoSomething
        }
    }

}


推荐答案

请确保您在了RunScript 类实施 INotifyPropertyChanged的。下面是一个完整的示例:

Make sure you implement INotifyPropertyChanged in RunScript class. Here's a complete sample:

class RunScript : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int scriptCounter;

    private ISynchronizeInvoke invoker;
    public RunScript(ISynchronizeInvoke invoker)
    {
        if (invoker == null) throw new ArgumentNullException("invoker");
        this.invoker = invoker;
    }

    public async void RunScriptList()
    {
        ScriptCounter = 0;
        foreach (var cell in Enumerable.Range(1, 15))
        {
            ScriptCounter++;
            await Task.Delay(1000);
            //DoSomething
        }
    }

    public int ScriptCounter
    {
        get { return scriptCounter; }
        set
        {
            scriptCounter = value;
            OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            Action action = () => handler(this, new PropertyChangedEventArgs(propertyName));
            invoker.Invoke(action, null);
        }
    }
}

private RunScript rs;
public Form1()
{
    InitializeComponent();

    rs = new RunScript(this)

    var binding = new Binding("SelectedIndex", rs, "ScriptCounter", false, DataSourceUpdateMode.OnPropertyChanged);
    listBox1.DataBindings.Add(binding);
}

private void button1_Click(object sender, EventArgs e)
{
    rs.RunScriptList();
}

请注意我用异步/的await RunScriptList ,如果你做的,你需要火在主线程的PropertyChanged 事件的另一个线程避免交叉线异常。

Note I have used async/await in RunScriptList, If you do it in another thread you need to fire PropertyChanged event in main thread to avoid cross thread exception.

这篇关于绑定列表框索引C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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