从列表框中转换字符串以分隔文本字段 [英] Cast string from a list box to separate text fields

查看:21
本文介绍了从列表框中转换字符串以分隔文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个程序,该程序允许我通过从一组旅行中选择数据来从中提取数据,并将所有找到的项目作为该旅行的一部分填充到列表框中.当我从列表框中选择一个项目时,我希望它填充一系列允许我编辑每个字段的文本框.

I'm building a program that would allow me to pull data from by selecting it from a group of Trips, and populate all found items as part of that Trip into a listbox. When I select an item from the listbox, I want it to populate a series of textboxes that allow me to edit each field.

这是我的问题代码在表单中的位置(tripChoose 是组合框,listExpenses 是列表框):

Here's where my problem code lies within the Form (tripChoose is the combo box and listExpenses is the list box):

private void tripChoose_SelectedIndexChanged(object sender, EventArgs e)
{
    IEnumerable<TripExpense> selectedExpenses = roster.ToFind((string)tripChoose.SelectedItem);
    foreach (TripExpense item in selectedExpenses)
        listExpenses.Items.Add(item);
}

private void listExpenses_SelectedIndexChanged(object sender, EventArgs e)
{
    specificExpenses = (TripExpense)roster.TripFind((string)listExpenses.SelectedItem);
    tripTextBox.Text = specificExpenses.Trip;
    tripTextBox.Enabled = false;
    descriptionTextBox.Text = specificExpenses.Description;
    amountTextBox.Text = specificExpenses.Amount.ToString();
    paymentMethodTextBox.Text = specificExpenses.PaymentMethod;
    dateExpenseTimePicker.Value = specificExpenses.Date;
    dateExpenseTimePicker.Enabled = true;
    noteTextBox.Text = specificExpenses.Note;
}

JIT 调试器让我知道我隐式地将对象转换为字符串

The JIT debugger lets me know that I implicitly convert an object to a string

我尝试过使用这样的 ToString 方法:

I've tried using a ToString method like this:

private void tripChoose_SelectedIndexChanged(object sender, EventArgs e)
{
    IEnumerable<TripExpense> selectedExpenses = roster.ToFind((string)tripChoose.SelectedItem);
    foreach (TripExpense item in selectedExpenses)
    listExpenses.Items.Add(item.ToString());
}

我认为我在正确的轨道上,因为我收到一个错误,告诉我对象引用未设置为对象的实例.

I think I'm on the right track there, as I get an error telling me that the object reference is not set to an instance of an object.

推荐答案

我认为问题在于 TripExpenseToString() 方法的实现.当您使用其中的字符串并将其输入 FindTrip 时,它再也找不到它了.

I believe the issue is the implementation of the ToString() method in TripExpense. When you use the string out of that and feed it into the FindTrip it cannot find it anymore.

你可以做的是拥有组合项的原始设置并修改SelectedIndexChanged

What you can do instead is to have your original setting of the combo items and modify the SelectedIndexChanged instead

private void tripChoose_SelectedIndexChanged(object sender, EventArgs e)
{
    IEnumerable<TripExpense> selectedExpenses = roster.ToFind((string)tripChoose.SelectedItem);
    foreach (TripExpense item in selectedExpenses)
        listExpenses.Items.Add(item);
}

private void listExpenses_SelectedIndexChanged(object sender, EventArgs e)
{
    specificExpenses = (TripExpense)listExpenses.SelectedItem;
    ... 
}

这样,ComboBox 文本仍会获取 TripExpense 对象的 ToString(),但您仍然可以从 SelectedItem 获取实际对象 使用而不是尝试重新查询对象.

This way the ComboBox text will still grab the ToString() of the TripExpense object but you will still be able to get the actual object from the SelectedItem to play with rather than trying to requery for the object.

这篇关于从列表框中转换字符串以分隔文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