Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Setting a tooltip using Reflection
Message
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Setting a tooltip using Reflection
Versions des environnements
Environment:
ASP.NET
Divers
Thread ID:
01089408
Message ID:
01089408
Vues:
94
I try to set the Tooltip property of TextBox and ImageButton controls using Reflection but I can't get it.

Any code VB.Net or C# will do.

Here the code I have so far (which gives me the exception "Object does not match target type."):
          private static void SetTooltip(object pForm, string pstrControlName, string pstrPropName, string pstrValue)
          {
               try
               {
                    object ctlX;

                    try
                    {
                         if (pstrControlName.Trim().ToUpper() == "ME")
                         {
                              ctlX = pForm;
                         }
                         else
                         {
                              ctlX = pForm.GetType().GetProperty(pstrControlName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance).GetValue(pForm, null);
                         }
                    }
                    catch 
                    {
                         //     Le contrôle n'a pas été trouvé!
                         return;
                    }
                    try
                    {
                         Type MyType = ctlX.GetType();
//                         System.Reflection.PropertyInfo Mypropertyinfo = MyType.GetProperty(pstrPropName);
//                    
//                         Mypropertyinfo.SetValue(ctlX, pstrValue, null);

                         foreach(System.Reflection.PropertyInfo pi in MyType.GetProperties())
                         {
                              if (pi.Name.ToUpper() == "AlternateText".ToUpper() && pi.CanWrite)
                              {
                                   pi.SetValue(MyType, pstrValue, null);
                              }
                              if (pi.Name.ToUpper() == pstrPropName.ToUpper() && pi.CanWrite)
                              {
                                   System.Reflection.MethodInfo methodInfo = pi.GetSetMethod(true); 
                                   System.Reflection.BindingFlags bindingFlags = System.Reflection.BindingFlags.SetProperty; 
                                   if (methodInfo.IsStatic) 
                                        bindingFlags |= System.Reflection.BindingFlags.Static; 
                                   pi.SetValue(MyType, pstrValue, bindingFlags, null, null, null); 

//                                   pi.SetValue(MyType, pstrValue, null);
                              }
                         }

                    }
                    catch (Exception ex)
                    {
                         return;
                    }
               }
               catch (Exception ex)
               {
                    int i = 0;
               }
          }
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform