Note that there are some explanatory texts on larger screens.

plurals
  1. POShapes doesn't show on panel in winform
    primarykey
    data
    text
    <p>I am drawing a rectangle using <code>CreateGraphics</code> with <code>MouseDrag</code> and on <code>MouseUp</code>, I add a <code>RectangleShape</code> (<code>Microsoft.VisualBasic.PowerPacks</code>). But the shape is not visible. Actually shape is added to <code>ShapeContainer</code>, but not shown. AND when I click on <code>Panel</code>, it shows up.</p> <p>Below is my code to do so.</p> <pre><code> Rectangle rect; Point p; Size s; bool mouseDown; private ShapeContainer shapeContainer1 = new ShapeContainer(); public Form1 () { InitializeComponent(); // This will reduce flicker (Recommended) this.DoubleBuffered = true; this.shapeContainer1.Parent = this.panel1; this.shapeContainer1.Enabled = false; } private void panel1_MouseDown (object sender, MouseEventArgs e) { this.panel1.SendToBack(); s.Height = 0; s.Width = 0; p = this.panel1.PointToClient(System.Windows.Forms.Cursor.Position); rect = new Rectangle(p, s); this.panel1.Invalidate(); mouseDown = true; } private void panel1_MouseMove (object sender, MouseEventArgs e) { if (mouseDown) { this.panel1.SendToBack(); p = this.panel1.PointToClient(System.Windows.Forms.Cursor.Position); rect = new Rectangle(rect.Left, rect.Top, p.X - rect.Left, p.Y - rect.Top); this.panel1.Invalidate(); } } protected override void OnPaint (PaintEventArgs e) { Graphics g = this.panel1.CreateGraphics(); if (mouseDown) { using (Pen pen = new Pen(Color.Red, 2)) { this.panel1.SendToBack(); g.DrawRectangle(pen, rect); } } } private void panel1_MouseUp (object sender, MouseEventArgs e) { this.panel1.SendToBack(); shapeContainer1.Size = this.panel1.Size; shapeContainer1.Location = this.panel1.Location; RectangleShape rectangle = new RectangleShape(); rectangle.Location = rect.Location; rectangle.Size = rect.Size; rectangle.Name = "rectShape"; rectangle.Parent = this.shapeContainer1; rectangle.Visible = true; this.shapeContainer1.Shapes.Add(rectangle); } </code></pre> <p>What is wrong with the code.? Following are some photos that shows the exact problem.</p> <p><img src="https://i.stack.imgur.com/yTbpq.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/JaQla.png" alt="enter image description here"></p> <p>First photo shows that the rectangle is added to panel. When mouse moved, it disappears. Again when you click on the panel, is appears.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload