اضافه کردن آیتم به لیست باکس در C# How might I add an item to a ListBox?

Rate this post
list.Items.add(new ListBoxItem("name", "value"));

The internal (default) data structure of the ListBox is the ListBoxItem.


You can choose what do display using the DisplayMember of the ListBox.

List<SomeData> data = new List<SomeData>();
data.Add(new SomeData() { Value = 1, Text= "Some Text"});
data.Add(new SomeData() { Value = 2, Text = "Some Other Text"});
listBox1.DisplayMember = "Text";
listBox1.DataSource = data;

When the user selects an item, you can read the value (or any other property) from the selected object:

int value = (listBox1.SelectedItem as SomeData).Value;

Update: note that DisplayMember works only with properties, not with fields, so you need to alter your class a bit:

public class SomeData
{
    public string Value { get; set; };
    public string Text { get; set; };
}
بیشتر بخوانید:   split ، تقسیم و جدا کردن یک متن بر اساس یک کاراکتر

Check Also

c# Shrp Programing

تغییر زبان اتوماتیک بصورت اتوماتیک از فارسی به انگلیسی و برعکس در نرم افزار

فرض کنید که دو نرم افزاری که دارید مینویسید چند تا   TextBox بر روی فرم …

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *