winForm 控件中右键点击事件收集


DataGridView
DataGridView上按右键弹出右键菜单前选中当前行

None.gif          private   void  DataGridView1_CellMouseDown( object  sender, DataGridViewCellMouseEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataGridView1.ClearSelection();
InBlock.gif            DataGridView1.Rows[e.RowIndex].Selected 
= true;
InBlock.gif            DataGridView1.CurrentCell 
= DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif2007-05-16 17:31 by HELLO [未注册用户] 
InBlock.gif用“dataGridView1_CellMouseClick”这个事件 
InBlock.gif
InBlock.gif有些时候可以只弹出菜单,没有选择单元格;有时候只选择单元格,没有弹出菜单 
InBlock.gif
InBlock.gif如果菜单控件在绑在DATAGRIDVIEW上(dataGridView1.ContextMenuStrip = this.ContextSrtip1),又不能选择单元格
ExpandedBlockEnd.gif
*/


DataGridView 超链接点击事件

None.gif          // 内容点击, 并判断所点击的列名
None.gif
         private   void  dgvData_CellContentClick( object  sender, DataGridViewCellEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataGridView dgv 
= sender as DataGridView;
InBlock.gif            
string columnName = dgv.Columns[e.ColumnIndex].Name;
InBlock.gif            
if ("operation".Equals(columnName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{//如果点击的列名符合                
InBlock.gif
                dgv.Rows.RemoveAt(e.RowIndex);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }




TreeView
c# winform TreeView控件中实现拖拽的功能

ContractedBlock.gif ExpandedBlockStart.gif 节点拖拽事件 #region 节点拖拽事件
InBlock.gif        
//当用户开始拖动节点时
InBlock.gif
        private void tvModel_ItemDrag(object sender, ItemDragEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode selectNode 
= e.Item as TreeNode;
InBlock.gif            
this.tvModel.SelectedNode = selectNode;
InBlock.gif
InBlock.gif            
this.form.DoDragDrop(e.Item, DragDropEffects.Move);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//将对象拖入控件的边界时
InBlock.gif
        private void tvModel_DragEnter(object sender, DragEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode enterNode 
= (TreeNode)(e.Data.GetData(typeof(TreeNode)));
InBlock.gif            
if (enterNode != null)
InBlock.gif                e.Effect 
= DragDropEffects.Move;
InBlock.gif            
else
InBlock.gif                e.Effect 
= DragDropEffects.None;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//在完成拖放操作时
InBlock.gif
        private void tvModel_DragDrop(object sender, DragEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode selectNode 
= (TreeNode)(e.Data.GetData(typeof(TreeNode)));
InBlock.gif
InBlock.gif            
//0.如果选择源为空 或者不是二级节点则退出
InBlock.gif
            if (selectNode == null || selectNode.Level != 1)
InBlock.gif                
return;
InBlock.gif
InBlock.gif            
//1.根据鼠标坐标获得目标节点
InBlock.gif
            Position.X = e.X;
InBlock.gif            Position.Y 
= e.Y;
InBlock.gif            Position 
= this.tvModel.PointToClient(Position);
InBlock.gif            TreeNode targetNode 
= this.tvModel.GetNodeAt(Position);
InBlock.gif            
foreach (TreeNode node in this.tvModel.Nodes[0].Nodes)
InBlock.gif                node.NodeFont 
= new Font(this.form.Font, FontStyle.Regular);
InBlock.gif
InBlock.gif            
//2.获得要插入的目标节点索引
InBlock.gif
            int index = 0;
InBlock.gif            
if (targetNode == null)
InBlock.gif                index 
= selectNode.Parent.Nodes.Count - 1;      //目标节点为null, 直接插入到最后
InBlock.gif
            else if (targetNode.Level == 1)
InBlock.gif                index 
= targetNode.Index + 1;                   //目标节点为同级, 直接在之后插入
InBlock.gif
            else if (targetNode.Level == 0)
InBlock.gif                index 
= 0;                                      //目标为根节点, 直接插入到最前
InBlock.gif
            else if (targetNode.Level > 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                                                   //目标节点为二级节点以后, 遍历到其二级节点
InBlock.gif
                while (targetNode.Level > 1)
InBlock.gif                    targetNode 
= targetNode.Parent;
InBlock.gif                index 
= index = targetNode.Index + 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//3.源节点删除, 目标节点在索引出插入, 并高亮选中
InBlock.gif
            selectNode.Remove();
InBlock.gif            
this.tvModel.Nodes[0].Nodes.Insert(index, selectNode);
InBlock.gif            
this.tvModel.SelectedNode = selectNode;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//将对象拖过控件边缘时
InBlock.gif
        private void tvModel_DragOver(object sender, DragEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Position.X 
= e.X;
InBlock.gif            Position.Y 
= e.Y;
InBlock.gif            Position 
= this.tvModel.PointToClient(Position);
InBlock.gif
InBlock.gif            
//在拖过的控件前后显示划线效果
InBlock.gif
            TreeNode targetNode = this.tvModel.GetNodeAt(Position);
InBlock.gif            
if (targetNode != null && targetNode.Level == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (targetNode.PrevNode != null)
InBlock.gif                    targetNode.PrevNode.NodeFont 
= new Font(this.form.Font, FontStyle.Regular);
InBlock.gif                
if (targetNode.NextNode != null)
InBlock.gif                    targetNode.NextNode.NodeFont 
= new Font(this.form.Font, FontStyle.Regular);
InBlock.gif                targetNode.NodeFont 
= new Font(this.form.Font, FontStyle.Underline);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

 

转载于:https://www.cnblogs.com/kiant71/archive/2009/07/10/1752023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值