0

我希望有人可以帮助我检查粘贴方法的错误。我想防止将任何不是剪贴板中的数值的内容粘贴到我的文本框中。下面列出了粘贴的编码。

       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
4

1 回答 1

0

如果你想包含Currency symbols,decimal placesThousand 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.
于 2013-01-28T02:30:10.613 回答