| View previous topic :: View next topic |
| Author |
Message |
Sneak Grandmaster Cheater Supreme
Reputation: 0
Joined: 05 Nov 2008 Posts: 1895
|
Posted: Sat Dec 26, 2009 3:51 pm Post subject: [C#] Listview adding items to Dfferent columns |
|
|
Im making a price tool, and I want it to have a listview, and when I press "Send", textbox1.text will appear in the 5th column. How would I do that?
Ive tried:
| Code: | ListViewItem item = new ListViewItem();
item.Text = textBox1.Text;
listView1.Items.Add(item); |
but all it does is send the string vertically down the first column.
Help
_________________
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Sun Dec 27, 2009 9:09 am Post subject: |
|
|
Change | Code: | | listView1.Items.Add(item); | to | Code: | | listView5.Items.Add(textbox1.Text); | , and remove the superfluous ListViewItem declaration.
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Sneak Grandmaster Cheater Supreme
Reputation: 0
Joined: 05 Nov 2008 Posts: 1895
|
Posted: Sun Dec 27, 2009 4:13 pm Post subject: |
|
|
I dont have a control called listview5
_________________
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Mon Dec 28, 2009 10:47 am Post subject: |
|
|
| Sneak wrote: | | I dont have a control called listview5 | Ah, my mistake. I misread it as ListBox. I haven't used this control before, but from what I read, you will have to do something like this: | Code: |
public void PlaceAtIndex(int index, ListViewItem item, ListViewItem.ListViewSubItem subItem)
{
if(index > item.SubItems.Count-1)
{
for (int i = item.SubItems.Count; i < index; i++)
{
item.SubItems.Add("");
}
item.SubItems.Add(subItem);
}
else
{
item.SubItems.RemoveAt(index);
item.SubItems.Insert(index, subItem);
}
}
|
And then add it like this: | Code: | ListViewItem item = new ListViewItem();
ListViewItem.ListViewSubItem subItem = new ListViewItem.ListViewSubItem(item,textbox1.Text);
PlaceAtIndex(4, item, subItem);
listView1.Items.Add(item); | There is probably an easier way, I just didn't find it.
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Dec 28, 2009 7:19 pm Post subject: |
|
|
| Code: |
private void ModifyItemString(ListViewSubItemCollection item, int index, string text)
{
if (index < 0 || index > (item.Count - 1))
throw new IndexOutOfRangeException();
item[index].Text = text;
}
private void Send_Click(object sender, EventArgs e)
{
ModifyItemString(listview.Items[<row you want to modify>].SubItems, 4, textbox.Text);
}
|
There are different ways to do it, but this is probably one of the easiest.
|
|
| Back to top |
|
 |
|