目前我有一个带有三个硬编码项目的组合框。 每个项目带有2个值。我使用switch case语句来获取每个项目的值,具体取决于选择的项目。
Switch(combobox.selectedindex)
{
case 0: // Item 1 in combobox
a = 100;
b = 0.1;
break;
case 1: // Item 2 in combobox
a = 300;
b = 0.5;
break;
//and so on....
}
我尝试添加一项功能,允许用户使用输入的a和b值将更多项目添加到组合框中。如何在每种情况条件下动态添加案例语句并定义值?我已经看过使用数据表,但我不知道如何在选择一个项目时从数据表中获取多个valuemembers。
此外,我想将用户添加的项目及其相应的值保存到.dat文件中。因此,当重新打开程序时,它将能够加载用户从文件中添加的项目列表。我考虑过使用streamwriter和readline,但我不确定如何完成。
答案 0 :(得分:1)
您可以使用DataSource在组合框上使用Binding。 ComboBox还可以绑定到除Primitive值之外的其他内容(字符串/ int /硬编码值)。因此,您可以创建一个小类来表示您在switch语句中设置的值,然后使用DisplayMember说明哪个属性应该在组合框中可见。
这种基本类的一个例子可能是
public class DataStructure
{
public double A { get; set; }
public int B { get; set; }
public string Title { get; set; }
}
由于您正在讨论用户动态地向组合框添加值,您可以使用包含单独类的BindingList,此BindingList可以是您的类中的受保护字段,当用户添加一个时,您可以向其添加新DataStructure ,然后使用您添加的新值自动更新组合框。
ComboBox的设置可以在Form_Load中完成,也可以在Form Constructor中完成(在InitializeComponent()调用之后),如下所示:
// your form
public partial class Form1 : Form
{
// the property contains all the items that will be shown in the combobox
protected IList<DataStructure> dataItems = new BindingList<DataStructure>();
// a way to keep the selected reference that you do not always have to ask the combobox, gets updated on selection changed events
protected DataStructure selectedDataStructure = null;
public Form1()
{
InitializeComponent();
// create your default values here
dataItems.Add(new DataStructure { A = 0.5, B = 100, Title = "Some value" });
dataItems.Add(new DataStructure { A = 0.75, B = 100, Title = "More value" });
dataItems.Add(new DataStructure { A = 0.95, B = 100, Title = "Even more value" });
// assign the dataitems to the combobox datasource
comboBox1.DataSource = dataItems;
// Say what the combobox should show in the dropdown
comboBox1.DisplayMember = "Title";
// set it to list only, no typing
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
// register to the event that triggers each time the selection changes
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
// a method to add items to the dataItems (and automatically to the ComboBox thanks to the BindingContext)
private void Add(double a, int b, string title)
{
dataItems.Add(new DataStructure { A = a, B = b, Title = title });
}
// when the value changes, update the selectedDataStructure field
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combo = sender as ComboBox;
if (combo == null)
{
return;
}
selectedDataStructure = combo.SelectedItem as DataStructure;
if (selectedDataStructure == null)
{
MessageBox.Show("You didn't select anything at the moment");
}
else
{
MessageBox.Show(string.Format("You currently selected {0} with A = {1:n2}, B = {2}", selectedDataStructure.Title, selectedDataStructure.A, selectedDataStructure.B));
}
}
// to add items on button click
private void AddComboBoxItemButton_Click(object sender, EventArgs e)
{
string title = textBox1.Text;
if (string.IsNullOrWhiteSpace(title))
{
MessageBox.Show("A title is required!");
return;
}
Random random = new Random();
double a = random.NextDouble();
int b = random.Next();
Add(a, b, title);
textBox1.Text = string.Empty;
}
}
像这样,您可以随时使用所选项目,您可以从所选属性中请求值,并且您不必担心将ComboBox与当前可见的项目同步
答案 1 :(得分:0)
虽然ComboBox通常用于显示文本项,但您可以将任何对象添加到ComboBox。通常,ComboBox中对象的表示形式是该对象的ToString方法返回的字符串。如果要显示对象的成员,请通过将DisplayMember属性设置为相应成员的名称来选择将显示的成员。您还可以通过设置ValueMember属性来选择将表示对象返回的值的对象成员。有关更多信息,请参阅ListControl。
因此,您只需将包含所有信息的对象直接添加到ComboBox的Items
集合中即可。稍后,检索SelectedItem属性并将其强制转换为正确的类型。