Windows Develop Bookmark and Share   
 index > Windows Forms General > user control fires a event during load when not invoked
 

user control fires a event during load when not invoked

Hi,

I work in a winforms application.
I have a main application in a solution and a user control in another solution.
The main application accesses the user solution.

The user control has 2 dropdowns which are pickup time and dropoff time.
I inserted code to check for a condition dropdown>pickup time in the selectindex changed event of the dropff dropdown.
This will pop a msgbox when the user selects wrong time.

Now when i run the application, the messagebox pops up when the user control first loads even though it is in a different event.

Can anyone point to me why this is coming up as soon as the user control loads instead of when value changed?

Thanks in Advance

rowter  Thursday, September 10, 2009 2:55 AM
Which field is being set first ? Imagine this scenario;

On form load, both controls have 'today's value... presumably with a midnight time.

The 'pickup' control value is set to be todays dateat the current time (before the drop off date/time is set)... which is now later than today at midnight, so when the event fires the message box displays.

However, this would only happen if you had the same code in event handlers for both controls, since the drop off index change event shouldn't fire when the pick up date is set.

Is that the case ?

Also, check that the 'selectedvalue' of both controls in the event includes a time... if for some reason the time has been stripped of the date when the comparison is performed then any two times on the same day will cause the message box to be shown.

I also note you are setting focus back to the control when this happens... this is perhaps not the best way to do this. You should consider altering your code to use the Validating event, in that event if you decide the value is bad you can set e.Cancel = true and focus will remain in the field. The Validating event was specifically designed for this purpose, although it doesn't fire until the user attempts to leave the field, so I don't know whether that will work for you or not.

Yort  Thursday, September 10, 2009 9:31 PM
The event is probably being "hooked up" in the InitializeComponent method of the form, which is usually the first method executed in the form's constructor.
This scenario usually occurs because you used the Form Designer to hook the event, instead of doing it in code.
During your portionof theform initialization, you set a value for the control, which in turn fires the event.

Change the order in which things occur.
You need to initialize the control, then hook up the event in code.
Or...you can unsubscribe to the event, modify/initialize the control, then re-subscribe to it.

Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Thursday, September 10, 2009 9:48 PM
Is it possible the value is being changed during start-up ?

Do you have code that sets a default value, or is there code in InitialiseComponent() of either the user control, or the form hosting the control instane, that would cause value to be set on one of the controls ?

Those events can fire (depending on the specific event) regardless of whether the value is changed via the keyboard, mouse, or code. If the value is being changed by code somewhere, the event will still fire the message box will still show up.
Yort  Thursday, September 10, 2009 3:08 AM

Hi yort,


Following is the code that i wrote in the selected index change operation. Messaetgbox should pop up only when dropoff value <= pickup value.

On form laod, the value of pickup is changing to current time(whatever the time is on the PC) and drop off time is automatically set to (pickuptime + 2 hrs )
when the time is greater, why is the messagebox coming up here on form load?

Public Sub OndropoffIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboDropOff.SelectedIndexChanged


If cmboDropOff.SelectedValue <= cmboPickup.SelectedValue Then

MessageBox.Show("Dropoff time should be greater than Pickup time", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
Call cmboDropOffFocus()


End If

end sub

Thanks,

rowter  Thursday, September 10, 2009 8:27 PM
Which field is being set first ? Imagine this scenario;

On form load, both controls have 'today's value... presumably with a midnight time.

The 'pickup' control value is set to be todays dateat the current time (before the drop off date/time is set)... which is now later than today at midnight, so when the event fires the message box displays.

However, this would only happen if you had the same code in event handlers for both controls, since the drop off index change event shouldn't fire when the pick up date is set.

Is that the case ?

Also, check that the 'selectedvalue' of both controls in the event includes a time... if for some reason the time has been stripped of the date when the comparison is performed then any two times on the same day will cause the message box to be shown.

I also note you are setting focus back to the control when this happens... this is perhaps not the best way to do this. You should consider altering your code to use the Validating event, in that event if you decide the value is bad you can set e.Cancel = true and focus will remain in the field. The Validating event was specifically designed for this purpose, although it doesn't fire until the user attempts to leave the field, so I don't know whether that will work for you or not.

Yort  Thursday, September 10, 2009 9:31 PM
The event is probably being "hooked up" in the InitializeComponent method of the form, which is usually the first method executed in the form's constructor.
This scenario usually occurs because you used the Form Designer to hook the event, instead of doing it in code.
During your portionof theform initialization, you set a value for the control, which in turn fires the event.

Change the order in which things occur.
You need to initialize the control, then hook up the event in code.
Or...you can unsubscribe to the event, modify/initialize the control, then re-subscribe to it.

Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Thursday, September 10, 2009 9:48 PM
FYI... That is effectively what I said in both of my earlier posts.
Yort  Thursday, September 10, 2009 9:49 PM
Yup. I just connected the dots for the OP.
I don't think they understood the implication of what you wrote.

Rudy
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Thursday, September 10, 2009 9:59 PM
Thanks Yort and Rudedog2,

I narrowed it to a line in the code which is causing it to fire.

rowter  Monday, September 14, 2009 3:26 PM

You can use google to search for other answers

Custom Search

More Threads

• Button text alignment Bottom
• Invoke Procedures
• Proportionally resizing listview columns
• ContextMenuStrip positioning after availabile state changes during the Opening event.
• data to table
• KeyUp and KeyDown problem
• How to achieve my contextMenuStrip look like 3d
• Is there a way to get print preview information from web control?
• Draw pie or bar graphs???
• VC# Beta 2005: Retrieving values out of DataGridView?