Reputation: 361
I've currently got two lists I'm working with. One filled with items and the other one is empty. When the user double-clicks an item in the filled list it's supposed to add that item to the empty/second list, but instead of adding it to the top of that list I want the newly added item at the bottom. So the items should be added from the bottom up. I'm working with a datagridview, but am willing to use listview/listbox as long as it gets the job done.
Upvotes: 1
Views: 2549
Reputation: 305
Try my code. I am using C# 3.5
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
int index = listBox2.Items.Count>0?listBox2.Items.Count:0;
listBox2.Items.Insert(index, listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
}
}
Upvotes: 0
Reputation: 3688
I added two list boxes to a windows form. listBox1 and listBox2 I added Seven Items to the first list box {One,Two,Three...} I added the double click event handler where I
listBox2.Items.Add(listBox1.SelectedItem);
The new Item added to the bottom of the list, which is what it sounds like you want. I know the same thing works with a DataGridView.
Do you want them to be added physically to the bottom of the box leaving whitespace at the top until it is filled? Is that what you are trying to do?
Sorry this isn't really an answer, I guess I don't have enough rep to reply as a comment.
EDIT: ok I think I have your answer now Add a list box with your items, it doesn't have to be a list box your Datagridview would work fine.
Try using a FlowControlPanel and change alignment to bottom up, sounds easy, well it is. Add labels to it, like this
//add a label to the flow control panel when you double click on an item
private void listBox1_DoubleClick(object sender, EventArgs e)
{
Label label = new Label();
label.Text = listBox1.SelectedItem.ToString();
label.Click += new EventHandler(label_Click);
label.AutoSize = true;
flowLayoutPanel1.Controls.Add(label);
label.BringToFront();
}
//Will remove the label if you click on it.
void label_Click(object sender, EventArgs e)
{
((Label)sender).Click -= new EventHandler(label_Click);
((Label)sender).Dispose();
}
brining the label to the front puts the new one at the bottom.
Upvotes: 2
Reputation: 9726
I think I understand what you want to do and the only way I know of is to fake it by pre-filling your second list / datagridview with with empty items so the user doesn't see anything. Then as your user makes selections from your first box you will replace the bottom most empty item with your new real item.
Upvotes: 0