Friday, August 04, 2006

Exposing hidden properties in Compact Framework Datagrid

The Datagrid given in Compact Framework is usually very basic. This is good for those who just want to great a simple application for demo purpose. But when you want to create business application, you will probably looking for more specific features to enhance the usability of the datagrid.

The challenge comes when you start realizing certain properties are not exposed during run time. For example, you may want to control the row hieght of respective row in the datagrid.
Frustrated about Datagrid's limitation, you may be looking around for third party controls to replace it. However, just before you do that, you may want to find out some unexposed properties in datagrid (or any other controls, as a matter of fact) via reflection.

Here is a sample of how to expose these properties.


1    Public Sub GetGridProperties(ByVal dg As DataGrid)
2 Dim fieldInfos As System.Reflection.FieldInfo() = _
3 dg.GetType.GetFields(BindingFlags.NonPublic Or _
4 BindingFlags.Static Or BindingFlags.Instance)
5
6 For Each fieldInfo As System.Reflection.FieldInfo In fieldInfos
7 Debug.WriteLine(fieldInfo.Name & "," & fieldInfo.FieldType.ToString)
8 Next
9 End Sub

After you run this code, the following properties are exposed for your perusal.
m_kcxDefaultColWidth System.Int32
m_kfDefaultVScrollVisible System.Boolean
m_kfDefaultHScrollVisible System.Boolean
m_kfDefaultColumnHeadersVisible System.Boolean
m_kfDefaultRowHeadersVisible System.Boolean
v_xyDpi System.Int32
v_cxDefaultWidth System.Int32
v_cyDefaultHeight System.Int32
v_cxyDefaultBorderWidth System.Int32
v_cxyDefaultGridLineWidth System.Int32
v_nDefaultTextInset System.Int32
v_cxyDefaultResizePixel System.Int32
v_cxDefaultRowHdrWidth System.Int32
v_cxAutoGenColWidth System.Int32
v_cxyMinRowColWidth System.Int32
m_renderer System.Windows.Forms.GridRenderer
m_tabstycol System.Windows.Forms.GridTableStylesCollection
m_tabstyActive System.Windows.Forms.DataGridTableStyle
m_objDataSource System.Object
m_cmData System.Windows.Forms.CurrencyManager
m_rlrow System.Collections.ArrayList
m_pdcolData System.ComponentModel.PropertyDescriptorCollection
m_fListHandlerActive System.Boolean
m_irowVisibleFirst System.Int32
m_irowVisibleLast System.Int32
m_crowVisible System.Int32
m_cyRow System.Int32
m_fRowHeadersVis System.Boolean
m_icolVisibleFirst System.Int32
m_icolVisibleLast System.Int32
m_ccolVisible System.Int32
m_cyColumnHeader System.Int32
m_fColHeadersVis System.Boolean
m_fValidColsSet System.Boolean
m_icolValidFirst System.Int32
m_icolValidLast System.Int32
m_curcell System.Windows.Forms.DataGridCurrentCell
m_fColResizeActive System.Boolean
m_fRowResizeActive System.Boolean
m_irowcolResize System.Int32
m_xyResizeCur System.Int32
m_xyResizeLast System.Int32
m_xyResizeMax System.Int32
m_xyResizeMin System.Int32
m_fRowSelectDragActive System.Boolean
m_sbHorz System.Windows.Forms.HScrollBar
m_sbVert System.Windows.Forms.VScrollBar
m_yRenderLast System.Int32
m_xRenderLast System.Int32
CurrentCellChanged System.EventHandler
m_hwn System.IntPtr
m_gch System.Runtime.InteropServices.GCHandle
m_wnt Microsoft.AGL.Forms.WNT
m_thread System.Threading.Thread
m_mnucOwned System.Windows.Forms.ContextMenu
m_mnucFloating System.Windows.Forms.ContextMenu
m_qutaskInvoke System.Collections.Queue
m_dataBindings System.Windows.Forms.ControlBindingsCollection
m_bindingContext System.Windows.Forms.BindingContext
m_fDisposed System.Boolean
m_fCalledDispose System.Boolean
m_fDestroyed System.Boolean
m_fEnabledReal System.Boolean
m_objTag System.Object
m_strName System.String
m_cLayoutSuspend System.Int32
m_fScrollableSav System.Boolean
m_fMousePressed System.Boolean
m_fFocusAttempt System.Boolean
m_fSkipValidation System.Boolean
m_bsRequiredScaling System.Windows.Forms.BoundsSpecified
m_cSuspendValidation System.Int32

