简体   繁体   中英

c# winform listbox adds system.collection to listbox

to whom it may respond to , we have 2 listboxes that use dictionaries as datasource. The code to swap element between textboxes is as :

private void btnSrc2Trg_Click(object sender, EventArgs e)
{
    if (lbxSourceSite.Items.Count > 0 ) {
        try { 
            string[] item = lbxSourceSite.Items[lbxSourceSite.SelectedIndex].ToString().Replace("[","").Split(',');
            dctTargetsites.Add(item[0], item[1]);
            dctSites.Remove(item[0]);
            lbxSourceSite.DataSource = null;
            lbxSourceSite.DataSource = new BindingSource(dctSites,null);
            lbxSourceSite.DisplayMember = "Key";
            lbxSourceSite.ValueMember = "Value";
            lbxTargetSite.DataSource = null;
            lbxTargetSite.DataSource =  new BindingSource(dctTargetsites, null);
            lbxTargetSite.DisplayMember = "Key";
            lbxTargetSite.ValueMember = "Value";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.GetBaseException().Message.ToString());
        }
    }

}

private void btnTrg2Src_Click(object sender, EventArgs e)
{
    if (lbxTargetSite.Items.Count > 0 ) {
        try{
            string[] item = lbxTargetSite.Items[lbxTargetSite.SelectedIndex].ToString().Replace("[", "").Split(',');
            dctSites.Add(item[0], item[1]);
            dctTargetsites.Remove(item[0]);

            lbxTargetSite.DataSource = null;
            lbxTargetSite.DataSource = new BindingSource(dctTargetsites, null);
            lbxTargetSite.DisplayMember = "Key";
            lbxTargetSite.ValueMember = "Value";

            lbxSourceSite.DataSource = null;
            lbxSourceSite.DataSource = new BindingSource(dctSites, null);
            lbxSourceSite.DisplayMember = "Key";
            lbxSourceSite.ValueMember = "Value";
        }catch(Exception ex){
            MessageBox.Show(ex.GetBaseException().Message.ToString());              
        }
    }
}

After adding the last element to target textbox and removing from source textbox (or vice versa) , there an element as displayed as "system.collection" , it might be related to re-binding dictionaries as datasources after operation, how can I get rid of it? Or any other proper method to use dictionaries for listbox is welcomed, Thank you for your concern

The displayed element "system.collection" may occur becaus you are using

string[] item = lbxSourceSite.Items[lbxSourceSite.SelectedIndex].ToString().Replace("[","").Split(',');

the ToString() method will return you the name of the data type of the selected item -> system.collection.

You could use:

string[] item = lbxSourceSite.Items[lbxSourceSite.SelectedIndex].Text.Replace("[","").Split(',');

or even:

string[] item = lbxSourceSite.Items[lbxSourceSite.SelectedIndex].Value.ToString().Replace("[","").Split(',');

You have to change this in both listboxes :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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