简体   繁体   中英

Draw a rectangle in a panel of a form when a button click event is fired from another form

I'm currently working on windows form application where I have two forms; form1 and form2. There is a button inside form1 which opens form2 when clicked and what I want is to create a rectangle inside a panel of form1 when a button inside Form2 is clicked. I put some code to create rectangle inside the button click event of form2, but it showed nothing when clicked. However, whenever I put the draw.rectangle method inside the same form where button is clicked, it work, but differently it doesn't

Here's the code inside form1

  private void btnSave_Click(object sender, EventArgs e)
    {
        Layoutsetting a = new Layoutsetting();
        a.ShowDialog();
    }
public void DrawObject()
    {

            Graphics g = panel1.CreateGraphics();
            Rectangle rect = new Rectangle(10, 10, 80, 90);
            rect.Inflate(-10, -10);
            g.DrawRectangle(black, rect);
            g.FillRectangle(Brushes.BlueViolet, rect);
            StringFormat f = new StringFormat();
            f.LineAlignment = StringAlignment.Center;
            f.Alignment = StringAlignment.Center;
            g.DrawString("Hello", this.Font, Brushes.GhostWhite, rect, f);
            panel1.Refresh();
 }

This is the code inside form2

  private void btnConfirm_Click(object sender, EventArgs e)
    {
        Form1.Default.DrawObject();
        this.Close();
    }

You have to add a method to Paint:

panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.draw);

private void draw(object sender, PaintEventArgs e)
{
    if(buttonClicked) {
        Graphics g = e.Graphics;
        //...
    }
}

The problem is not with drawing rectangle, the panel paint event fires whenever even a slightest part of the panel were hidden (for example a part of it was behind another form) and redraws the panel, so the rectangle disappears (but when it is the active form which paint event doesn't fire, the rectangle will be drawn and will not be cleared unless you do something that panel needs to be redrawn.).

Easy Solution:

Create an image of recangle and use it as background image when needed, instead of drawing it.

Another Solution:

add a property to your form (or your panel):

public bool NeedsToBeDrawn {get; set;}

instead of this line of code:

Form1.Default.DrawObject();

just set the property to true:

   Form1.NeedsToBeDrawn  = true;

and move your code to your panel's paint event:

private void panel1_Paint(object sender, PaintEventArgs e)
{
     if(NeedsToBeDrawn)
     {
            Rectangle rect = new Rectangle(10, 10, 80, 90);
            rect.Inflate(-10, -10);
            e.Graphics.DrawRectangle(black, rect);
            e.Graphics.FillRectangle(Brushes.BlueViolet, rect);
            StringFormat f = new StringFormat();
            f.LineAlignment = StringAlignment.Center;
            f.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Hello", this.Font, Brushes.GhostWhite, rect, f);
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM