【问题标题】:Edit the value of a property in a listBox if selected?如果选中,编辑列表框中的属性值?
【发布时间】:2012-10-22 10:16:44
【问题描述】:

我希望允许用户通过双击并像重命名模式一样在 listBox 中输入或通过消息框来编辑 listBox 字符串的一部分,这是真的。该值是双精度值,我希望结果同时反映在 listBox 和实际列表中。

这是我的清单,我需要修改费用:

        public BindingList<Edge> edgeList = new BindingList<Edge>();

这是我的 ToString 函数:

        public override string ToString()
        {
            return string.Format("Cost: {0}, Start: {1}, End: {2}", (int)_cost, _start, _end);
        }

这将添加到我的列表中:

pfi.ni.edgeList.Add(e1);

这就是我在列表框中出现新属性时添加新属性的方式:

listBox2.Items.Add(e1);

我已经为我的 listBox 设置了 selectedIndexChanged 事件并且可以获取它,因此它会显示单击属性的成本,我猜它现在只是在编辑它。

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Edge selectedItem = listBox1.SelectedItem as Edge;

        result = MessageBox.Show(string.Format("Cost: {0}", e1._cost));
        //selectedItem._cost doesn't work for some reason, so I use e1._cost 
        //until I can figure it out
    }

【问题讨论】:

    标签: c# list listbox edit


    【解决方案1】:
    Edge selectedItem = listBox1.SelectedItem as Edge;
    selectedItem._cost = inputTextBox.Text;
    listBox1.Refresh();
    

    【讨论】:

    • 嗨,谢谢。目前,它在我的 listBox 和我的列表中将我的成本更改为 1,所以它是一些东西,但它并没有更改为我输入的数字。我的成本是双倍的,所以我这样转换: selectedItem._cost = Convert.ToDouble(MessageBox.Show(inputText.Text));
    • 没关系,取出 MessageBox.Show 解决了这个问题。我认为它返回 1 因为可能 OK 返回 1?我不确定,但如果没有弹出窗口,它可以正常工作,干杯!
    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多