tomatomax.net

Every rule has its exception.

Archive for the C# Category

ユニークなIDを取得する

ここらを参考に。Sigmarion3ではUUIDの方を取得しろとのことなので。

using System;
using System.Runtime.InteropServices;

public class Sigmarion3
{
private static Int32 FILE_DEVICE_HAL = 0x00000101;
private static Int32 FILE_ANY_ACCESS = 0x0;
private static Int32 METHOD_BUFFERED = 0x0;

private static Int32 IOCTL_HAL_GET_UUID =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) | ((13) << 2) | (METHOD_BUFFERED);

[DllImport("coredll.dll")]
private static extern bool KernelIoControl(Int32 IoControlCode,
IntPtr InputBuffer,
Int32 InputBufferSize,
byte[] OutputBuffer,
Int32 OutputBufferSize,
ref Int32 BytesReturned);

public static byte[] GetID()
{
byte[] OutputBuffer = new byte[16];
Int32 OutputBufferSize, BytesReturned;
OutputBufferSize = OutputBuffer.Length;
BytesReturned = 0;

bool retVal = KernelIoControl(IOCTL_HAL_GET_UUID, IntPtr.Zero, 0, OutputBuffer, OutputBufferSize, ref BytesReturned);

if (!retVal)
{
return null;
}

return OutputBuffer;
}
}

エミュレータだと取得に失敗するなぁ。実機ならおk

ソート

いまさらソートが必要になってきたりしたので、Array.Sortメソッドでさくっと。intからstring等、配列の各要素がIComparableインターフェースの実装なら丸投げでいい。アルゴリズムはクイックソート。要素同士の比較方法を変えたいときはIComparer.Compareを実装したクラスを引数に渡してやれば。

Tabキーでフォーカスを変える

Tabキーでフォーカスを変える動作はCompactFramework SP2で対応したらしい。フォーカスをフォーム上のコントロールに設定しておく必要があるようで。けどエミュレータでテストしたところ動かない。その答えがGoogleのキャッシュに残ってた(cache:7YT6Ry2vjBAJ:thedotnet.com/howto/work338269.aspx – Google 検索)

As far as I remember, this is a know problem with emulators. The Tab does
not work there. If you have an input panel installed on the emulator, you
can use it to press the Tab key, it should work.

エミュレータが悪いようで。実機でやったら反応したよorz

このときフォーカスが移る順番は

this.Controls.Add(this.Textbox1);
this.Controls.Add(this.Textbox2);
this.Controls.Add(this.OKbutton);
this.Controls.Add(this.Cancelbutton);

で加えた順番になる(ここではTextbox1→Textbox2→OKbutton→Cancelbutton→Textbox1→…)