Friday, June 29, 2007

Mihin Air Flyies to Thailand!

yeap this is some thing that i have been waiting for a long time !

http://www.mihinlanka.com/

Monday, June 25, 2007

Windows ListView with subitem images

i was assign to migrate our application (Richfocus) from freamwork 1.1 to 2.0. one portion which is using the listview control stoped working suddenly. all the program code works fine in 1.1 but not in 2.0. It was a extended version of the ListView to add images to it's sub items, finally i managed to fix the code by over writing with this code.

i have pasted the code here, so u might save some time.

using System;
using System.Runtime.InteropServices;
public class ListViewEx : System.Windows.Forms.ListView
{
private const int LVIF_IMAGE = 0x2;
private const int LVS_EX_SUBITEMIMAGES = 0x2;
private const int LVM_FIRST = 0x1000;
private const int LVM_GETITEMA = LVM_FIRST + 5;
private const int LVM_GETITEMW = LVM_FIRST + 75;
private static readonly int LVM_GETITEM = (Marshal.SystemDefaultCharSize
== 1) ? LVM_GETITEMA : LVM_GETITEMW;
private const int LVM_SETITEMA = LVM_FIRST + 6;
private const int LVM_SETITEMW = LVM_FIRST + 76;
private static readonly int LVM_SETITEM = (Marshal.SystemDefaultCharSize
== 1) ? LVM_SETITEMA : LVM_SETITEMW;
private const int LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr
wParam, ref LVITEM lParam);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr
wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct LVITEM
{
public int mask;
public int iItem;
public int subItem;
public int state;
public int stateMask ;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
}

private bool ListView_SetItem(IntPtr hwnd, ref LVITEM lvi)
{
int result = SendMessage(hwnd, LVM_SETITEM, IntPtr.Zero, ref lvi);
return (result == 0) ? false : true;
}

private bool ListView_GetItem(IntPtr hwnd, ref LVITEM lvi)
{
int result = SendMessage(hwnd, LVM_GETITEM, IntPtr.Zero, ref lvi);
return (result == 0) ? false : true;
}

private void ListView_SetExtendedListViewStyleEx(IntPtr hwnd, int mask,
int style)
{
SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, new IntPtr(mask),
new IntPtr(style));
}

private void ListView_SetSubItemImageIndex(IntPtr hwnd, int index, int
subItemIndex, int imageIndex)
{
LVITEM lvi = new LVITEM();
lvi.iItem = index;
lvi.subItem = subItemIndex;
lvi.iImage = imageIndex;
lvi.mask = LVIF_IMAGE;
ListView_SetItem(hwnd, ref lvi);
}

private int ListView_GetSubItemImageIndex(IntPtr hwnd, int index, int
subItemIndex)
{
LVITEM lvi = new LVITEM();
lvi.iItem = index;
lvi.subItem = subItemIndex;
lvi.mask = LVIF_IMAGE;
if (ListView_GetItem(hwnd, ref lvi))
return lvi.iImage;
else
return -1;
}

protected override void OnCreateControl()
{
base.OnCreateControl();
ListView_SetExtendedListViewStyleEx(this.Handle,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES);
}

public void SetSubItemImageIndex(int index, int subItemIndex, int
imageIndex)
{
ListView_SetSubItemImageIndex(this.Handle, index, subItemIndex,
imageIndex);
}

public int GetSubItemImageIndex(int index, int subItemIndex)
{
return ListView_GetSubItemImageIndex(this.Handle, index,
subItemIndex);
}
}

Where is WMSDefine namespace in SP2 ?

I was working on a windows media service plug-in for my bro. When i installed it in Windows |Server 2003 standard version PC it didn't work because custom plug-in doesn't support in Windows 2003 stranded version either :) dammm

So I moved everything i had in to a 64 bit Windows Sever 2003 SP2 PC. nothing worked. i looked over and over thu a stupid error code in Google and didn't help it either.

So i compiled everything on top of Windows Freamwork 2.0, now WMSDefine enum is missing in the latest windowsmediaservice.dll ahh frustrating.. so i reflector to copy the WMSDefined class from 1.1 to my project and it worked.

simply this is why i dont like MS,

Friday, June 01, 2007

Auto Size Columns in the DataGrid ?

do u want to Auto Size Columns in the DataGrid ? here is the code

Public Sub AutoSizeGrid(ByVal Grid As DataGrid)
Try
Dim t As Type = grdInfo.GetType
Dim m As MethodInfo = t.GetMethod("ColAutoResize", _
BindingFlags.NonPublic Or BindingFlags.Instance)
Dim start As Integer
start = grdInfo.FirstVisibleColumn
Dim i As Integer = 0
While i < grdInfo.VisibleColumnCount OrElse i = 0
m.Invoke(Grid, New Object() {i + start})
i = i + 1
End While
Catch ex As Exception
m_parentForm.imcMessege.LogErrorMessage(Me, ex)
End Try
End Sub

