问题描述
直接在属性添加事件没有问题,但像这样添加后,拖拽鼠标一放上去,就直接触发了DragDrop的事件了。不是等释放左键才触发。不知道是为什么。privatevoidtextBox1_DragEnter(objectsender,DragEventArgse){if(e.Data.GetDataPresent(DataFormats.FileDrop))e.Effect=DragDropEffects.Copy;elsee.Effect=DragDropEffects.None;}privatevoidtextBox1_DragDrop(objectsender,DragEventArgse){TextBoxtb=(TextBox)sender;tb.Text=((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();}privatevoidForm1_Load(objectsender,EventArgse){textBox1.AllowDrop=true;textBox1.DragEnter+=newDragEventHandler(textBox1_DragEnter);textBox1.DragOver+=newDragEventHandler(textBox1_DragDrop);}
后来我在DragDrop里面加了if(MouseButtons.Left!=Control.MouseButtons){TextBoxtb=(TextBox)sender;tb.Text=((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();}
在测试程序里虽然可以和正常的一样效果,但在主程序比较复杂的窗体里面没有效果。新人求指教。
解决方案
解决方案二:
把DataGridView中的数据拖曳到TreeView//窗体加载时,显示原有的数据privatevoidForm1_Load(objectsender,EventArgse){stringP_Connection=string.Format(//创建数据库连接字符串"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=test.mdb;UserId=Admin");OleDbDataAdapterP_OLeDbDataAdapter=newOleDbDataAdapter("selectau_idas用户编号,au_lnameas用户名,phoneas联系电话fromauthors",P_Connection);DataSetds=newDataSet();P_OLeDbDataAdapter.Fill(ds,"UserInfo");dataGridView1.DataSource=ds.Tables["UserInfo"].DefaultView;TreeNodetreeNode=newTreeNode("用户信息",0,0);treeView1.Nodes.Add(treeNode);//默认情况下追加节点追加节点ToolStripMenuItem.Checked=true;}//DataGridView的按下鼠标事件privatevoiddataGridView1_MouseDown(objectsender,MouseEventArgse){if(dataGridView1.SelectedCells.Count!=0){//定义一个二维数组,数组中的每一行代表DataGridView中的一条记录recordInfo=newstring[dataGridView1.Rows.Count,dataGridView1.Columns.Count];//当按下鼠标左键时,首先获取选定行,记录每一行对应的信息for(inti=0;i<dataGridView1.Rows.Count;i++){if(dataGridView1.Rows[i].Selected){for(intj=0;j<dataGridView1.Columns.Count;j++){recordInfo[i,j]=dataGridView1.Rows[i].Cells[j].Value.ToString();}}}}}//当鼠标进入TreeView控件时,触发的操作privatevoidtreeView1_MouseEnter(objectsender,EventArgse){if(追加节点ToolStripMenuItem.Checked==true){#region代码区域if(recordInfo!=null&&recordInfo.Length!=0){//用双重for循环遍历数组recordInfo中的内容for(inti=0;i<recordInfo.GetLength(0);i++){for(intj=0;j<recordInfo.GetLength(1);j++){//判断数组中的值是否为空if(recordInfo[i,j]!=null){if(j==0){//向TreeView中加入节点TreeNodeNode1=newTreeNode(recordInfo[i,j].ToString());treeView1.SelectedNode.Nodes.Add(Node1);treeView1.SelectedNode=Node1;}else{//添加子级节点下的子节点TreeNodeNode2=newTreeNode(recordInfo[i,j].ToString());treeView1.SelectedNode.Nodes.Add(Node2);}}}treeView1.SelectedNode=treeView1.Nodes[0];treeView1.ExpandAll();}//清空recordInfo中的记录for(intm=0;m<recordInfo.GetLength(0);m++){for(intn=0;n<recordInfo.GetLength(1);n++){recordInfo[m,n]=null;}}}#endregion}if(清空内容ToolStripMenuItem.Checked==true){if(treeView1.SelectedNode.Nodes.Count!=0){treeView1.SelectedNode.Remove();TreeNodetreeNode=newTreeNode("用户信息",0,0);treeView1.Nodes.Add(treeNode);treeView1.SelectedNode=treeNode;#region代码区域if(recordInfo!=null&&recordInfo.Length!=0){//用双重for循环遍历数组recordInfo中的内容for(inti=0;i<recordInfo.GetLength(0);i++){for(intj=0;j<recordInfo.GetLength(1);j++){//判断数组中的值是否为空if(recordInfo[i,j]!=null){if(j==0){//向TreeView中加入节点TreeNodeNode1=newTreeNode(recordInfo[i,j].ToString());treeView1.SelectedNode.Nodes.Add(Node1);treeView1.SelectedNode=Node1;}else{//添加子级节点下的子节点TreeNodeNode2=newTreeNode(recordInfo[i,j].ToString());treeView1.SelectedNode.Nodes.Add(Node2);}}}treeView1.SelectedNode=treeView1.Nodes[0];treeView1.ExpandAll();}//清空recordInfo中的记录for(intm=0;m<recordInfo.GetLength(0);m++){for(intn=0;n<recordInfo.GetLength(1);n++){recordInfo[m,n]=null;}}}#endregion追加节点ToolStripMenuItem.Checked=true;清空内容ToolStripMenuItem.Checked=false;}}}