C# 中的自定义 Windows 控件库
我使用 Windows 窗体控件库创建了一个自定义文本框,其中包含数字文本框、Alpa 数字文本框、十进制选项等等...
我已经重写了 PreProcessMessage(ref Message msg) 方法来执行此操作。
这是我的数字文本框
的示例片段
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);
}
现在我需要在文本框中实现unit
为此,我在 TextBox 类型
中添加了一个新的枚举,即 Distance
。 因此,如果我输入一个十进制数字,然后按 k
键,则文本框值应附加 Km
例如,如果文本框的文本为 < code>100 然后我按 k
那么文本框的文本应该是 100Km
。
我还需要为 Up Down
键提供单位更改功能。
因此,如果我在具有上述值的文本框中按向下
键,则文本框值应为100000m
。
我该怎么做?
I've created a customized Text Box using Windows Forms Control Library, and which have options for Numeric Text Box
, Alpa Numeric Text Box
, Decimal
etc...
I've overridden the PreProcessMessage(ref Message msg)
Method to do this.
Here is my sample snippet for 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);
}
Now I need to implement the unit
in the Textbox
for this I've added a new enumeration in the TextBox type
i.e. Distance
.
So if I entered a decimal number, and after that if I pressed the k
key then the text box value should be appended with Km
for example if the text box's text is 100
and then I pressed k
then the text box's text should be 100Km
.
And also I need to provide the unit change functionality to the Up Down
keys.
So if i press the down
key in the text box with the above value , then the text box value should be 100000m
.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这是您的问题,也许您可以使用屏蔽文本框。因为我看到您已经使用了 KeyDown,并且我认为您知道如何处理。
演练:使用 MaskedTextBox 控件
蒙版编辑示例
Perhaps you can use a masked Textbox if this is your question. Because I see you already have the KeyDown in use and I think you know how to handle.
Walkthrough: Working with the MaskedTextBox Control
Mask edit sample
也许您可以继承文本框控件并更改 OnTextChanged 和 onkeydown 事件。查看此链接,他使用这两个事件。 (不过不要看名字)
如果你认为这对你没有任何帮助,请解释一下是什么让你如此。
Maybe you could just inherit the textbox control and alter the OnTextChanged and onkeydown events. Look at this link, he uses both the events. (Don't look at the name though)
If you don't think this will help you for whatever reason, please explain me what holds you.