Hi I'm making a code which I can drag and drop the text between two boxes. In my code only the listbox1 works but the listbox2 is not. Another thing is if I add several text on my listbox and select any text in my listbox I'm getting an error: InvalidArgument=Value of '-1' is not valid for 'index' Parameter name: index. can you please help me look where should I change the value of index into -1? Thanks
I'm quite stuck-up here: By the way, here is my code:
namespace ListBoxDragandDrop
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Add for the drag and drop
this.listBox1.MouseDown += new MouseEventHandler(this.listBox1_MouseDown);
//public DragDropEffects DoDragDrop(object data,
//DragDropEffects allowedEffects);
}
private void cmdAdd_Click(object sender, EventArgs e)
{
string yourInputText;
yourInputText = textBox1.Text.Trim();
if (yourInputText.Length > 1)
{
listBox1.Items.Add(yourInputText);
textBox1.Text = string.Empty;
}
else
{
MessageBox.Show("Bitte schreiben Sie Ihre Texte im Feld auf");
}
}
private void cmdDelete_Click(object sender, EventArgs e)
{
int listBoxSelectedItem = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
//Selected "False" in the Enable Properties
cmdDelete.Enabled = false;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
cmdDelete.Enabled = true;
//Added if you use the btnToRight
btnToRight.Enabled = true;
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox2.SelectedItem!=null)
{
cmdDelete.Enabled = true;
//Added if you use the btnToLeft
btnToLeft.Enabled = true;
}
}
private void btnToRight_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem!=null)
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
//Add this to disable the btnToRight unless you select an item
//from listbox1
//Set "Enable" in the Properties as false
btnToRight.Enabled = false;
}
}
private void btnToLeft_Click(object sender, EventArgs e)
{
if (listBox2.SelectedItem != null)
{
listBox1.Items.Add(listBox2.SelectedItem);
listBox2.Items.Remove(listBox2.SelectedItem);
//Add this to disable the btnToLeft unless you select an item
//from listbox2
//Set "Enable" in the Properties as false
btnToLeft.Enabled = false;
}
}
//For the Drag and Drop set the destination control "Allow Drop" to True
//Destination Control is where you drag drop an item
//then add an event handler, this can be seen in the initialization
//then add the event MouseDown, here set the Events of listbox1
//public DragDropEffects DoDragDrop(object data,DragDropEffects
//allowedEffects);
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (listBox1.Items.Count==0)
return;
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
if (dde1==DragDropEffects.All)
{
listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y));
}
}
private void listBox2_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
string str = (string) e.Data.GetData(DataFormats.StringFormat);
listBox2.Items.Add(str);
}
}