简体   繁体   中英

Mouse click event on generated textboxes in c#

I am new to programming and c# so I would appreciate it if the answer to my question comes along with the resources you are using.

My code generates textboxes with each mousedown on the form. And I would like to close the current form and open another form when I (double)click on the generated texboxes.

Thanks in advance!

private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (rbtnText.Checked)
        {
            TextBox tb = new TextBox();
            tb.Location = new Point(e.X, e.Y);
            tb.Width = 75;
            this.Controls.Add(tb);
        }

If you want to subscribe to an event, you can do it inline when you create the control.

This will close the form when you double-click on it:

tb.MouseDoubleClick += (s, e) =>
  {
      Close();
      new YourForm().Show();
  }

Note: If Form1 (like in your example) is your main form (the first one your app loads), then as soon as you call Close() your entire application will close.

Attach an event handler to your Textbox's DoubleClick event

tb.DoubleClick += (s, e) =>
        {
            Form2 f2 = new Form2;
            f2.Show();
            this.Close();
        };

Note: Form2 is just for example.You should change it with your second Form's name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM