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);
            }

    }
2

2 Answers 2

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);

}
Sign up to request clarification or add additional context in comments.

6 Comments

Why would that be a problem?
@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 edit your post with more details about the error you are getting. Which line of code is causing it?
@JeremyThompson his MyTextBox class actually has an Pasted event - see vcskicks.com/clipboard-textbox.php
@Jeremy Thompson not working, I need it to detect the mouse paste not keyboard keys :p i tried it anyway but nah not working
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.