0
我正在嘗試將項目從列表框中拖放到文本框中。這對我來說可以。 現在,當我試圖將第二個項目拖到同一個文本框中時,它將它追加到文本框中包含的最後一個文本。我想它應該粘貼在我拖動項目到文本框的位置。我用下面的代碼到目前爲止將文本插入到x,y座標位置的文本框中
private void Form1_Load(object sender, System.EventArgs e) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0)
listBoxControl1.Items.Add("Item " + i.ToString());
}
private void listBoxControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
ListBoxControl c = sender as ListBoxControl;
p = new Point(e.X, e.Y);
int selectedIndex = c.IndexFromPoint(p);
if (selectedIndex == -1)
p = Point.Empty;
}
private void listBoxControl1_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e) {
if (e.Button == MouseButtons.Left)
if ((p != Point.Empty) && ((Math.Abs(e.X - p.X) > 5) || (Math.Abs(e.Y - p.Y) > 5)))
listBoxControl1.DoDragDrop(sender, DragDropEffects.Move);
}
private void textEdit1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void textEdit1_DragDrop(object sender, DragEventArgs e)
{
TextEdit textBox1 = sender as TextEdit;
Point newPoint = new Point(e.X, e.Y);
newPoint = textBox1.PointToClient(newPoint);
object item = listBoxControl1.Items[listBoxControl1.IndexFromPoint(p)];
if (textBox1.Text == "")
{
textBox1.Text = item.ToString();
}
else
{
textBox1.Text = textBox1.Text + "," + item.ToString();
}
listBoxControl1.Items.Remove(item);
}
它給了我一些邏輯來解決這個問題。我修改了一下你的解決方案,因爲在textBox1.Text不是空字符串的情況下它會給出錯誤的結果。即當它進入你的代碼的其他部分..但無論如何.. – 2013-03-15 08:40:41