简体   繁体   中英

c# multiply opening windowsform

I have a problem. I want to open windows form multiply times with using only one button one time. I draw something in different places on my form and form is opened when i click the button. I would like to when I click the button the form will open and close and open again but with different picture(because I choose points randomly) Could you help me? I cant fix it alone :( Here is my code to draw something

    public void Form1_Paint(object sender, PaintEventArgs e)
    {
        int x1, y1, x2, y2;
        Random losowa1 = new Random();
        x1 = losowa1.Next(0, 200);
        Random losowa2 = new Random();
        y1 = losowa2.Next(0, 480);
        Random losowa3 = new Random();
        x2 = losowa1.Next(300, 500);
        Random losowa4 = new Random();
        y2 = losowa2.Next(0, 480);
        e.Graphics.FillRectangle(Brushes.Black, x1, y1, 100, 100);
        e.Graphics.FillEllipse(Brushes.Black, x2, y2, 100, 100);
     }

And here I open the form

   private void button1_Click(object sender, EventArgs e)
       {
            {
                Form1 OknoStart = new Form1();
                OknoStart.ShowDialog();
            }
      }

Hope you will help me or will show me proper way... Thanks!

try this

form1:

 private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int x1, y1, x2, y2;
            Random losowa1 = new Random();
            x1 = losowa1.Next(0, 200);
            Random losowa2 = new Random();
            y1 = losowa2.Next(0, 480);
            Random losowa3 = new Random();
            x2 = losowa1.Next(300, 500);
            Random losowa4 = new Random();
            y2 = losowa2.Next(0, 480);
            e.Graphics.FillRectangle(Brushes.Black, x1, y1, 100, 100);
            e.Graphics.FillEllipse(Brushes.Black, x2, y2, 100, 100);

            System.Threading.Thread.Sleep(1000);
            this.Close();
        }

button1:

private void button1_Click(object sender, EventArgs e)
        {
            for (int i=0;i<2;i++) {
                Form1 form1 = new Form1();
                form1.ShowDialog();

            }
        }

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