Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a code for erase command:
  public void MyMouseDown(Object sender, MouseEventArgs e)
        {
Graphics graphics = CreateGraphics();
int linh = System.Int32.Parse(textBox3.Text);

graphics.FillRectangle(new SolidBrush(Color.White), e.X, e.Y, linh, linh);

SelectRect.Width = 0;
SelectRect.Height = 0;
SelectRect.X = e.X;
SelectRect.Y = e.Y;

SelectRect.Width = linh;
SelectRect.Height = linh;
}


public void MyMouseMove(Object sender, MouseEventArgs e)
        {
Graphics graphics = CreateGraphics();
int linh = System.Int32.Parse(textBox3.Text);

graphics.FillRectangle(new SolidBrush(Color.White), e.X, e.Y,linh,linh);

SelectRect.Width = 0;
SelectRect.Height = 0;
SelectRect.X = e.X;
SelectRect.Y = e.Y;

SelectRect.Width = linh;
SelectRect.Height = linh;

Form thisform = (Form)sender;
ControlPaint.DrawReversibleFrame(thisform.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed);
}

Picture what this produces is here:

http://www63.zippyshare.com/v/17151299/file.html[^]

How to fix this, so the erase command would be just like in MS Paint? Only to show the rectangle for the eraser but not to draw it.
Posted

1 solution

You heavily misuse Graphics. In principle, in some cases it's possible to do what you are doing, but you cannot expect that such rendering will be shown permanently. You really should not create and instance of System.Windows.Graphics, you should handle System.Windows.Control.Paint event or override System.Windows.Control.OnPaint and the instance of Graphics passed in the event arguments.

I explained everything in my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

—SA
 
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