2010-10-06 15 views
0

Salut tous Je crée un contrôle utilisateur personnalisé et je veux que ce contrôle personnalisé placé à l'intérieur d'une zone de liste déroulante. Je suis en utilisant ce code:Ajouter un contrôle personnalisé à ComboBox

public class UserControlComboBox : ComboBox, IMessageFilter 
{ 
    public readonly NimaDatePickerUC.MiladiNimaDatePickerUC UserControl = new NimaDatePickerUC.MiladiNimaDatePickerUC(); 

    protected override void WndProc(ref Message m) 
    { 
     if ((m.Msg == 0x0201) || (m.Msg == 0x0203)) 
     { 
      if (DroppedDown) 
       HideUserControl(); 
      else 
       ShowUserControl(); 
     } 
     else 
     { 
      base.WndProc(ref m); 
     } 
    } 

    public bool PreFilterMessage(ref Message m) 
    { 
     // intercept mouse events 
     if ((m.Msg >= 0x0200) && (m.Msg <= 0x020A)) 
     { 
      if (this.UserControl.RectangleToScreen(this.UserControl.DisplayRectangle).Contains(Cursor.Position)) 
      { 
       // clicks inside the user control, handle normally 
       return false; 
      } 
      else 
      { 
       // clicks outside the user controlcollapse it. 
       if ((m.Msg == 0x0201) || (m.Msg == 0x0203)) 
        this.HideUserControl(); 
       return true; 
      } 
     } 
     else return false; 
    } 

    public new bool DroppedDown 
    { 
     get { return this.UserControl.Visible; } 
    } 

    protected void ShowUserControl() 
    { 
     if (!this.Visible) 
      return; 

     this.UserControl.Anchor = this.Anchor; 
     this.UserControl.BackColor = this.BackColor; 
     this.UserControl.Font = this.Font; 
     this.UserControl.ForeColor = this.ForeColor; 

     // you can be cleverer than this if you need to 
     this.UserControl.Top = this.Bottom; 
     this.UserControl.Left = this.Left; 
     this.UserControl.Width = Math.Max(this.UserControl.Width, this.Width); 


     this.Parent.Controls.Add(this.UserControl); 
     this.Parent.Controls[0].BringToFront(); 
     this.UserControl.Visible = true; 
     this.UserControl.BringToFront(); 

     base.OnDropDown(EventArgs.Empty); 

     // start listening for clicks 
     Application.AddMessageFilter(this); 
    } 

    protected void HideUserControl() 
    { 
     Application.RemoveMessageFilter(this); 

     base.OnDropDownClosed(EventArgs.Empty); 
     this.UserControl.Visible = false; 
     this.Parent.Controls.Remove(this.UserControl); 

     // you probably want to replace this with something more sensible 
     this.Text = this.UserControl.Text; 
    } 

    protected override void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
      this.UserControl.Dispose(); 
     } 

     base.Dispose(disposing); 
    } 
} 

ce code fonctionne bien, mais si j'utilise Combo Box dans un récipient et le récipient plus petit que la taille COMBOBOX + Mon contrôle personnalisé, mon contrôle personnalisé ne bien apparaît pas et apparaît sous la zone de groupe. Comment puis-je montrer mon contrôle personnalisé devant tous les contrôles et les conteneurs?

+0

Est-ce un contrôle pour .NET 3.5? – EKet

+0

Ce contrôle utilise .Net 4.0 – Nima

Répondre

0

Cela peut sembler intuitif, mais avez-vous essayé le

[controlname].BringToFront(); 

également trouvé here

+0

Merci mais cela n'a pas fonctionné – Nima

+0

Vous voudrez peut-être chercher comment rendre votre contrôle au plus haut niveau. Il semble que c'est ce que vous essayez de faire. – EKet