Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ok. My problem is that when i am in the form and press F5 it works like a charm and all my data and controls are updated. Second time i press F5 it fires the frmarbeidsplan_keydown twice (?), and the third time it fires the event 4 times, and then 8 times and 16 times.

Every time i press F5 it doubles the amount of times i have pressed it and loops the code. I cannot understand why?



C#
private void frmArbeidsplan_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.F5)
     {
         MessageBox.Show("F5");
         e.SuppressKeyPress = true;
         e.Handled = true;
         OppdaterSkjema();
     }

 }




C#
//Refresh skjema med data
        private void OppdaterSkjema()
        {
            if (this.lOG_ArbeidsplanDataGridView.IsCurrentCellInEditMode)
            {
                this.Validate();
                this.lOG_ArbeidsplanDataGridView.EndEdit();
                this.lOG_ArbeidsplanBindingSource.EndEdit();
                this.lOG_ArbeidsplanTableAdapter.Update(this.dataSetApoLogiDose);
            }
            OppdaterStatus(0, 1, 5, "Fjerner objekter fra skjema");

            Form.ControlCollection fObjs = (ControlCollection)this.Controls;
            int i = fObjs.Count;

            while (i != 0)
            {
                fObjs[0].Dispose();
                i = i - 1;
            }


            this.InitializeComponent();
            this.frmArbeidsplan_Load(sndr, es);
            OppdaterStatus(-1, 4, 5, "Siste lasting:" + DateTime.Now.ToLongTimeString());


        }
Posted

On the keypress event, you are calling OppdaterSkjema() which then calls this: this.frmArbeidsplan_Load(sndr, es);.

In the form load method, it is hooking up the keypress event again. So when you press a key, it calls the event one more time than last time.

Get rid of the call to Load, or move everything but Initialize() out of the load method and into a different one that can be called more than once.
 
Share this answer
 
Comments
wizardzz 15-Jul-11 10:37am    
Good answer!
MrDeej 15-Jul-11 14:37pm    
I am not sure i understand. Is there something i should set if() around in the form_load event? I can bring an argument from the Keypress event to cancel the load.

Or is it something in the frmArbeidsplan.Designer.Cs which fires the keypress?

Thanks for the rapid response, this looks like a very good forum.
Tim Groven 15-Jul-11 16:03pm    
The biggest problem is calling this.frmArbeidsplan_Load(sndr, es); from an update method.

Set a boolean to true in the _Load method, and if it's true, don't call Initialize().
MrDeej 18-Jul-11 5:53am    
Okai. I will look into it. For now i have found a quickfix

this.KeyDown -= new System.Windows.Forms.KeyEventHandler(this.frmArbeidsplan_KeyDown);
Philippe Mori 15-Jul-11 20:39pm    
By the way you should never explicitly call an event handler like that nor should you call yourself InitializeComponent which will hookup the event handler again.

The event handler is called automatically by the framework at the appropriate time. If you need to reuse some of the code you put in that handler, then create a private function for that and call it from the handler and form other place you need that code.

When the designed is used to create a Form, InitializeComponent is called by the generated constructor. Normally, you should not call it yourself and neither modify the generated code.
If you felt difficulties by re arranging the InitializeComponentor construcors, just follow the method.

In simple, Just add
C#
void onKeyPressEvent(object sender, KeyEventArgs e)
{
if(e.Handled)
return;

{
//
// the block of codes to be executed on the key press
// should added here.
//
}
e.Handled = true;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900