在From内放了 两个控件 TextBox 和Listbox
在Listbox内增加了几行数据设置TextBox AllowDrop=True代码如下
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Determines which item was selected.
string a;
ListBox lb =( (ListBox)sender);
Point pt = new Point(e.X,e.Y);
int index = lb.IndexFromPoint(pt); // Starts a drag-and-drop operation with that item.
if(index>=0)
{
lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link);
a = lb.Items[index].ToString();
} }
private void textBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
// If the data is text, copy the data to the RichTextBox control.
if(e.Data.GetDataPresent("Text"))
e.Effect = DragDropEffects.Copy; }
private void textBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
// Loads the file into the control.
//richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText);
textBox1.Text = (String)e.Data.GetData("Text");
}
this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop);
this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);
结果时 Drag的数据可以看到 就是 DragDrop 无法触发
请高手指点!!! 急!!
在Listbox内增加了几行数据设置TextBox AllowDrop=True代码如下
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Determines which item was selected.
string a;
ListBox lb =( (ListBox)sender);
Point pt = new Point(e.X,e.Y);
int index = lb.IndexFromPoint(pt); // Starts a drag-and-drop operation with that item.
if(index>=0)
{
lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link);
a = lb.Items[index].ToString();
} }
private void textBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
// If the data is text, copy the data to the RichTextBox control.
if(e.Data.GetDataPresent("Text"))
e.Effect = DragDropEffects.Copy; }
private void textBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
// Loads the file into the control.
//richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText);
textBox1.Text = (String)e.Data.GetData("Text");
}
this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop);
this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);
结果时 Drag的数据可以看到 就是 DragDrop 无法触发
请高手指点!!! 急!!
解决方案 »
- vs 2008的问题~!!! 望解答!~~
- c# 平台调用复杂结构体 非托管 DLL
- 请教各位线程互斥的问题。
- 有规律的声明XMLELEMENT,大约几十个,如何写循环?
- 如何用c#写不基与网页的程序????
- WPF性能
- 100分问题!!C# DataTable 批量更新数据问题 大家帮帮忙
- 请问如何用C#将wav文件存为mp3文件啊?
- 在XP(P)下无法安装Vs.net 7.0?
- 贺喜巴西2:1
- 一个关于XML读节点的问题,分只有20了!希望大家能帮我一下
- 昨天才装的 Microsoft SQL Server 2005 CTP 里边的 LogFiles 目录竟高达 19753 个文件和 3.91G 的容量
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace CS_Test2005_07_07
{
/// <summary>
/// Form6 的摘要说明。
/// </summary>
public class Form6 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox ListDragTarget;
private System.Windows.Forms.Label DropLocationLabel;
private System.Windows.Forms.ListBox ListDragSource;
private System.Windows.Forms.CheckBox UseCustomCursorsCheck;
private System.ComponentModel.IContainer components = null; public Form6()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.ListDragTarget = new System.Windows.Forms.ListBox();
this.DropLocationLabel = new System.Windows.Forms.Label();
this.ListDragSource = new System.Windows.Forms.ListBox();
this.UseCustomCursorsCheck = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.AllowDrop = true;
this.textBox1.Location = new System.Drawing.Point(304, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// ListDragTarget
//
this.ListDragTarget.AllowDrop = true;
this.ListDragTarget.ItemHeight = 12;
this.ListDragTarget.Items.AddRange(new object[] {
"efaweg",
"wegaweg",
"erwahger",
"rewagreg"});
this.ListDragTarget.Location = new System.Drawing.Point(80, 112);
this.ListDragTarget.Name = "ListDragTarget";
this.ListDragTarget.Size = new System.Drawing.Size(120, 88);
this.ListDragTarget.TabIndex = 1;
this.ListDragTarget.DragOver += new System.Windows.Forms.DragEventHandler(this.ListDragTarget_DragOver);
this.ListDragTarget.DragDrop += new System.Windows.Forms.DragEventHandler(this.ListDragTarget_DragDrop);
this.ListDragTarget.DragEnter += new System.Windows.Forms.DragEventHandler(this.ListDragTarget_DragEnter);
this.ListDragTarget.DragLeave += new System.EventHandler(this.ListDragTarget_DragLeave);
//
// DropLocationLabel
//
this.DropLocationLabel.Location = new System.Drawing.Point(176, 16);
this.DropLocationLabel.Name = "DropLocationLabel";
this.DropLocationLabel.TabIndex = 2;
this.DropLocationLabel.Text = "label1";
//
// ListDragSource
//
this.ListDragSource.ItemHeight = 12;
this.ListDragSource.Items.AddRange(new object[] {
"GBWEWE",
"HGEWG",
"RWEG"});
this.ListDragSource.Location = new System.Drawing.Point(288, 112);
this.ListDragSource.Name = "ListDragSource";
this.ListDragSource.Size = new System.Drawing.Size(120, 88);
this.ListDragSource.TabIndex = 3;
this.ListDragSource.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListDragSource_MouseDown);
this.ListDragSource.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.ListDragSource_QueryContinueDrag);
this.ListDragSource.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ListDragSource_MouseUp);
this.ListDragSource.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ListDragSource_MouseMove);
this.ListDragSource.GiveFeedback += new System.Windows.Forms.GiveFeedbackEventHandler(this.ListDragSource_GiveFeedback);
//
// UseCustomCursorsCheck
//
this.UseCustomCursorsCheck.Location = new System.Drawing.Point(168, 64);
this.UseCustomCursorsCheck.Name = "UseCustomCursorsCheck";
this.UseCustomCursorsCheck.TabIndex = 4;
this.UseCustomCursorsCheck.Text = "checkBox1";
//
// Form6
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(704, 365);
this.Controls.Add(this.UseCustomCursorsCheck);
this.Controls.Add(this.ListDragSource);
this.Controls.Add(this.DropLocationLabel);
this.Controls.Add(this.ListDragTarget);
this.Controls.Add(this.textBox1);
this.Name = "Form6";
this.Text = "Form6";
this.Load += new System.EventHandler(this.Form6_Load);
this.ResumeLayout(false); }
#endregion private int indexOfItemUnderMouseToDrag;
private int indexOfItemUnderMouseToDrop;
private Rectangle dragBoxFromMouseDown;
private Point screenOffset; private Cursor MyNoDropCursor;
private Cursor MyNormalCursor; private void Form6_Load(object sender, System.EventArgs e)
{
}
private void ListDragSource_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Get the index of the item the mouse is below.
indexOfItemUnderMouseToDrag = ListDragSource.IndexFromPoint(e.X, e.Y); if (indexOfItemUnderMouseToDrag != ListBox.NoMatches)
{
// Remember the point where the mouse down occurred. The DragSize indicates
// the size that the mouse can move before a drag event should be started.
Size dragSize = SystemInformation.DragSize; // Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width /2),
e.Y - (dragSize.Height /2)), dragSize);
}
else
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty; } private void ListDragSource_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Reset the drag rectangle when the mouse button is raised.
dragBoxFromMouseDown = Rectangle.Empty; }
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{ // If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{ // Create custom cursors for the drag-and-drop operation.
try
{
MyNormalCursor = new Cursor("3dwarro.cur");
MyNoDropCursor = new Cursor("3dwno.cur"); }
catch
{
// An error occurred while attempting to load the cursors, so use
// standard cursors.
UseCustomCursorsCheck.Checked = false;
}
finally
{ // The screenOffset is used to account for any desktop bands
// that may be at the top or left side of the screen when
// determining when to cancel the drag drop operation.
screenOffset = SystemInformation.WorkingArea.Location; // Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = ListDragSource.DoDragDrop(ListDragSource.Items[indexOfItemUnderMouseToDrag], DragDropEffects.All | DragDropEffects.Link); // If the drag operation was a move then remove the item.
if (dropEffect == DragDropEffects.Move)
{
ListDragSource.Items.RemoveAt(indexOfItemUnderMouseToDrag); // Selects the previous item in the list as long as the list has an item.
if (indexOfItemUnderMouseToDrag > 0)
ListDragSource.SelectedIndex = indexOfItemUnderMouseToDrag -1; else if (ListDragSource.Items.Count > 0)
// Selects the first item.
ListDragSource.SelectedIndex =0;
} // Dispose of the cursors since they are no longer needed.
if (MyNormalCursor != null)
MyNormalCursor.Dispose(); if (MyNoDropCursor != null)
MyNoDropCursor.Dispose();
}
}
} } private void ListDragSource_GiveFeedback(object sender, System.Windows.Forms.GiveFeedbackEventArgs e)
{
// Use custom cursors if the check box is checked.
if (UseCustomCursorsCheck.Checked)
{ // Sets the custom cursor based upon the effect.
e.UseDefaultCursors = false;
if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
Cursor.Current = MyNormalCursor;
else
Cursor.Current = MyNoDropCursor;
} }
private void ListDragTarget_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
// Determine whether string data exists in the drop data. If not, then
// the drop effect reflects that the drop cannot occur.
if (!e.Data.GetDataPresent(typeof(System.String)))
{ e.Effect = DragDropEffects.None;
DropLocationLabel.Text = "None - no string data.";
return;
} // Set the effect based upon the KeyState.
if ((e.KeyState & (8+32)) == (8+32) &&
(e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
{
// KeyState 8 + 32 = CTL + ALT // Link drag and drop effect.
e.Effect = DragDropEffects.Link; }
else if ((e.KeyState & 32) == 32 &&
(e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
{ // ALT KeyState for link.
e.Effect = DragDropEffects.Link; }
else if ((e.KeyState & 4) == 4 &&
(e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{ // SHIFT KeyState for move.
e.Effect = DragDropEffects.Move; }
else if ((e.KeyState & 8) == 8 &&
(e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{ // CTL KeyState for copy.
e.Effect = DragDropEffects.Copy; }
else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
{ // By default, the drop action should be move, if allowed.
e.Effect = DragDropEffects.Move; }
else
e.Effect = DragDropEffects.None;
// Get the index of the item the mouse is below. // The mouse locations are relative to the screen, so they must be
// converted to client coordinates. indexOfItemUnderMouseToDrop =
ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y))); // Updates the label text.
if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
{ DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1);
}
else
DropLocationLabel.Text = "Drops at the end."; }
private void ListDragTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
// Ensure that the list item index is contained in the data.
if (e.Data.GetDataPresent(typeof(System.String)))
{ Object item = (object)e.Data.GetData(typeof(System.String)); // Perform drag and drop, depending upon the effect.
if (e.Effect == DragDropEffects.Copy ||
e.Effect == DragDropEffects.Move)
{
// Insert the item.
if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item);
else
ListDragTarget.Items.Add(item);
}
}
// Reset the label text.
DropLocationLabel.Text = "None"; } private void ListDragSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
{
// Cancel the drag if the mouse moves off the form.
ListBox lb = sender as ListBox; if (lb != null)
{ Form f = lb.FindForm(); // Cancel the drag if the mouse moves off the form. The screenOffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) ||
((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) ||
((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) ||
((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom))
{ e.Action = DragAction.Cancel;
}
} } private void ListDragTarget_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
// Reset the label text.
DropLocationLabel.Text = "None"; } private void ListDragTarget_DragLeave(object sender, System.EventArgs e)
{
// Reset the label text.
DropLocationLabel.Text = "None"; }
}
}