Update second column listView from TextBox changed

2019-08-11 11:21发布

With this code I can populate my listview:

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);
    }
}

this is result:

enter image description here

How can I update only second column(Total Price) from textbox changed event? (Windows Forms, Visual Studio 2012)

1条回答
叛逆
2楼-- · 2019-08-11 12:03

Some properties

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

I think this is what you are asking for:

    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;
        }
    }

May I also suggest: (only allow numbers and one .)

    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;
        }
    }
查看更多
登录 后发表回答