ClickEvent Issue

T

Trollpower

Dear NG,

ive got two Forms, lets say A and B. Form A shows Form B. I put some
buttons on both forms. Addionally i put them at the same location. If
I click a Button on form B twice the second click gets passed to form
A. This happens as followed: After clicking the button on form B the
form gets closed after some function gets called. In the meantime the
second click gets passed to the button on form B. But due to some
mishappenings the second click wont be ignored, it gets passed to the
button with the same location on form A.

Does anyone knows how to solve this problem, so that i only get the
click once and only on the button i made the click on? Please note: I
dont want to diasable all the controls on Form A to solve the problem.

Thanks in advance

Jens

Here is the sample code:

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace Formstest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
InitializeComponent();


}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region blah
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 16);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(0, 200);
this.button2.Size = new System.Drawing.Size(240, 72);
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Hello World");
}

private void button1_Click(object sender, System.EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
}


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace Formstest
{
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;

public Form2()
{
InitializeComponent();

}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

#region blah
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 208);
this.button1.Size = new System.Drawing.Size(240, 64);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.Controls.Add(this.button1);
this.Text = "Form2";

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
Thread.Sleep(2000);
this.Close();
}
}
}
 
A

Alex Feinman [MVP]

In your Click event handler on the form B try calling Application.DoEvents()
 
J

Jens Meyer

Dear Alex,

thank you very much for your help, but your tip didnt work. Do you have
any other ideas?

Greetings

Jens
 
J

Jordan Irwin via DotNetMonster.com

This is probably not the fix you were looking for.. but,

I've had this problem before. Luckily, my second form (the one accepting the click event you do not want) was simple. I worked around this problem by disabling the button by default, and enabling it at the end of my FormLoad event.

This worked well for me since my secondary form had few controls to handle in this way. But it's just a workaround, not a true fix.

Perhaps this will work until a better answer is provided.

*****************************************
* A copy of the whole thread can be found at:
* http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-compact-framework/9027
*
* Report spam or abuse by clicking the following URL:
* http://www.dotnetmonster.com/Uwe/Abuse.aspx?aid=20148103ec2b4ec587b20d13b0d727a5
*****************************************
 
J

Jordan Irwin via DotNetMonster.com

(Relating to Alex's response)

The Application.DoEvents() should probably be called before the .Close() event of the form. I've experienced some different results with placing code after the form has been closed. Try making it the last method called before you close the form.

*****************************************
* A copy of the whole thread can be found at:
* http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-compact-framework/9027
*
* Report spam or abuse by clicking the following URL:
* http://www.dotnetmonster.com/Uwe/Abuse.aspx?aid=8301c29ef4964baabb227e4a49faa9d5
*****************************************
 
J

Jens Meyer

Thanks to all who tried to help me out,

i tried to use the Application.DoEvents()-method again. I experienced
some success with it. The problem seems also to be if i open up a new
form, where a Button is placed at the same location than the Button
which makes the form to appear. So i dont only have to put a
Application.DoEvents() at before the .Close() call but also before the
.Show call.

Thanks to all again :)

Greetings

Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top