Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a small problem which I can't fix. I hope someone out there can teach me how. Here is the problem:

I have a hardware a keyboard and writing a test application for it. To test if the buttons are pressed. When a button pressed I am drawing a rectangle on it. but the problem is when i release the button rectangle disappears. I want to stay there with all the pressed buttons. like when i hit button 1 and button 2 I want them both display rectangles and keep them on the form.

Here is my code:
private void ImageBox_Paint_1(object sender, PaintEventArgs e)
        {
            if (buttcorrs != null && buttcorrs.Count() == 4)   
            {
                e.Graphics.FillRectangle(Brushes.Green, buttcorrs[0], buttcorrs[1], buttcorrs[3] - buttcorrs[1], buttcorrs[2] - buttcorrs[0]);
                ImageBox.Invalidate(); 
                //Array.Clear(buttcorrs, 0, buttcorrs.Length);
            }

buttcorrs is an array where i send the coordinates as a unicode from the device. as I said when i hit another key it deletes the rectangle and displays a new one on the new button.
Posted

1 solution

That's the way that overridding the ImageBox_Paint method is supposed to be.

The graphics are not drawn in memory, just on the screen. When Paint is called, it clears the graphics and starts over.

If you want to draw all of the rectangles, you'll have to store them all in a List or some other collection and then in the Paint, draw them all each time.
 
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