Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting a tooltip using Reflection
Message
From
23/01/2006 12:34:48
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
Miscellaneous
Thread ID:
01089408
Message ID:
01089441
Views:
14
>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;
>               }
>          }
>
Eric,
Something like this works for me:
 private void SetPropertyValue(string pstrControlName, string pstrPropName, string pstrValue)
 {
	Control c = this.FindControl(pstrControlName);
	if ( c != null )
	{
		PropertyInfo pi = c.GetType().GetProperty(pstrPropName,pstrValue.GetType());
		if (pi != null && pi.CanWrite)
			pi.SetValue(c,pstrValue,null);
	}
 }
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform