.Net ramblings
# Tuesday, 24 April 2012
Using XStandard dll on a Win 64-bit

I have a winforms app that ran into trouble with the XStandard XHTML component on a 64 bit system. I was registering the OCX file from code if it didn't register via the MSI. found this kb article useful, and then called the appropriate version of regsvr32.exe based on the platform.

try
{
    // attempt to load the control 
    this.pageEditor = new PageEditor();
}
catch (System.Runtime.InteropServices.COMException)
{
    // control loading failed
    string ocxPath = Path.Combine(Application.StartupPath, "Xstandard.ocx");
    try
    {
        // OCX is not registered, register it now using the Syswow64 version of regsvr32.exe if we are using a 64 bit system
        Process p = new Process();
        p.StartInfo.FileName = @"regsvr32.exe";
        p.StartInfo.WorkingDirectory = (IntPtr.Size == 8) ? @"%SystemRoot%\Syswow64" : @"%SystemRoot%\system32";  // IntPtr is size 8 on a 64 bit system
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.Arguments = String.Format("/s \"{0}\"", ocxPath);
        p.Start();
        p.Close();
        MessageBox.Show("Please restart... (OCX file registered)", "Restart");
        Application.Exit();
        return;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Unable to register OCX file: " + ex.Message, "Error");
        Application.Exit();
        return;
    }
}

Tuesday, 24 April 2012 20:51:07 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net Windows Forms

# Monday, 16 January 2012
.Net windows forms Multi-line ComboBox / text-wrapping
seems like the most basic requirement for a list control... but there we are.  use this as a replacement for the System.Windows.Forms.ComboBox.

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace HortLaptopApp
{
    class ComboBoxWrap : ComboBox
    {
        // ref http://stackoverflow.com/questions/1245530/unable-to-set-the-dropdownheight-of-combobox
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }

        public const int SWP_NOZORDER = 0x0004;
        public const int SWP_NOACTIVATE = 0x0010;
        public const int SWP_FRAMECHANGED = 0x0020;
        public const int SWP_NOOWNERZORDER = 0x0200;

        public const int WM_CTLCOLORLISTBOX = 0x0134;

        private int _hwndDropDown = 0;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_CTLCOLORLISTBOX)
            {
                if (_hwndDropDown == 0)
                {
                    _hwndDropDown = m.LParam.ToInt32();

                    RECT r;
                    GetWindowRect((IntPtr)_hwndDropDown, out r);

                    //int newHeight = 0;
                   // for(int i=0; i<Items.Count && i < MaxDropDownItems; i++)
                    //    newHeight += this.GetItemHeight(i);

                    int total = 0;
                    for (int i = 0; i < this.Items.Count; i++)
                        total += this.GetItemHeight(i);
                    this.DropDownHeight = total + SystemInformation.BorderSize.Height * (this.Items.Count + 2);
           

                    SetWindowPos((IntPtr)_hwndDropDown, IntPtr.Zero,
                        r.Left,
                                 r.Top,
                                 DropDownWidth,
                                 DropDownHeight,
                                 SWP_FRAMECHANGED |
                                     SWP_NOACTIVATE |
                                     SWP_NOZORDER |
                                     SWP_NOOWNERZORDER);
                }
            }

            base.WndProc(ref m);
        }

        protected override void OnDropDownClosed(EventArgs e)
        {
            _hwndDropDown = 0;
            base.OnDropDownClosed(e);
        }

        public ComboBoxWrap() : base()
        {
            // add event handlers
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
            this.DrawItem += new DrawItemEventHandler(ComboBoxWrap_DrawItem);
            this.MeasureItem += new MeasureItemEventHandler(ComboBoxWrap_MeasureItem);
        }

        void ComboBoxWrap_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            // set the height of the item, using MeasureString with the font and control width
            ComboBoxWrap ddl = (ComboBoxWrap)sender;
            string text = ddl.Items[e.Index].ToString();
            SizeF size = e.Graphics.MeasureString(text, this.Font, ddl.DropDownWidth); 
            e.ItemHeight = (int)Math.Ceiling(size.Height) + 1;  // plus one for the border
            e.ItemWidth = ddl.DropDownWidth;
            System.Diagnostics.Trace.WriteLine(String.Format("Height {0}, Text {1}", e.ItemHeight, text));
        }

        void ComboBoxWrap_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;

            // draw a lighter blue selected BG colour, the dark blue default has poor contrast with black text on a dark blue background
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                e.Graphics.FillRectangle(Brushes.PowderBlue, e.Bounds);
            else
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            
            // get the text of the item
            ComboBoxWrap ddl = (ComboBoxWrap)sender;
            string text = ddl.Items[e.Index].ToString();

            // don't dispose the brush afterwards
            Brush b = Brushes.Black;
            e.Graphics.DrawString(text, this.Font, b, e.Bounds, StringFormat.GenericDefault);
            
            // draw a light grey border line to separate the items
            Pen p = new Pen(Brushes.Gainsboro, 1);
            e.Graphics.DrawLine(p, new Point(e.Bounds.Left, e.Bounds.Bottom-1), new Point(e.Bounds.Right, e.Bounds.Bottom-1));
            p.Dispose();
            
            e.DrawFocusRectangle();
        }
    }
}


Monday, 16 January 2012 10:08:16 (GMT Standard Time, UTC+00:00)  #    Comments [1]  .Net Windows Forms

# Sunday, 01 January 2012
Locating the TypeGuessRows registry key on Windows 64-bit operating systems

Data truncated to 255 characters with Excel Jet/ODBC driver: http://support.microsoft.com/kb/189897

Solution: http://support.sas.com/kb/31/765.html
My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel

Set to 0 to avoid the truncation problem!


Sunday, 01 January 2012 22:19:39 (GMT Standard Time, UTC+00:00)  #    Comments [0]  .Net General | Database

# Wednesday, 28 December 2011
Excel interop with .Net / Server 2008
I'm using Asp.Net to open excel files and read the values cell by cell, normally i prefer to import Excel files as a datatable, but some documents don't fit the strict rows/columns format required for importing into a DataTable.  the code i'm using is based on this from dotnetperls.  worked fine for the most part on my dev PC but I had a few issues trying to get this to work on a 32bit version of excel on a 64 bit Windows 2008 server.
here's what i had to do to get it working:
  • disable macros (throwing exceptions opening the workbook), requires a reference to the Microsoft.Office.Core assembly:
    excelApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;   // disable macros
  • give launch / open permissions to the Excel application in DCOM config, this was tricky because it isn't shown in a 64 bit OS if the program is 32 bit, i found the answer here.
  • set the identity in DCOM for Excel to the interactive user.



Wednesday, 28 December 2011 21:16:30 (GMT Standard Time, UTC+00:00)  #    Comments [0]  .Net General | Asp.Net

# Friday, 14 October 2011
Asus P6T deluxe v2 overclock settings with Intel i7 920
Found a safe/low-temp overclock @ 3.6Ghz for this board. Using 1600Mhz DDR3 RAM. standard air cooling in a z-machine GT1000 case.

RAM set to XMP profile #1. 8/8/8/24/1.66V/1.35v
BCLK:180
PCIE:100
Speedstep/turbotech:enable
RAM:~1450 Mhz
CPU Volt:1.25V
QPI/DRAM:1.35V
DRAM bus:1.66v

everything else auto/default
CPU Fan and chassis fan set to Standard profile.

44c @ idle. 66c under orthos CPU stress test.  The 1.25 CPU voltage keeps the temps nice and low, and it's very stable.  the 920 does this o'c with ease, could probably go lower but stability is more of a priority for a software dev PC.


Friday, 14 October 2011 13:42:08 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  General

# Monday, 24 January 2011
Playing a DVD remotely from a network share
you can rip a DVD and then stream the file to a remote computer, or with VLC media player you can stream the DVD itself, but that takes a bit of setting up.  i wanted to pop a DVD into my desktop PC and then play it remotely from a netbook plugged in to the TV.  so i share out the DVD drive on the desktop, all well and good, i can see the VIDEO_TS and AUDIO_TS folders on the netbook, but there isn't an Autoplay option or any way to 'start' the DVD.  i found a useful tip from the VLC forums, you can drag the VIDEO_TS folder into VLC (on the netbook, for example) and then it will start playing the DVD, with full support for menus and chapters etc.  The bit i got stuck on was that you have to start the DVD for a few seconds on the host PC (desktop in my case) which authenticates the disc and allows it to be streamed. 

