2017-10-03 5 views
0

J'ai étendu DataGridView. Je souhaite ajouter le nouveau contrôle à la boîte à outils sans ajouter de référence à un assemblage différent (en utilisant l'option de clic droit "Choisir les éléments"). Le contrôle est dans le même projet du formulaire, et je ne veux pas les séparer. Comment puis-je atteindre cet objectif?Comment ajouter un contrôle étendu à la boîte à outils, lorsque le contrôle est dans le même projet?

Merci.

Modifier: Cela ne peut pas être une copie d'une question sur les contrôles utilisateur si ce n'est pas un contrôle utilisateur.

Edit 2: Le code lui-même (c'est un travail en cours Ce n'est pas fini.):

class BindedDataGrid<T> : DataGridView 
{ 
    public BindedDataGrid() 
    { 
     InitializeComponent(); 

     IEnumerable<PropertyInfo> properties = typeof(T).GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute))); 

     foreach (PropertyInfo property in properties) 
     { 
      var column = new DataGridViewColumn() 
      { 
       HeaderText = ((property.GetCustomAttributes(true)[0]) as BindingValueAttribute).Value 
      }; 

      Columns.Add(column); 
     } 
    } 
} 
+0

hmm. il est censé être défini comme l'un des composants du projet. apparaît dans l'onglet ProjectName_Components .. votre extension a hérité de UserControl? –

+0

@o_O - cette question concerne les contrôles étendus, pas les contrôles utilisateur. – Sipo

+0

Mon mauvais, [cela pourrait vous aider] (https://stackoverflow.com/questions/1116311/how-to-put-an-extended-winforms-control-on-toolbox) –

Répondre

0
public class BindedDataGrid : DataGridView 
    { 
     public BindedDataGrid() 
     { 
     } 

     public BindedDataGrid(Type bindType) 
     { 
      IEnumerable<PropertyInfo> properties = bindType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute))); 

      foreach (PropertyInfo property in properties) 
      { 
       var prop = property.GetCustomAttributes(true)[0]; 
       if (prop is BindingValueAttribute) 
       { 
        var column = new DataGridViewColumn() 
        { 
         HeaderText = property.Name 
        }; 
        column.CellTemplate = new DataGridViewTextBoxCell(); 
        Columns.Add(column); 
       } 
      } 
     } 
    } 

    public class BindingValueAttribute : Attribute 
    { 
     public string Value { get; set; } 
    } 

    public class BindOne 
    { 
     [BindingValueAttribute()] 
     public string Name { get; set; } 

     [BindingValueAttribute()] 
     public int Age { get; set; } 
    } 
  1. d'abord faire aucun BindedDataGrid générique
  2. créer un non constructeur paramétré

    private void InitializeComponent() 
    { 
        this.bindedDataGrid1 = new PlayingWithThread.BindedDataGrid(typeof(BindOne)); 
        ((System.ComponentModel.ISupportInitialize)(this.bindedDataGrid1)).BeginInit();