2009年9月28日 星期一

全形/半形 互換

  1. '半形轉全形
  2. Public Function ToWchr(ByRef data As String) As String
  3. Dim sb As New StringBuilder
  4. Dim ascii As Integer = 0

  5. For Each c As Char In data.ToCharArray()
  6. ascii = Convert.ToInt32(c)
  7. If ascii = 32 Then
  8. sb.Append(Convert.ToChar(12288))
  9. Else
  10. sb.Append(Convert.ToChar(ascii + IIf(ascii <>
  11. End If
  12. Next

  13. Return sb.ToString
  14. End Function

  15. ' 全形 轉 半形
  16. Public Function ToNchr(ByRef data As String) As String
  17. Dim sb As New StringBuilder
  18. Dim ascii As Integer = 0

  19. For Each c As Char In data.Replace("〔", "[").Replace("〕", "]").Replace("'", "'").ToCharArray()
  20. ascii = Convert.ToInt32(c)
  21. If ascii = 12288 Then
  22. sb.Append(Convert.ToChar(32))
  23. Else
  24. If ascii > 65280 And ascii <>Then
  25. sb.Append(Convert.ToChar(ascii - 65248))
  26. Else
  27. sb.Append(Convert.ToChar(ascii))
  28. End If
  29. End If
  30. Next

  31. Return sb.ToString
  32. End Function

2009年9月21日 星期一

Winform 設定輸入法

如何改變輸入法

//1.設定輸入法,依comboBox1選擇
InputLanguage MyInput = InputLanguage.InstalledInputLanguages[this.comboBox1.SelectedIndex];
//2.設定輸入法
InputLanguage.CurrentInputLanguage = MyInput;
完整程式碼
private void Form1_Load(object sender, EventArgs e)
{
//清除
this.listBox1.Items.Clear();
//1.收集系統已安裝的輸入法
InputLanguageCollection myInput = InputLanguage.InstalledInputLanguages;
//2.列舉輸入法
foreach (InputLanguage input in myInput)
{
//加入控制項
this.comboBox1.Items.Add(input.LayoutName);
}
//3.取得目前輸入法
InputLanguage CurrentInput = InputLanguage.CurrentInputLanguage;
listBox1.Items.Add("目前輸入法名稱為: " + "\t" + CurrentInput.LayoutName);
//4.取得輸入法區域
listBox1.Items.Add("目前輸入法文化特性為: " + "\t" + CurrentInput.Culture.DisplayName);
//5.取得預設的輸入法
InputLanguage DefaultInput = InputLanguage.DefaultInputLanguage;
listBox1.Items.Add("預設輸入法文化特性為: " + "\t" + DefaultInput.LayoutName);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//清除
this.listBox1.Items.Clear();
//1.設定輸入法,依comboBox1選擇
InputLanguage MyInput = InputLanguage.InstalledInputLanguages[this.comboBox1.SelectedIndex];
//2.設定輸入法
InputLanguage.CurrentInputLanguage = MyInput;
//3.取得目前輸入訊息
InputLanguage CurrentInput = InputLanguage.CurrentInputLanguage;
listBox1.Items.Add("目前輸入法名稱為: " + "\t" + CurrentInput.LayoutName);
//4.取得輸入法區域
listBox1.Items.Add("目前輸入法文化特性為: " + "\t" + CurrentInput.Culture.DisplayName);
//5.取得預設輸入法
InputLanguage DefaultInput = InputLanguage.DefaultInputLanguage;
listBox1.Items.Add("預設輸入法文化特性為: " + "\t" + DefaultInput.LayoutName);
}

2009年9月8日 星期二

C# Textbox Enable 時改變字體及背景顏色

//重寫OnEnabledChanged
protected override void OnEnabledChanged(EventArgs e)
{
if (Enabled==false){
SetStyle(ControlStyles.UserPaint,true);
}
else{
SetStyle(ControlStyles.UserPaint,false);
}
base.OnEnabledChanged(e);
}
//重寫OnPaint
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (Enabled==false){
pe.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
pe.ClipRectangle);
//文字描畫
int x=0,y=0;
Size s = pe.Graphics.MeasureString(Text,Font).ToSize();
x=Width-s.Width;
y=(Height-s.Height)/2;

pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black , x, y);
}

}

2009年9月7日 星期一

How to find objects in Generics with List.Find() method

How to find objects in Generics with List.Find() method

I've been looking for help on how to find objects in Generics with List.Find() method .... and ... take a look what I have found.
In the follow example, I created a simple class:

public class Person
{
private int _id;
private string _name;

public int ID { get{ return _id;} set{ _id = value;}}
public int Name { get{ return _name;} set{ _name= value;}}

public Person(int id, string name)
{
_id = id;
_name = name;
}
}

In the example, there's a simple class with two private attributes. Now we're going to create a typed List of this object and take advantage of the Find() method

public void CreateAndSearchList()
{
//create and fill the collection
List myList = new List();
myList.Add(new Person(1, "AndreySanches"));
myList.Add(new Person(2, "AlexandreTarifa"));
myList.Add(new Person(3, "EmersonFacunte"));

//find a specific object
Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });
}