Muhammet
Muhammet

Reputation: 35

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

Upvotes: 2

Views: 2091

Answers (2)

Grant Winney
Grant Winney

Reputation: 66499

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.

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101711

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.

Upvotes: 1

Related Questions