augustus


I've put this code (just these two lines, nothing more) in the KeyDown event handler for a combobox. It works fine and keeps up with a user's normal typing speed. (So does KeyUp).

keysSoFar is a stringbuilder

keysSoFar.Append(e.KeyCode.ToString)

Debug.WriteLine(keysSoFar.ToString)

The equivalent code in the KeyPress event handler for the same combobox stalls, unable to keep up with the user's keystrokes.

keysSoFar.Append(e.KeyChar.ToString)

Debug.WriteLine(keysSoFar.ToString)

I have only one of the handlers compiled during a test so there's no combinatorial issue.

I want to use KeyPress so I don't have to deal with all the non-printing key codes.

Is the delay due to a difference between the KeyDown and KeyPress processing

Or is it due to the difference in the way StringBuilder handles KeyCode and KeyChar

All help appreciated,

Gus



Re: Windows Forms Data Controls and Databinding Can't keep up with data entry - is it KeyPress or Stringbuilder?

MarcD

Depending upon the rate at which keys are being pressed - chances are the Debug.WriteLine is actually what is slowing it down. Not sure if that helps but it is always useful to know that Debug.WriteLine is extremely slow/rated and thus using it in something that loops or repeats can have the appearance of causing undesired performance issues.



Re: Windows Forms Data Controls and Databinding Can't keep up with data entry - is it KeyPress or Stringbuilder?

augustus

Thanks Marc, point taken - but the problem remains without the debug.writeline statement. I put it there so I could see if the stringbuilder.append is working. I should have removed it from the post.

Gus





Re: Windows Forms Data Controls and Databinding Can't keep up with data entry - is it KeyPress or Stringbuilder?

MarcD

instead of appending the keycode or keychar simply append "a". See if the performance problem disappears. If it doesn't disappear then it's a problem with the event. You might want to see if there is a .Handled flag that you should set to true. If it does disappear then you may be seeing a penalty for looking up an enum.



Re: Windows Forms Data Controls and Databinding Can't keep up with data entry - is it KeyPress or Stringbuilder?

Ken Tucker

You can append a char to a stringbuilder you do not have to convert it to a string first.

keysSoFar.Append(e.KeyChar)







Re: Windows Forms Data Controls and Databinding Can't keep up with data entry - is it KeyPress or Stringbuilder?

augustus

Thanks Marc. Appending "a" instead of the keychar did not improve performance. Neither did setting e.Handled to true. I'm going to have to spend some time (which I don't have right now) to dig into this. Appreciate your help.

Gus





Re: Windows Forms Data Controls and Databinding Can't keep up with data entry - is it KeyPress or Stringbuilder?

augustus

Thanks Ken. Even without that overhead the performance issue remains. I'll return to this later when I've got time. Appreciate your help.

Gus