user1001074
user1001074

Reputation: 21

Using Lists With Textboxes

This is my code for adding to my list:

List<string> myList = new List<string>();

private void button1_Click(object sender, EventArgs e)
{
  ListViewItem myList = new ListViewItem(txtBox1.Text);
  myList.SubItems.Add(txtBox2.Text);
  myList.SubItems.Add(txtBox3.Text);
  myList.SubItems.Add(txtBox4.Text);
  listView1.Items.Add(myList);
  txtBox1.Text = "";
  txtBox2.Text = "";
  txtBox3.Text = "";
  txtBox4.Text = "";
}

This adds to my list and clears and lets me add another post to my list. The problem is I want to be able to repopulate the textboxes to allow me to update a post in the list.

To make my point more clear, my list looks like this:

Code | Name | Price | In stock
------------------------------
123    aa     122     2
124    bb     111     5

Say I want the first post, can I somehow set all the data back into the textboxes by that identifier or do I have search by index? I would like to be able to put the code in the first textbox then hit a button called retrieve that populates the other textfield kinda like going by the unique key in an sql table but I cant find any info on wether its possible or not.

Upvotes: 2

Views: 155

Answers (3)

Gage
Gage

Reputation: 7513

Why don't you create a class that has Code, Name, Price, In stock, and use a List

Public MyItem
{
public string Code;
public string Name,
public float Price;
public bool inStock;

}

List<MyItem> myList = new List<MyItem>();

private void button1_Click(object sender, EventArgs e)
{
  MyItem temp = new MyItem()
  temp.Code=txtBox1.Text;
  temp.Name=(txtBox2.Text);
  temp.Price=(txtBox3.Text);
  temp.inStock=(txtBox4.Text);
  mylist.add(temp);
  txtBox1.Text = "";
  txtBox2.Text = "";
  txtBox3.Text = "";
  txtBox4.Text = "";
}

If you implement it this way then you can just use the index to go through all the items in the list. With the example above you would probably need to convert some of the values from the text boxes before you can use them (price to double)

Also if you want to add the items to a Listview and display them a certain way then override the toString property in the MyItem class and you can display whatever text you want. Hope this helps.

Upvotes: 2

Stacker
Stacker

Reputation: 8237

I dont quite understand what you mean by saying "can I somehow set all the data back into the textboxes by (that identifier) " which identifier ?!

but i would get data like this:

var item = listview1.items.where(x=>x.SubItems[0].value == mycodeId).firstordefault();

now i have the item i can retrieve data from it:

textbox1.Text = item[1].value;

Upvotes: 0

GazTheDestroyer
GazTheDestroyer

Reputation: 21261

You shouldn't store data only in controls. I would create a class to hold all the info about a particular item, and populate THAT with the contents of the textboxes, and store it in some collection (Maybe a dictionary if you want to look one up by code)

You can then add a reference to this object in the "Tag" property of ListViewItem, so you can immediately grab it from any selected ListViewItem.

Upvotes: 0

Related Questions