2017-07-20 32 views
1

私は独自のTabPagesで独自のTabControlを作成しています。削除部分を除いてうまくいっています。デザイナのフォームにTabControlを追加すると、すべて正常に動作し、2つのデフォルトのTabPagesが追加され、コントロールが描画されます。しかし、デザイナのフォームからTabControlを削除すると、TabControl.Controlsコレクションの一部であるTabPagesはデザイナコードから削除されません。彼らはただ親を失うだけです。デザイナーコードから子コントロールを削除する

どのような考えですか?

私は次のコードを使用します。

public class CustomTabControlDesigner : ParentControlDesigner 
{ 
    DesignerVerbCollection _fVerbs; 
    public override DesignerVerbCollection Verbs 
    { 
     get 
     { 
      if (_fVerbs == null) 
      { 
       _fVerbs = new DesignerVerbCollection(new[] { new DesignerVerb("Add Tab", OnAdd), new DesignerVerb("Del Tab", OnDel) }); 
      } 

      return _fVerbs; 
     } 
    } 

    void OnAdd(object sender, EventArgs e) 
    { 
     IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost)); 
     if (designerHost != null) 
     { 
      WTabPage newPage = (WTabPage)designerHost.CreateComponent(typeof(WTabPage)); 
      //newPage.Text = newPage.Name; 

      ((WTab)Component).Controls.Add(newPage); 
     } 
    } 
    void OnDel(object sender, EventArgs e) 
    { 
     IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost)); 
     if (designerHost != null) 
     { 
      ((WTab)Component).Controls.Remove(((WTab)Component).SelectedTab); 
     } 
    } 

    public override void InitializeNewComponent(IDictionary defaultValues) 
    { 
     base.InitializeNewComponent(defaultValues); 

     for (int i = 0; i < 2; i++) 
     { 
      OnAdd(this, EventArgs.Empty); 
     } 
    } 
    //protected override void Dispose(bool disposing) 
    //{ 
    // for (int i = ((WTab)Component).Controls.Count - 1; i >= 0; i--) 
    // { 
    //  ((WTab)Component).Controls.Remove(((WTab)Component).Controls[i]); 
    // } 

    // base.Dispose(disposing); 
    //} 

    protected override void WndProc(ref Message m) 
    { 
     base.WndProc(ref m); 

     // Selection of tabs via mouse 
     if (m.Msg == 0x201/*WM_LBUTTONDOWN*/) 
     { 
      WTab control = (WTab)Component; 

      int lParam = m.LParam.ToInt32(); 

      Point hitPoint = new Point(lParam & 0xffff, lParam >> 0x10); 

      if (Control.FromHandle(m.HWnd) == null) // Navigation 
      { 
       if (hitPoint.X < 18 && control.SelectedIndex > 0) // Left 
       { 
        control.SelectedIndex--; 
       } 

       else 
       { 
        control.SelectedIndex++; // Right} 
       } 
      } 
      else 
      { 
       // Header click 
       for (int i = 0; i < control.TabCount; i++) 
       { 
        if (control.GetTabRect(i).Contains(hitPoint)) 
        { 
         control.SelectedIndex = i; 

         return; 
        } 
       } 
      } 
     } 
    } 
    protected override void OnDragDrop(DragEventArgs de) 
    { 
     ((IDropTarget)((WTab)Component).SelectedTab).OnDragDrop(de); 
    } 
    protected override void OnDragEnter(DragEventArgs de) 
    { 
     ((IDropTarget)((WTab)Component).SelectedTab).OnDragEnter(de); 
    } 
    protected override void OnDragLeave(EventArgs e) 
    { 
     ((IDropTarget)((WTab)Component).SelectedTab).OnDragLeave(e); 
    } 
    protected override void OnDragOver(DragEventArgs de) 
    { 
     ((IDropTarget)((WTab)Component).SelectedTab).OnDragOver(de); 
    } 
} 

コントロールが追加または削除されたときにOnAddとOnDelは仕事でいないtriggerdされていますTask Img

+0

は、それはあなたが '((WTab)成分).Controls.Remove(((WTab)コンポーネント).SelectedTab)を交換する必要があること、である可能性があります。'に ''((WTab)成分)とOnDel'。 Controls.Clear(); '? –

+0

OnDelメソッドは、私の理解でコントロールが削除されたときにトリガーされません。右上の三角形を押すとTabControlタスクで使用されます。 –

+0

'WTab'のDisposeメソッドをオーバーライドしますか?あなたのコントロール 'WTab'と' WTabPage'のコードを投稿できますか?特にページが追加/削除されるコードと処理方法。 –

答えて

0

@Pablo notPicassoに基づいて彼の答えは、動作するように見える次のコードを使用しました。これに何らかの不具合がありますか?

 protected override void Dispose(bool disposing) 
    { 
     IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost)); 
     if (designerHost != null) 
     { 
      for (int i = ((WTab)Component).Controls.Count - 1; i >= 0; i--) 
      { 
       // 
       var tab = ((WTab)Component).Controls[i]; 

       // 
       ((WTab)Component).Controls.Remove(tab); 

       // 
       designerHost.DestroyComponent(tab); 
      } 
     } 

     base.Dispose(disposing); 
    } 
0

あなたがIDesignerHost.DestroyComponent(...)を試してみましたか?

void OnDel(object sender, EventArgs e) 
{ 
    IDesignerHost designerHost = (IDesignerHost) GetService(typeof(IDesignerHost)); 
    if (designerHost != null) 
    { 
     var tab = ((WTab) Component).SelectedTab; 
     ((WTab) Component).Controls.Remove(tab); 
     designerHost.DestroyComponent(tab); 
    } 
} 
+0

OnDelは、TabControlがフォームから削除されたときにトリガされず、コントロールタスクに使用されます。例:https://i.stack.imgur.com/YQ4Y7.png –

関連する問題