【问题标题】:ListBox insert from a dictionary and i need to get the value and the key seperateListBox 从字典中插入,我需要单独获取值和键
【发布时间】:2011-12-18 10:02:54
【问题描述】:

我正在尝试在我的应用程序中创建收藏夹,我需要在 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();
    }

【问题讨论】:

    标签: c# winforms dictionary listbox key


    【解决方案1】:

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

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

    【讨论】:

    • 好的,这项工作,让我选择的值,但我想把钥匙也放在另一个文本框中
    • key是什么意思?一个列表框项只能包含 2 个值,一个是您可以看到的,一个是“SelectedValue”。
    • 我的意思是当我为列表框绑定源时,我从字典中插入两个值,键和值在你的答案中,我只得到我想要的字典键的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多