How to resize description area in PropertyGrid


From this article you can learn how to resize description area in PropertyGrid. Example shows use of reflection in order to dig to private members of PropertyGrid - not accessible in normal way.

Implementation is available both in C# and VB.Net languages. Use buttons below to switch language visibility.

Implementation

System.Reflection namespace needs to be put in usings.

private static void ChangeDescriptionHeight(PropertyGrid grid, int height)
{
  if (grid == null) throw new ArgumentNullException("grid");
 
  foreach (Control control in grid.Controls)
   if (control.GetType().Name == "DocComment")
    {
      FieldInfo fieldInfo = control.GetType().BaseType.GetField("userSized",
        BindingFlags.Instance |
        BindingFlags.NonPublic);
      fieldInfo.SetValue(control, true);
      control.Height = height;
      return;
    }
}

Private Shared Sub ChangeDescriptionHeight(grid As PropertyGrid, height As Integer)
  If grid Is Nothing Then
    Throw New ArgumentNullException("grid")
  End If
 
  For Each control As Control In grid.Controls
    If control.[GetType]().Name = "DocComment" Then
      Dim fieldInfo As FieldInfo = control.[GetType]().BaseType.GetField("userSized", 
	    BindingFlags.Instance Or BindingFlags.NonPublic)
      fieldInfo.SetValue(control, True)
      control.Height = height
      Return
      End If
  Next
End Sub

PS3Merge 1.0.1.0 release info


We are happy to announce another PS3Merge release!
  • Visit project summary page to get more details about general features.
  • Please feel free to leave a comment here on any usage difficulties or benefits of this version.
Give PS3Merge a try to see what it's really capable of!

Submitted changes:
  • Bug fix: There is not enough space on the disk issue.

Oracle regular expressions: search by pattern exact match

Here you can learn about finding the exact regular expression match using SQL query in Oracle. Article is made upon examples so you will be on the right track in no time.


Usually when we want to search Oracle database users table via regular expressions by email domain filter, it's easy:

SELECT * FROM users
WHERE regexp_like(mail, '@gmail\.com');

the above query will return all users which have an email in @gmail.com domain.

But lets say that we want to be more specific and we want to search the database for users whose name is John and last name is starting with "B". For this example, lets assume that some users email address format is "firstname.lastname@gmail.com".

If we use this statement:

SELECT * FROM users
WHERE regexp_like(mail, 'john\.b[a-zA-Z]*@gmail\.com');

some of the results will be what we want but a lot will be just a trash pile. This statement will return users with emails like "mark.john.bradley@gmail.com" or "john.thebadboy@gmail.com.uk". Why? Because regexp_like function checks if the given string contains a substring that matches our regular expression and NOT if the entire string matches the regular expression.

The solution to this problem i very simple. We must add two special characters. One, ^ (caret), at the beginning and the other, $ (dollar), at the end of our regular expression.

Quick explanation:
^ (caret) - means that the string starts with regular expression pattern.
$ (dollar) - means that the string ends with regular expression pattern.

Knowing this correct statement should look like this:

SELECT * FROM users
WHERE regexp_like(mail, '^john\.b[a-zA-Z]*@gmail\.com$');

PS3Merge 1.0.0.0 release info


We are happy to announce PS3Merge first release!
  • Visit project summary page to get more details about general features.
  • PS3Merge in this release connects to Karmian Update Service on application startup. Keeping all features and bug fixes up to date is necessary in our opinion.

Give PS3Merge a try to see what it's really capable of!

Assembly custom attributes helper

From this article you can learn how to define and use simple helper. Example shows generic methods returning list of any type of Attribute from Assembly. AssemblyHelper class can be easily extended with further functionalities as needed.

Implementation and examples are available both in C# and VB.Net languages.

Implementation

Below you can find implementation we use in Karmian Framework as Assembly helper. All members in AssemblyHelper class should be marked as static, so no instance is needed to invoke any. Class AssemblyHelper itself is marked as sealed as we do not want to allow any inheritance. If you need to add new functionality that supports the Assembly - it should be implemented in the class AssemblyHelper. This approach allows the system to prevent duplication of helpers that are responsible for similar actions.

AssemblyHelper class

using System;
using System.Reflection;
 
namespace Karmian.Framework.Helpers
{
  public sealed class AssemblyHelper
  {
    public static T[] GetCustomAttributes<T>(Assembly assembly) where T : Attribute
    {
      return GetCustomAttributes<T>(assembly, true);
    }
 
    public static T[] GetCustomAttributes<T>(Assembly assembly, bool inherit) where T : Attribute
    {
      if (assembly == null)
        throw new ArgumentNullException("assembly");
      var attributes = assembly.GetCustomAttributes(typeof (T), inherit) as T[];
      return attributes ?? new T[0];
    }
  }
}

Imports System.Reflection
 
