C# 更改列表框中的文本而不更改值
我有一个列表框,可以将字典中的id添加到列表框中,但是我需要它来显示名称,而不是id,但是项目的值必须是id,有什么方法可以这样做吗?这是我使用的代码C# 更改列表框中的文本而不更改值,c#,listbox,C#,Listbox,我有一个列表框,可以将字典中的id添加到列表框中,但是我需要它来显示名称,而不是id,但是项目的值必须是id,有什么方法可以这样做吗?这是我使用的代码 public static Dictionary<int, CardInfos> CardDataeff = new Dictionary<int, CardInfos>(); private void textBox2_TextChanged(object sender, EventArgs e)
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()));
}
}
publicstaticdictionary CardDataeff=newdictionary();
私有void textBox2_TextChanged(对象发送方,事件参数e)
{
锁(搜索锁)
{
如果(textBox2.Text==“”)
{
listBox2.Items.Clear();
返回;
}
如果(textBox2.Text!=“搜索”)
{
listBox2.Items.Clear();
foreach(CardDataeff.密钥中的int卡)
{
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(对象发送方,DrawItemEventArgs e)
{
e、 牵引杆接地();
bool selected=((e.State&DrawItemState.selected)=DrawItemState.selected);
int-index=e.index;
如果(索引>=0&&index=0)
{
LoadCard(Int32.Parse(list.SelectedItem.ToString());
}
}
我包含了我认为必要的所有代码您应该查看列表框的
displayMember
和valueMember
属性,并将其绑定到集合,而不是单独添加元素。如果我理解正确,您可以设置项。标记属性id和文本属性值。实际上,这是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;
由于某些原因,我的注释无法处理您的代码,我收到此错误,错误为18'System.Windows.Forms.ListView'不包含'SelectedItem'的定义,并且找不到接受'System.Windows.Forms.ListView'类型的第一个参数的扩展方法'SelectedItem'(是否缺少using指令或程序集引用?)nvm通过执行以下操作修复了该错误:int id=(int)listView1.SelectedItems[0]。标记;