0

I made a post about detecting the paste event in a textbox and was directed to some place with code that does this.. i got it working, but it required that I create my own textbox control from the Program.cs Main event. here is the code:

    var txtNum = new MyTextBox();
    txtNum.Pasted += (sender, args) => MessageBox.Show("Pasted: " + args.ClipboardText);
    txtNum.Size = new System.Drawing.Size(578, 20);
    txtNum.Location = new System.Drawing.Point(12, 30);
    var form = new Form1();
    form.Controls.Add(txtNum);
    Application.Run(form);

now the new problem is that when i try toprocess anything in txtNum i receive "Object reference not set to instance of an object" how can I resolve this? it's a winforms application .net 4.0

the error is here:

    private void button1_Click(object sender, EventArgs e)
    {
        string s = txtNum.Text; //OBJECT REFERENCE ERROR

            string[] numbers = s.Split(' ');
            double sum = 0;
            for (int i = 0; i < numbers.Length; i++)
            {
                double num = double.Parse(numbers[i]);
                sum += num;
            }
            lblRESULT.Text = sum.ToString();
            if (cp == true)
            {
                Clipboard.SetText(lblRESULT.Text);
            }

    }
ace007
  • 577
  • 1
  • 10
  • 20

2 Answers2

2

Its because you have declare the textbox in the scope of the Main() .

static TextBox txtNum = new TextBox();
[STAThread]
static void Main()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
// txtNum.Paste += (sender, args) => MessageBox.Show("Pasted: " + args.ClipboardText);
txtNum.Size = new System.Drawing.Size(578, 20);
txtNum.Location = new System.Drawing.Point(12, 30);
Form1 form = new Form1();
form.Controls.Add(txtNum);
Application.Run(form);
}

A better approach would be to add the textbox in Form1s constructor or Form_Load events.

TextBox txtNum = new TextBox();
public Form1()
{
InitializeComponent();
txtNum.Size = new System.Drawing.Size(578, 20);
txtNum.Location = new System.Drawing.Point(12, 30);
txtNum.PreviewKeyDown += (sender, e) =>
{
    if (e.KeyValue == 17 && e.Control == true)
    {
        MessageBox.Show("you pasted:" + Clipboard.GetText());
    }
};
this.Controls.Add(txtNum);

}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Why would that be a problem? – Blorgbeard Nov 16 '12 at 00:24
  • @JeremyThompson the first snipped was full of errors about object references and ..ect, the second snipped is not optional because if you see the snipped I posted it requires the "sender" argument, and when i try to use it in the form_load event it conflicts with the form load event sender. – ace007 Nov 16 '12 at 00:39
  • @ace007 edit your post with more details about the error you are getting. Which line of code is causing it? – Blorgbeard Nov 16 '12 at 00:40
  • 1
    @JeremyThompson his `MyTextBox` class actually has an `Pasted` event - see http://www.vcskicks.com/clipboard-textbox.php – Blorgbeard Nov 16 '12 at 00:49
  • @Jeremy Thompson not working, I need it to detect the mouse paste not keyboard keys :p i tried it anyway but nah not working – ace007 Nov 16 '12 at 01:34
  • ALL along, all i should have done is just simply made a context menu strip and make my own paste function for the textbox jeez that would have saved allot of time! – ace007 Nov 16 '12 at 02:10
0

OK, that code that declares the textbox in Main is just an example. You should declare the textbox in the form code, as per Jeremy's answer.

Alternatively you should be able to find your MyTextBox control in the toolbox - just drag it onto the form like any control, and add the Pasted event handler code like normal.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272