views:

385

answers:

4

I have a datetimepicker in C#. When I click on it, it expands to show a monthly calendar, when I click the left arrow to go back a month, it changes the value and calls my event. The event includes too much code to include here but it calls several functions needless to say.
The problem I'm having is that when I click that left arrow it gets stuck in some sort of loop and keeps descending through the months and I can't stop it. One of the functions that is being called contains a Application.DoEvents() and if I comment that out it doesn't get stuck in the loop, but I need that command to update another section of the interface. Any idea why this is happening?

I can duplicate it sometimes with this code, sometimes it just does it a couple times, sometimes it gets stuck in the loop.

private void DateTimePickerValueChangedEvent(object sender, EventArgs e) 
{ 
afunction(); 
} 

private void afunction() 
{ 
listView1.Clear(); 
panel1.Visible = true; 
Application.DoEvents(); 
}
A: 

How can we help you without seeing that code ?

JonH
Not an answer... more of a comment.
Austin Salonen
I can duplicate it sometimes with this code, sometimes it just does it a couple times, sometimes it gets stuck in the loop.private void DateTimePickerValueChangedEvent(object sender, EventArgs e) { afunction(); } private void afunction() { listView1.Clear(); panel1.Visible = true; Application.DoEvents(); }
Scott Chantry
+1  A: 

Without seeing any of the code, try these steps:

  1. Comment out the entire event handler to see how fast it runs with nothing attached to it.
  2. Uncomment lines one at a time to see which ones are causing the most problems.
  3. Analyze those method calls.
  4. ...
  5. Profit!
Austin Salonen
A: 

You could try a couple of things. Get rid of the DoEvents inside of the ChangedEvent. Call the doevents inside of a seperate function after maybe a period of time (thread.sleep() ?).

I know doevents does cause issues but I rarely use it.

JonH
The problem is that I make a panel visible at the beginning of the event. Do a web service call and then hide the panel. I have to put the DoEvents after the panel1.visible = true; to actually get it to display while the webservice is called.
Scott Chantry
+3  A: 

I also have the same problem. In my case, instead of calling DoEvents I'm updating a Crystal Report view. The only workaround I found is to update my view upon the CloseUp event instead of ValueChanged or TextChanged.

Scott, how did you finally corrected your problem ?

rold2007
I finally decided to just remove the panel I was trying to display, it was more for cosmetic reasons than functionality but I'll give your solution a try. Thanks.
Scott Chantry
+1 - The `CloseUp` event solved my problem in something I am working on in a VB.NET project. Thanks for your answer!
Buggabill