Drag and drop listbox to textbox

Chong

Well-known member
Joined
Apr 4, 2003
Messages
79
I need help with drag and drop listbox to a textbox. Heres my situation. Below are my code:

Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
Dim strTextBox2 As String
popAmount()
strHolder = e.Data.GetData(lstScholar.SelectedItem.GetType.ToString())
TextBox2.Text = strHolder & vbCrLf & "SSN#: " & strSSN & "Amount Award: $" & strAmount

End Sub

Private Sub popAmount()
Dim jmpFrmAmount As New frmAmount()
jmpFrmAmount.ShowDialog()
strAmount = jmpFrmAmount.txtAmount.Text()
strSSN = jmpFrmAmount.txtSSN.Text()
End Sub

Private Sub lstScholar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstScholar.MouseDown
lstScholar.Refresh()
lstScholar.DoDragDrop(lstScholar.SelectedItem, DragDropEffects.Copy Or DragDropEffects.Move)

End Sub

The problem now is that the lstScholar.SelectedItem() isnt select the curent selected item. This code only works for the first try. Any addtional attemp to select items in the listbox and drop them to the textbox2 will not work. Theres no error generated by the selection never refresh to reflect the selected item in the listbox. Please help.

Many help in advance!

Charlie
 
Instead of using quote /quote to put your code in try vb /vb instead.

I also, am having the same problem as you when using MouseDown to get the selected item. Except my trouble is that the selected item is always different. Index 0 was 4 one time and others kept getting all messed up just the same with this code:
C#:
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    this.Text= this.listBox1.SelectedIndex.ToString();
}
I found that the problem is caused here:
C#:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
	if((string)styleKey.GetValue(this.listBox1.SelectedIndex+1+"")=="Stretched")
					this.StretchedRB.Checked=true;
	else if((string)styleKey.GetValue(this.listBox1.SelectedIndex+1+"")=="Centered")
					this.CenteredRB.Checked=true;
	else this.TiledRB.Checked=true;

	this.minutesTB.Text= a.Conversions.MilisectoMin((int)minKey.GetValue(this.listBox1.SelectedIndex+1+""))+"";
}
I dont see any reason why that would make any difference at all but when I remove that code the problem stops. Can anyone see what the problem might be? I found that it has something to do with multiselection but I only select one item at a time when testing this.
 
Last edited by a moderator:
I was forced to put this in the beginning of my mouse down block:

Point p = new Point(e.X, e.Y);
int index= this.listBox1.IndexFromPoint(p);
this.listBox1.SetSelected(index, true);

but it destroys multi selection!! This is not good!!! We still need help with this if anyone can?
 
Back
Top