我使用的是Windows窗體控件庫中創建自定義的文本框,並有選擇的Numeric Text Box
,Alpa Numeric Text Box
,Decimal
等等自定義Windows控件庫在C#
我已經覆蓋了PreProcessMessage(ref Message msg)
方法做這個。
這裏是我的示例代碼段用於Numeric Text Box
public override bool PreProcessMessage(ref Message msg)
{
int WM_KEYDOWN = 0x0100;
if (msg.Msg == WM_KEYDOWN)
{
Keys keys = (Keys)msg.WParam.ToInt32();
bool bNumbers = false;
switch (eType)
{
case enTextBoxTypes.Numeric:
bNumbers = ((keys >= Keys.D0 && keys <= Keys.D9) || (keys >= Keys.NumPad0 && keys <= Keys.NumPad9)) && ModifierKeys != Keys.Shift;
System.Diagnostics.Debug.Print(bNumbers.ToString());
if ((keys == Keys.OemMinus || keys == Keys.Subtract) && bAllowMinusSign && ModifierKeys != Keys.Shift)
{
bIsOEMMinus = (keys == Keys.OemMinus) | (keys == Keys.Subtract);
if ((this.Text.Length != this.SelectedText.Length || !bAllowMinusSign) && this.SelectionStart != 0)
bIsOEMMinus = false;
}
break;
}
bool bDel = keys == Keys.Delete;
bool bBack = keys == Keys.Back;
bool arrows = (keys == Keys.Up) | (keys == Keys.Down) | (keys == Keys.Left) | (keys == Keys.Right);
bool Enter = (keys == Keys.Enter);
bool ESC = (keys == Keys.Escape);
bool TAB = (keys == Keys.Tab);
bool Home = (keys == Keys.Home);
bool End = (keys == Keys.End);
if (bNumbers bBack | bDel | arrows | Home | End | bIsOEMMinus)
return false;
else if(TAB)
return base.PreProcessMessage(ref msg);
else
return true;
}
else
return base.PreProcessMessage(ref msg);
}
現在我需要實現在TextBox 的unit
爲了這個,我已經添加在TextBox type
即Distance
一個新的枚舉。 所以,如果我進入了一個十進制數,之後,如果我按了k
鍵,然後在文本框中的值應該與Km
附加例如,如果文本框的文本是100
然後我按01然後將文本框的文本應該是100Km
。
而且我還需要將單位更改功能提供給Up Down
鍵。
所以,如果我在文本框中按down
鍵上面的值,那麼文本框的值應該是100000m
。
我該怎麼做?