0

i have a listbox that adds the ids from a dictionary to the listbox but i need it to display the name and not the id but the value of the item has to be the id is there any way to do this heres the code i use

    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()));
        }
    }

i included all code that i think is necessary

2 Answers 2

1

If i understand you correctly you can set items.Tag property your id and Text property your Value. Actually this is purpose of the Tag property.To do this you can consider using ListView instead of listBox.

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

Then when you need get item id:

int id = (int)listView1.SelectedItems[0].Tag;
Sign up to request clarification or add additional context in comments.

2 Comments

for some reason my comments werent working with your code i get this error Error 18 'System.Windows.Forms.ListView' does not contain a definition for 'SelectedItem' and no extension method 'SelectedItem' accepting a first argument of type 'System.Windows.Forms.ListView' could be found (are you missing a using directive or an assembly reference?)
nvm fixed that error by doing this int id = (int)listView1.SelectedItems[0].Tag;
1

You should look at the displayMember and valueMember properties of the ListBox, and bind it to a collection rather than adding the elements individually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.