Namespace Karmian.Framework.Helpers
  Public NotInheritable Class AssemblyHelper
    Public Shared Function GetCustomAttributes(Of T As Attribute)(assembly As Assembly) As T()
      Return GetCustomAttributes(Of T)(assembly, True)
    End Function
 
    Public Shared Function GetCustomAttributes(Of T As Attribute)(assembly As Assembly, inherit As Boolean) As T()
      If assembly Is Nothing Then
        Throw New ArgumentNullException("assembly")
      End If
      Dim attributes = TryCast(assembly.GetCustomAttributes(GetType(T), inherit), T())
      Return If(attributes, New T(-1) {})
    End Function
  End Class
End Namespace

How to use PS3Splitter screencasts

If you don't like to read how things work and instead you like to watch how it's done we have found some videos on PS3Splitter usage. We hope that the videos posted here will lift your dilemmas on basic questions with a series of 'how to'. Also if you know about some interesting article/tutorial about PS3Splitter please share it with us!



Enum description TypeConverter

From this article you can learn how to define and use enum description type converter so sytem will know how to handle enum to string and string to enum values (very helpful in PropertyGrid for example).

Implementation is available both in C# and VB.Net languages.

Implementation

Below you can find implementation we use in Karmian Framework as enum description type converter. It can be easily extended to work also as localizable enum description type converter - Description attribute can be used as a key of localized resource.

EnumDescriptionTypeConverter class

using System;
using System.ComponentModel;
using System.Globalization;
 
namespace Karmian.Core.TypeConverters
{
  public class EnumDescriptionTypeConverter : EnumConverter
  {
    public EnumDescriptionTypeConverter(Type type) : base(type)
    {
    }
 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
      return sourceType == typeof(string) || TypeDescriptor.GetConverter(typeof(Enum)).CanConvertFrom(context, sourceType);
    }
 
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
      if (value is string)
        return GetEnumValue(EnumType, (string) value);
      if (value is Enum)
        return GetEnumDescription((Enum) value);
      return base.ConvertFrom(context, culture, value);
    }
 
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
      return value is Enum && destinationType == typeof(string)
          ? GetEnumDescription((Enum)value)
          : (value is string && destinationType == typeof(string)
            ? GetEnumDescription(EnumType, (string)value)
            : base.ConvertTo(context, culture, value, destinationType));
    }
 
    public static string GetEnumDescription(Enum value)
    {
      var fieldInfo = value.GetType().GetField(value.ToString());
      var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
      return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
    }
 
    public static string GetEnumDescription(Type value, string name)
    {
      var fieldInfo = value.GetField(name);
      var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
      return (attributes.Length > 0) ? attributes[0].Description : name;
    }
 
    public static object GetEnumValue(Type value, string description)
    {
      var fields = value.GetFields();
      foreach (var fieldInfo in fields)
      {
        var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attributes.Length > 0 && attributes[0].Description == description)
          return fieldInfo.GetValue(fieldInfo.Name);
        if (fieldInfo.Name == description)
          return fieldInfo.GetValue(fieldInfo.Name);
      }
      return description;
    }
  }
}

Imports System.ComponentModel
Imports System.Globalization
 
Namespace Karmian.Core.TypeConverters
  Public Class EnumDescriptionTypeConverter
    Inherits EnumConverter
    Public Sub New(type As Type)
      MyBase.New(type)
    End Sub
 
    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
      Return sourceType Is GetType(String) OrElse TypeDescriptor.GetConverter(GetType([Enum])).CanConvertFrom(context, sourceType)
    End Function
 
    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
      If TypeOf value Is String Then
        Return GetEnumValue(EnumType, DirectCast(value, String))
      End If
      If TypeOf value Is [Enum] Then
        Return GetEnumDescription(DirectCast(value, [Enum]))
      End If
      Return MyBase.ConvertFrom(context, culture, value)
    End Function
 
    Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
      Return If(TypeOf value Is [Enum] AndAlso destinationType Is GetType(String), GetEnumDescription(DirectCast(value, [Enum])), (If(TypeOf value Is String AndAlso destinationType Is GetType(String), GetEnumDescription(EnumType, DirectCast(value, String)), MyBase.ConvertTo(context, culture, value, destinationType))))
    End Function
 
    Public Shared Function GetEnumDescription(value As [Enum]) As String
      Dim fieldInfo = value.[GetType]().GetField(value.ToString())
      Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
      Return If((attributes.Length > 0), attributes(0).Description, value.ToString())
    End Function
 
    Public Shared Function GetEnumDescription(value As Type, name As String) As String
      Dim fieldInfo = value.GetField(name)
      Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
      Return If((attributes.Length > 0), attributes(0).Description, name)
    End Function
 
    Public Shared Function GetEnumValue(value As Type, description As String) As Object
      Dim fields = value.GetFields()
      For Each fieldInfo As FieldInfo In fields
        Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
        If attributes.Length > 0 AndAlso attributes(0).Description = description Then
          Return fieldInfo.GetValue(fieldInfo.Name)
        End If
        If fieldInfo.Name = description Then
          Return fieldInfo.GetValue(fieldInfo.Name)
        End If
      Next
      Return description
    End Function
  End Class
End Namespace

Syndicate content