not very high tech, but handy, if you ever find yourself without a DVD player.


Monday, 24 January 2011 20:43:52 (GMT Standard Time, UTC+00:00)  #    Comments [0]  General

# Tuesday, 14 September 2010
Gridview Delete via LinqDataSource fails with ChangeConflictException: Row not found or changed
if you use SqlMetal to generate DataContext classes against an SQL database, you might run into some problems with GridView and deleting a row, when using a LinqDataSource.
this was a very frustrating problem to track down, apparently there is a bug in the LinqDataSource with datetime fields.  i kept getting this error: ChangeConflictException: Row not found or changed
and there was no apparent reason why it was happening, because the same code worked for other tables.  I eventually narrowed it down to the only difference between the two tables, a non nullable datetime field. changing this field to nullable removed the problem.
this thread was useful in troubleshooting the problem.
i also found that calling DataBind() on the LinqDataSource before re-binding the GridView helped get rid of this error message.


Tuesday, 14 September 2010 12:50:05 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net General | Database

# Thursday, 17 June 2010
Nokia Ovi Suite Calendar Sync Cancelled error message
I have a nokia E52 and in general i'm very happy with the Nokia Ovi Suite software.  But today it refused to synchronise my calendar with a "Sync Cancelled" error message, other items synchronised fine.  The solution was to reset the nokia profile/files on my computer.  Make sure to close Outlook, Ovi Suite, and end the "nokiamserver.exe" process.  Then open up explorer and browse to C:\Users\Name\AppData\Local and C:\Users\Tim\AppData\Roaming.  You could delete these files, but just in case i renamed the "Nokia" and "Nokia Ovi Suite" folders to "xNokia" and "xNokia Ovi Suite".  If anything goes wrong you can always rename them back to their original names.
Then open up Ovi Suite again and reconnect the phone.  It should synchronise fine then, there must be some bug with the calendar synchronisation that can corrupt the local database.


Thursday, 17 June 2010 09:44:58 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  General

# Friday, 11 June 2010
Excel interop with Asp.Net
Ran into a permissions problem today using Excel interop code from within ASP.Net, which had worked fine from a windows forms application.
Thanks to 'Frosty' for his post which explains how to enable the appropriate permissions, reproducing it here in case the link ever goes down:

From command prompt / start->run... type dcomcnfg

Select Component Services->Computers->My Compter->DCOM Config

Scroll down and select Microsoft Excel Applicaton

Right click on Microsoft Excel Applicaton and select properties.

Select the Security tab

In Launch Permissions group box click Edit button.

Add the appropriate user for your particular situation. In my case, I
selected MyDomainName\Domain Users.

Make sure that Allow check box is checked for your appropriate user.

Click OK

In Access Permissions group box click Edit button.

Add the appropriate user for your particular situation. In my case, I
selected MyDomainName\Domain Users.

Make sure that Allow check box is checked for your appropriate user.

Click OK

Excel interop will now work via asp.net


Friday, 11 June 2010 18:32:37 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  Asp.Net