need a combobox in DataGrid ?

I having been tring to add a combobox to the datagrid. here is da code


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;



// Derive class from DataGridTextBoxColumn
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
// Hosted ComboBox control
private ComboBox comboBox;
private CurrencyManager cm;
private int iCurrentRow;

// Constructor - create combobox, register selection change event handler,
// register lose focus event handler
public DataGridComboBoxColumn()
{
this.cm = null;

// Create ComboBox and force DropDownList style
this.comboBox = new ComboBox();
this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;

// Add event handler for notification of when ComboBox loses focus
this.comboBox.Leave += new EventHandler(comboBox_Leave);
}

// Property to provide access to ComboBox
public ComboBox ComboBox
{
get { return comboBox; }
}

// On edit, add scroll event handler, and display combo box
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
Debug.WriteLine(String.Format("Edit {0}", rowNum));
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

if (!readOnly && cellIsVisible)
{
// Save current row in the datagrid and currency manager associated with
// the data source for the datagrid
this.iCurrentRow = rowNum;
this.cm = source;

// Add event handler for datagrid scroll notification
this.DataGridTableStyle.DataGrid.Scroll += new EventHandler(DataGrid_Scroll);

// Site the combo box control within the bounds of the current cell
this.comboBox.Parent = this.TextBox.Parent;
Rectangle rect = this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();
this.comboBox.Location = rect.Location;
this.comboBox.Size = new Size(this.TextBox.Size.Width, this.comboBox.Size.Height);

// Set combo box selection to given text
this.comboBox.SelectedIndex = this.comboBox.FindStringExact(this.TextBox.Text);

// Make the ComboBox visible and place on top text box control
this.comboBox.Show();
this.comboBox.BringToFront();
this.comboBox.Focus();
}
}

// Given a row, get the value member associated with a row. Use the value
// member to find the associated display member by iterating over bound datasource
protected override object GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum)
{

if(this.comboBox.DataSource == null) return null;

Debug.WriteLine(String.Format("GetColumnValueAtRow {0}", rowNum));
// Given a row number in the datagrid, get the display member
object obj = base.GetColumnValueAtRow(source, rowNum);

// Iterate through the datasource bound to the ColumnComboBox
// Don't confuse this datasource with the datasource of the associated datagrid
CurrencyManager cm = (CurrencyManager) (this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
// Assumes the associated DataGrid is bound to a DataView, or DataTable that
// implements a default DataView
DataView dataview = ((DataView)cm.List);

int i;

for (i = 0; i < dataview.Count; i++)
{
if (obj.Equals(dataview[i][this.comboBox.ValueMember]))
break;
}

if (i < dataview.Count)
return dataview[i][this.comboBox.DisplayMember];

return DBNull.Value;
}

// Given a row and a display member, iterating over bound datasource to find
// the associated value member. Set this value member.
protected override void SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum, object value)
{
Debug.WriteLine(String.Format("SetColumnValueAtRow {0} {1}", rowNum, value));
object s = value;

// Iterate through the datasource bound to the ColumnComboBox
// Don't confuse this datasource with the datasource of the associated datagrid
CurrencyManager cm = (CurrencyManager)
(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
// Assumes the associated DataGrid is bound to a DataView, or DataTable that
// implements a default DataView
DataView dataview = ((DataView)cm.List);
int i;

for (i = 0; i < dataview.Count; i++)
{
if (s.Equals(dataview[i][this.comboBox.DisplayMember]))
break;
}

// If set item was found return corresponding value, otherwise return DbNull.Value
if(i < dataview.Count)
s = dataview[i][this.comboBox.ValueMember];
else
s = DBNull.Value;

base.SetColumnValueAtRow(source, rowNum, s);
}

// On datagrid scroll, hide the combobox
private void DataGrid_Scroll(object sender, EventArgs e)
{
Debug.WriteLine("Scroll");
this.comboBox.Hide();
}

// On combo box losing focus, set the column value, hide the combo box,
// and unregister scroll event handler
private void comboBox_Leave(object sender, EventArgs e)
{


try
{

if(this.comboBox.DisplayMember == "")
{
this.comboBox.Hide();
return;
}

if(this.comboBox.SelectedItem == null)
{
this.comboBox.Hide();
return;
}

DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;
string s = (string) rowView.Row[this.comboBox.DisplayMember];
Debug.WriteLine(String.Format("Leave: {0} {1}", this.comboBox.Text, s));

SetColumnValueAtRow(this.cm, this.iCurrentRow, s);
Invalidate();

this.comboBox.Hide();
this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler(DataGrid_Scroll);

}
finally
{
this.comboBox.Hide();
}

}
}