我希望有人可以帮助我对我的粘贴方法进行错误检查。我想防止将剪贴板中不是数值的任何内容粘贴到我的textBox中。下面列出了粘贴的编码。
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
// Determine if there is any text in the Clipboard to paste into the text box.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
// Determine if any text is selected in the text box.
if (textBox1.SelectionLength > 0)
{
// Ask user if they want to paste over currently selected text.
if (MessageBox.Show("Do you want to paste over current selection?", "Cut Example", MessageBoxButtons.YesNo) == DialogResult.No)
// Move selection to the point after the current selection and paste.
textBox1.SelectionStart = textBox1.SelectionStart + textBox1.SelectionLength;
} // end if
} // Paste current text in Clipboard into text box.
textBox1.Paste();
} // end pasteToolStripMenuItem
答案 0 :(得分:0)
如果您想加入Currency symbols
,decimal places
,Thousand seperators
等,Int.Parse
可能是您最好的选择
public bool IsValidNumber(string input)
{
int val = 0;
return int.TryParse(input.Trim(), NumberStyles.Any, null, out val);
}
"$100,000" = true
"100.002" = true
"1000,44j" = false
etc.