# Wednesday, 26 May 2010
Compact Framework - Using the Treeview to replace the ListBox
As any compact framework developer will tell you, the out-of-the-box ListBox control is a bit crap because it doesn't support horizontal scrolling.  what were they thinking??
anyway, after several work-arounds to this problem, i finally realised that the treeview control does support horizontal scrolling, and you can load up a treeview to look just like a listbox, having all the nodes added at the top level, i.e. a "flat" treeview.
the extra bonus with this approach is that Windows Mobile 6.5 has a brilliant touch friendly Treeview control that you can finger-drag up and down and left and right (at least the HTC HD2 has this, i can't vouch for other devices).

i've included a "Chooser" class i wrote to make user selections easy to code for.  to use the class, use the following approach, the SelectedItem string is kept as a static variable in the Form so you can close and dispose the form straight away, and still catch the "return" value of the form. If you pass in the parameter to use Checkboxes, you can access the CheckedItems static property which is a List<string>.
Chooser c = new Chooser("Select Item", new string[]{"Option 1", "Option 2", "Option 3"}, true, true); 
DialogResult d = c.ShowDialog();
c.Dispose();
if(d != DialogResult.Cancel)
MessageBox.Show("You chose " + Chooser.SelectedItem);
here's the class then:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace IbenzaMobileUtil
{
public partial class Chooser : Form
{
private MainMenu mainMenu1;
private MenuItem mmOK;
private MenuItem mmCancel;
private Label lblCaption;
public static string SelectedItem = null;
public static List<string> CheckedItems = null;
private TreeView treeView1;
private bool selectInvokesClose;


public Chooser()
{
InitializeComponent();
}

private Chooser(string Caption, bool FullScreen, bool SelectInvokesClose)
{
InitializeComponent();
CheckedItems = new List<string>();
SelectedItem = null;
this.selectInvokesClose = SelectInvokesClose;
this.lblCaption.Text = Caption;
this.mmOK.Enabled = false;

if (FullScreen)
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.ControlBox = false;
this.MinimizeBox = false;
}
}

public Chooser(string Caption, string[] Items, bool FullScreen, bool SelectInvokesClose)
: this(Caption, FullScreen, SelectInvokesClose)
{
this.treeView1.BeginUpdate();
foreach (string s in Items)
this.treeView1.Nodes.Add(s);
this.treeView1.EndUpdate();
}

public Chooser(string Caption, string[] Items, bool FullScreen, bool CheckBoxes, bool SelectInvokesClose, bool CheckAll)
: this(Caption, Items, FullScreen, SelectInvokesClose)
{
this.treeView1.CheckBoxes = CheckBoxes;
if (CheckBoxes && CheckAll)
foreach (TreeNode node in this.treeView1.Nodes)
node.Checked = true;
if(CheckBoxes)
this.mmOK.Enabled = true; // don't require any user input
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
this.mmOK.Enabled = true;
if (selectInvokesClose)
this.mmOK_Click(sender, EventArgs.Empty);
}

#region Windows Form Designer generated code

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}


/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mmCancel = new System.Windows.Forms.MenuItem();
this.mmOK = new System.Windows.Forms.MenuItem();
this.lblCaption = new System.Windows.Forms.Label();
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.mmCancel);
this.mainMenu1.MenuItems.Add(this.mmOK);
//
// mmCancel
//
this.mmCancel.Text = "Cancel";
this.mmCancel.Click += new System.EventHandler(this.mmCancel_Click);
//
// mmOK
//
this.mmOK.Text = "OK";
this.mmOK.Click += new System.EventHandler(this.mmOK_Click);
//
// lblCaption
//
this.lblCaption.Dock = System.Windows.Forms.DockStyle.Top;
this.lblCaption.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold);
this.lblCaption.Location = new System.Drawing.Point(0, 0);
this.lblCaption.Name = "lblCaption";
this.lblCaption.Size = new System.Drawing.Size(240, 27);
this.lblCaption.Text = "...";
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.Location = new System.Drawing.Point(0, 27);
this.treeView1.Name = "treeView1";
this.treeView1.ShowLines = false;
this.treeView1.ShowPlusMinus = false;
this.treeView1.ShowRootLines = false;
this.treeView1.Size = new System.Drawing.Size(240, 241);
this.treeView1.TabIndex = 12;
//
// Chooser
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(240, 268);
this.ControlBox = false;
this.Controls.Add(this.treeView1);
this.Controls.Add(this.lblCaption);
this.KeyPreview = true;
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Name = "Chooser";
this.Text = "Choose Item";
this.Load += new System.EventHandler(this.Chooser_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Chooser_KeyDown);
this.ResumeLayout(false);

}
#endregion

private void Chooser_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
this.mmOK_Click(sender, EventArgs.Empty);
}

private void mmOK_Click(object sender, EventArgs e)
{
if (this.treeView1.SelectedNode != null)
Chooser.SelectedItem = this.treeView1.SelectedNode.Text;
if (this.treeView1.CheckBoxes)
foreach (TreeNode n in this.treeView1.Nodes)
if (n.Checked)
CheckedItems.Add(n.Text);

this.DialogResult = DialogResult.OK;
this.Close();
}

private void mmCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}

private void Chooser_Load(object sender, EventArgs e)
{
this.treeView1.Focus();
this.treeView1.SelectedNode = null;
this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
}
}
}


Wednesday, 26 May 2010 15:51:34 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  .Net Compact