|
Hi
I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled?
here is the code snippet:
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.button1.Enabled = false;
System.Threading.Thread.Sleep(3000);
i++;
this.label1.Text = "Total Calls: " + i.ToString();
this.button1.Enabled = true;
}
}
when the button is disabled and you click on it, you can see the count gets increased.
Please help
|
|
|
|
|
You can unbind the click event at the
time of disabling the button.
Try following code..
button1.Click -= new System.EventHandler(this.button1_Click);
And you can rebind the click even on making it enable.
button1.Click += new System.EventHandler(this.button1_Click);
Hope this will help!
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Tried but no success still the same behavior
The event handler now looks like this:
private void button1_Click(object sender, EventArgs e)
{
this.button1.Enabled = false;
this.button1.Click -= new System.EventHandler(this.button1_Click);
System.Threading.Thread.Sleep(3000);
i++;
this.label1.Text = "Total Calls: " + i.ToString();
this.button1.Enabled = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
}
still count is increasing if I click disabled button
|
|
|
|
|
Add an if ( this.button1.Enabled ) ?
|
|
|
|
|
Tried that also. The problem is that the event handler is getting called after first execution and at the end of it I am setting this.button1.Enabled = true;
so this is not going to solve my problem..
|
|
|
|
|
Are you only clicking the button once?
Have you attached the handler more than once?
|
|
|
|
|
I have not attached the handler more than once.
After first click the button is disabled for 3 seconds when I click on the disabled button somewhere it is queuing that event and after execution of first event handler it is calling that event handler again that I don't want as I clicked on disabled button.
I don't know how to attach the file here. PM provide me your email id I'll send you the test application I created to reproduce the problem.
|
|
|
|
|
Hi,
it does not work like that. Those Enabled changes will take effect only after your handler finishes, and putting a Thread.Sleep in a handler is a big no no. A handler should never last more than some 10 milliseconds, your app's GUI should remain responsive at all times. What you need is either another thread (assuming the Sleep is emulating some real action), or a timer (assuming you really just want a delay).
For timers, your best bet would be a System.Windows.Forms.Timer; you disable the button and launch the timer inside the button click handler, then re-enable the button and stop the timer inside the timer's tick handler. Works like a charm, without blocking anything but the button itself.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: Those Enabled changes will take effect only after your handler finishes
Does this only apply to the Enabled property of controls, or others as well, and why wait until the handler completes? I've been seeing a different behavior for something like the ComobBox.SelectedIndex property. I have an app that, when you click a button, sets some ComboBox selections, does some processing, and maybe changes the ComboBox selection again (to indicate to the user that something is actually going on). This is all happenning in a function called by my button handler on the GUI thread, and I see the ComboBox values update while it runs.
Yes, I know the fact that such a long procedure occurs in the click handler isn't the best solution (it was fine with the original program design, but we're in the process of moving this all out to a separate thread).
Thanks,
Dybs
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
dybs wrote: Does this only apply to the Enabled property of controls
I don't know; I don;t think it is documented, and I don't need to know as I keep my handlers short.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
How about using a flag in order to suppres the event?
private bool suppresEvent = false;
private void button1_Click(object sender, EventArgs e)
{
if(suppresEvent)
return;
suppresEvent = true;
System.Threading.Thread.Sleep(3000);
suppresEvent = false;
}
I have no smart signature yet...
|
|
|
|
|
Please, i am working on my BSc Degree project. I need a code snippet that will query the server/connection to my ISP provider to determine if my bandwidth subscription is exactly what i requested. for example, if i subscribed to 100MB, i want to be sure its 100MB before i start consuming the bandwidth at all. else if the bandwidth is less than 100MB, then i can complain to them to make necessary adjustment.
I need your advice or help on this. thanks.
modified on Wednesday, May 12, 2010 5:37 AM
|
|
|
|
|
Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email (like this one) to let you know.
Edit it, and remove your address.
I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in.
Apathy Error: Don't bother striking any key.
|
|
|
|
|
thank you sir, point taken and learnt
|
|
|
|
|
You cannot determine that on your own. You need a server on the other end that will be able to send and receive with a bandwidth superior to your own, and you can then test against that server, but the bandwidth will always be the bandwidth of the weakest link.
|
|
|
|
|
You need some files of known size. Start the upload of the smallest of them, determine the time required for uploading, and then calculate (please note: bandwidth is typically in bit per second, while file size is in bytes, i.e. that "100MB" connection should translate into 12.5 MegaBytes per second). If that did not take far too long, take the next bigger file and so on - the result has the smallest error for the biggest file.
Of course, repeat that for download.
And, as Michel Godfroid pointed out, be certain that on the other side the connection is better than on your side.
|
|
|
|
|
They also use a different kind of "mega".. as in 1000000 instead of 1048576
|
|
|
|
|
Hi guys,
I develop a csharp smart device.I want know now fill my combobox with data extractedfrom my database .mdf(wishing that the TCP/IP goes well and the connection to my local server works well ).
My code is:
string sConnection = "Provider=SQLNCLI.1;Persist Security Info=False;User ID=sa;Initial Catalog=GMAO;Data Source=127.0.0.1,1433;Password=sa;";
string sSQL = "SELECT com FROM energie; ";
SqlConnection conn = new SqlConnection(sConnection);
SqlCommand comm = new SqlCommand(sSQL, conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sSQL,conn);
SqlDataReader dr = null;
try
{
comm.Connection.Open();
da.Fill(ds, "SQL Temp Table");
foreach (DataRow d in ds.Tables[0].Rows)
{
MessageBox.Show("xxxx");
comboBox1.Items.Add(d.ItemArray[0].ToString());
}
}
catch (SqlException ex)
{
MessageBox.Show("connexion impossible");
MessageBox.Show(ex.Message);
return;
}
dr.Close();
comm.Connection.Close();
Still not filled after running .
what is the problem???
Thanks
|
|
|
|
|
I suggest you run it under the debugger and see if a) there's a table in the dataset, and b) if there are rows in the table.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
I really don't know to run it under the debugger???<
Thanks
|
|
|
|
|
Put a breakpoint in the code at the point you want to break it. There's a shortcut to do this: press F9. Ensure you've built the code as a Debug build and press F5 to run it in debug mode. When your code hits the breakpoint, you can step over code using F10. To step into a function, press F11. To inspect the value of a variable, you can use the locals window, or the quick watch, you can inspect it with the immediate window or you can hover over the input in the running code and see the values in the visualizer.
It's that easy, and you really need to learn how to debug your code - it's a vital skill.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
It's almost impossible to help you until you learn how to use Visual Studio.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
It is not a problem if u don't like me to help me.There are always some starters in any field u know.The experimental ones try to help them.So,I am a starter .If u try to help me,thank u a lot of u collaborativ caracter.Else,it is not time nor place to give me tips
Thank u a lot
|
|
|
|
|
Hi "Starter",
After reading your answer, I think I should give you a tip.
You seem to be a beginner in programming - otherwise you would know how to debug your code (with or without a debugger...)
Many members on codeproject are real experts, ask them if you have a real problem.
But before that, learn how to use your tools and language. No one likes the kind of "beginner" unwilling to learn and asking "questions".
Developing for a smart device is even more challenging than for a PC platform - (I do it a lot)
So if you still have the problem with your combobox and the problem is unspottable during debug - contact me - i will try to help you
|
|
|
|
|
I suggest you read and study this article[^], even if you are using a different version of Visual Studio.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|