You will immediately find some of the properties useful - m_rlrow returns arraylist of all rows in the datagrid. you can continue to expose more in depth properties Using reflection by simply drilling down each property. m_cy is exposed if you get fieldinfo under each row m_rlrow arraylist. m_cy return the row hieght of a particular row.

There are a lot more properties to look for, you just need to browse through them and find the property that is useful to you. If you can't find the properties you need, then you can consider third party controls.

Friday, June 30, 2006

Using correct textbox event handler

You should come across scenario where you need to validate value of a textbox and then do some calculation after someone enter value into a textbox and leave the field. Typically we will just LostFocus event and perform the two tasks in this event handler block.

In .net, you should split your code to Validating and Validated events. The following code is a sample.
    
Private Sub txtCubicCapacity_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCubicCapacity.Validating
        If txtCubicCapacity.Text.Length > 0 Then
            If Not ValidateCubicCapacity(txtCubicCapacity.Text) Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub txtCubicCapacity_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCubicCapacity.Validated
        If txtCubicCapacity.Text.Length > 0 Then
            txtVehicleClass.Text = GetVehicleClassByCubicCapacity(txtCubicCapacity.Text)
        End If
    End Sub

    Public Function ValidateCubicCapacity(ByVal CubicCapacity As String) As Boolean
        ValidateCubicCapacity = False

        If Not IsNumeric(CubicCapacity) Then
            MsgBox("Cubic Capacity must be in numeric.", MsgBoxStyle.Exclamation, "Validation")
            Exit Function
        End If

        If Not Convert.ToInt32(CubicCapacity) > 0 Then
            MsgBox("Cubic Capacity must be more than 0.", MsgBoxStyle.Exclamation, "Validation")
            Exit Function
        End If

        ValidateCubicCapacity = True
End Function
The reason why you should use Validating event to do validation code is because this event expose e.Cancel property within the event handler so that if validation fails, you just need to set this property to false and exit the block. The textbox will acknowledge the validation failure and set focus back to textbox.

The reason that calculation code should be in Validated event instead of LostFocus is that Validated event is the event raised immediately after validated. This ensures that your code is executed only after proper validation is successfully done.

Thursday, June 22, 2006

Medical Problem vs. Economic Solution

Did any of you realize that food store out that is cutting down sugar input because of recent sugar shortage? Did you feel you teh tarik is less sweet even though you didn't ask for less sugar?

I felt it and I think this is great measures to help our country from suffering a diabetic illness. As the sugar intake is reduce, our body has less sugar spite (attack). Thus, cause less disturbance to you body.

Sometime, the medical problem can be solved by an economic solution... :)

Tuesday, June 20, 2006

Memory leek in VS2005 VB Compiler

Some of you may be experiencing slowness in VS2005 IDE. It may be cause by a reported memory leek in VB Compiler when opening large vb projects. There is a hotfix where you can install to see if it will improve the performance.

I have experienced this problem on my Pentium D 1GB box. I develop enterprise application that usually spans across many projects. Before apply the hotfix, it took me more than 30 seconds to do a local compilation. The memory utilization on devenv.exe will shoot up to 800MB while maintaing 400MB during development.

Another strange thing is VS2005 IDE will automatically save recovery information from time to time. It took more than 20 seconds to do each save!! It is driving me nuts.

After applying the hotfix, devenv.exe is now maximum 240MB and I don't have to wait while developing anymore.

Here is the KB Microsoft published about the hotfix. Aparently, you need to call the support centre in order to get the email to download the hotfix.

If any of you have problem getting the hotfix, just drop me a comment. I can email it to you.

Monday, June 19, 2006

Aussies Rules!!

watched Aussies vs Brazil game yesterday. It feels great just to see Brazil struggled to score. :)