从字典中插入ListBox,我需要获取值和密钥分开

时间:2011-10-30 23:26:55

标签: c# winforms dictionary listbox key

我正在尝试在我的应用程序中制作收藏夹,我需要在ListBox中显示一个具有收藏夹名称的表单。 当选择一个名称并单击“选择”按钮时,我想将名称和Url放在不同的文本框中,这是我的代码中的任何想法。

       private void EditFourites_Click(object sender, EventArgs e)
    {
        textBox1.Text = listBox1.SelectedItem.ToString();//put the selected value from the listbox to the textbox1 and this is my problem i want to make textbox1.text takes only the key of the listbox1.text


    }

    private void ListBox() 
    {
        FavoriteXml FV = new FavoriteXml();//the favouritXml class is a class where i get the information about favourites
        Dictionary<string, string> Favourite = FV.GetFavouriteCombo();//GetFavouriteCombo() will get the value as dictionary

        listBox1.DataSource = new BindingSource(Favourite, null);

        listBox1.ValueMember = "Value";
    }

     private void EditSpecificFavourite_Click(object sender, EventArgs e)
    {
        FavoriteXml FV = new FavoriteXml();
        FV.EditFavourite(textBox1.Text.ToString(),textBox2.Text.ToString());//this is the EditFavourite where i want to change the specific favourite
        ListBox();
    }

1 个答案:

答案 0 :(得分:0)

要将所选listboxitem的数据值放入第二个文本框,请使用以下代码:

textBox2.Text = listBox1.SelectedValue.ToString();