實現功能:TabControl控件中的TabPage是動態添加的,點擊DataGridView的一行拖動到TabControl中,當鼠標拖動到不同的TabPage索引標籤上時顯示該TabPage
問題:鼠標進入TabControl后,在索引標籤上移動時,坐標都不會變,只會抓取到移入時的坐標,要想移入其它的索引標籤上,必須移出鼠標重新進入TabControl才行。
請各位高手指點!這個問題苦惱了幾天也沒找出解決辦法...下面是我寫的代碼:
        private void dGView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
             //開始拖動
            dGView.DoDragDrop(e.RowIndex, DragDropEffects.Move);
        }
        private void tabControl_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
            Point pnt1 = new Point(e.X, e.Y);
            Rectangle recTab;
            foreach (TabPage tpTemp in tabControl.TabPages)
            {
                recTab = tabControl.GetTabRect(tabControl.TabPages.IndexOf(tpTemp));
                if (recTab.Contains(pnt1.X, pnt1.Y))
                {
                    tabControl.SelectedTab = tpTemp;
                }
            }
        }        private void tabControl_DragDrop(object sender, DragEventArgs e)
        {
           //進入選中的TabPage后執行的代碼
        }

解决方案 »

  1.   

    今天終於把問題解決了!!!分就給自己吧^_^,下面把解決的代碼貼出來與大家分享一下吧!
    正確的代碼如下:
    private void dGView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
            { 
                //開始拖動 
                dGView.DoDragDrop(e.RowIndex, DragDropEffects.Move); 
            } 
            private void tabControl_DragEnter(object sender, DragEventArgs e) 
            { 
                e.Effect = DragDropEffects.Move; 
            } 
    private void tabControl1_DragOver(object sender, DragEventArgs e)
           {
               
               Point pnt1 = new Point(e.X-176, e.Y-257);
               
               Rectangle recTab;
               foreach (TabPage tpTemp in tabControl1.TabPages)
               {
                   recTab = tabControl1.GetTabRect(tabControl1.TabPages.IndexOf(tpTemp));
                   
                   if (recTab.Contains(pnt1.X, pnt1.Y))
                   {
                       tabControl1.SelectedTab = tpTemp;
                   }
               }
           }
            private void tabControl_DragDrop(object sender, DragEventArgs e) 
            { 
              //進入選中的TabPage后執行的代碼 
            } 
    因為DragOver事件在整個拖動過程中都會被執行!!!