更改列表框中的文本而不更改值

时间:2013-12-16 09:36:35

标签: c# listbox

我有一个列表框,将字典中的id添加到列表框但是我需要它来显示名称而不是id但是项目的值必须是id是否有任何方法可以执行此操作我用

    public static Dictionary<int, CardInfos> CardDataeff = new Dictionary<int, CardInfos>();


    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        lock (Searchlock)
        {
            if (textBox2.Text == "")
            {
                listBox2.Items.Clear();
                return;
            }
            if (textBox2.Text != "Search")
            {
                listBox2.Items.Clear();
                foreach (int card in CardDataeff.Keys)
                {
                    if (CardDataeff[card].Id.ToString().ToLower().StartsWith(textBox2.Text.ToLower()) ||
                        CardDataeff[card].Name.ToLower().Contains(textBox2.Text.ToLower()))
                    {
                        listBox2.Items.Add(CardDataeff[card].Id.ToString());
                    }
                }
            }
        }
    }

  public void listBox2_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();

        bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);

        int index = e.Index;
        if (index >= 0 && index < listBox2.Items.Count)
        {
            string text = listBox2.Items[index].ToString();
            Graphics g = e.Graphics;

            CardInfos card = CardDataeff[Int32.Parse(text)];

            g.FillRectangle((selected) ? new SolidBrush(Color.Blue) : new SolidBrush(Color.White), e.Bounds);

            // Print text
            g.DrawString((card.Name == "" ? card.Id.ToString() : card.Name), e.Font, (selected) ? Brushes.White : Brushes.Black,
                listBox2.GetItemRectangle(index).Location);
        }

        e.DrawFocusRectangle();
    }

    private bool LoadCard(int cardid)
    {
        if (!CardDataeff.ContainsKey(cardid))
        {
            return false;
        }

        CardInfos info = CardDataeff[cardid];

        if (!string.IsNullOrEmpty(this.richTextBox1.Text))
        {
            if (MessageBox.Show("do you want to save? ", "Prompt", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                if (string.IsNullOrEmpty(this.filePath))
                {
                    this.saveFileDialog1.InitialDirectory = this.scriptFolderPath;
                    this.saveFileDialog1.FileName = this.getFileName();
                    if (this.saveFileDialog1.ShowDialog() != DialogResult.OK)
                        this.saveFile(this.saveFileDialog1.FileName);
                }
                else
                {
                    this.saveFile(this.filePath);
                }
            } 
        }
        this.openFile(cdbdir + "\\script\\c" + cardid + ".lua");
        return true;
    }


    private void listBox2_DoubleClick(object sender, EventArgs e)
    {
        ListBox list = (ListBox)sender;
        if (list.SelectedIndex >= 0)
        {
            LoadCard(Int32.Parse(list.SelectedItem.ToString()));
        }
    }

我包含了我认为必要的所有代码

2 个答案:

答案 0 :(得分:1)

您应该查看ListBox的displayMembervalueMember属性,并将其绑定到集合,而不是单独添加元素。

答案 1 :(得分:1)

如果我理解正确,你可以设置items.Tag属性你的id和Text属性你的值。实际上这是Tag属性的目的。为此,您可以考虑使用ListView而不是listBox。

ListViewItem item = new ListViewItem();
item.Text = CardDataeff[card].Name;
item.Tag = CardDataeff[card].Id
listView1.Items.Add(item);

然后当你需要获得项目ID:

int id = (int)listView1.SelectedItems[0].Tag;
相关问题