从TextBox更新第二列listView已更改 - c#

时间:2013-03-08 15:04:28

标签: c# winforms listview textbox

使用此代码,我可以填充我的列表视图:

foreach (string elements in Properties.Settings.Default.Items)
{
    ListViewItem lvi = new ListViewItem();

    var txt = elements;

    if (!listView1.Items.ContainsKey(txt))
    {
        lvi.Text = txt;


        lvi.Name = txt;

        lvi.SubItems.Add("");
        lvi.SubItems.Add("1.0");

        listView1.Items.Add(lvi);
    }
}

这是结果:

enter image description here

如何从文本框更改的事件中仅更新第二列(总价格)? (Windows窗体,Visual Studio 2012)

1 个答案:

答案 0 :(得分:1)

一些属性

        listView1.HideSelection = false;
        listView1.MultiSelect = false;
        listView1.FullRowSelect = true;
        listView1.View = View.Details;

我认为这就是你要求的:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox txt_sender = (TextBox)sender;

        if (listView1.SelectedItems.Count > 0)
        {
            listView1.SelectedItems[0].SubItems[1].Text = txt_sender.Text;
        }
    }

我还建议:(只允许数字和一个。)

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        TextBox txt_sender = (TextBox)sender;

        if (!char.IsControl(e.KeyChar) &&
            !char.IsDigit(e.KeyChar) &&
            !((e.KeyChar == '.') && !(txt_sender).Text.Contains('.')))
        {
            e.Handled = true;
        }
    }