Windows Forms C++-CLI:在文本框输入中用逗号替换点
Windows Forms C++-CLI: Replace dot with comma in textbox input
我尝试通过自动更正用户对文本框的输入来解决非常微不足道的问题。我设法检测用户是否输入了数字(必要时)并控制其范围。现在我需要在用户将点键入文本框时自动用逗号替换点。我希望以下代码片段能够正常工作:
private: System::Void sample_ctrl_KeyDown(System::Object^ sender,
System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyCode == Keys::OemPeriod) {
int pos = timer_period_ctrl->SelectionStart;
sample_ctrl->Text = sample_ctrl->Text->Insert(pos,",");
sample_ctrl->SelectionStart = pos + 1;
e->Handled = true;
}
}
我期望的是捕获按下的键,用 Insert() 自己处理它并告诉听众我做了必须用
做的事情
e->Handled = true;
然而,结果如下:
// Before hitting '.' key
12
// After hitting '.' key
12,.
有没有办法忽略这个自然的'.'除了文本框?这是解决问题的最佳方法吗?
您需要使用 KeyPress
事件而不是 KeyDown
事件。
此事件使用 KeyPressEventArgs
而不是
if (e->KeyCode == Keys::OemPeriod) {
你需要使用
if (e->KeyChar == '.') {
我尝试通过自动更正用户对文本框的输入来解决非常微不足道的问题。我设法检测用户是否输入了数字(必要时)并控制其范围。现在我需要在用户将点键入文本框时自动用逗号替换点。我希望以下代码片段能够正常工作:
private: System::Void sample_ctrl_KeyDown(System::Object^ sender,
System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyCode == Keys::OemPeriod) {
int pos = timer_period_ctrl->SelectionStart;
sample_ctrl->Text = sample_ctrl->Text->Insert(pos,",");
sample_ctrl->SelectionStart = pos + 1;
e->Handled = true;
}
}
我期望的是捕获按下的键,用 Insert() 自己处理它并告诉听众我做了必须用
做的事情e->Handled = true;
然而,结果如下:
// Before hitting '.' key
12
// After hitting '.' key
12,.
有没有办法忽略这个自然的'.'除了文本框?这是解决问题的最佳方法吗?
您需要使用 KeyPress
事件而不是 KeyDown
事件。
此事件使用 KeyPressEventArgs
而不是
if (e->KeyCode == Keys::OemPeriod) {
你需要使用
if (e->KeyChar == '.') {