简体   繁体   中英

C#: Form width used in class does not refresh when form resized

I am working on some school work and I am wondering if you could solve this. I got my form.cs code and my class apple.cs(some snake fruit generation) I choosed that my apple is being randomly spawned accros the form width/height, but when I resize that form, It keeps to spawn within old "borders" of the form.

Can I reload it somehow?

 class Apple    {
    Random nc = new Random();
    Form1 hl;
    //Properties
    public Color Color { get; set; }
    public Size Size { get; set; }
    public Point Pt { get; set; }
    //constructors
    public Apple()
    {
        hl = new Form1();
        int s = 10;
        this.Color = Color.Red;
        this.Size = new Size(s,s);
        this.Pt = new Point(nc.Next(s,hl.panel1.Width-(2*s-1)),nc.Next(s,hl.panel1.Height-(2*s-1)));
    }
    //methods
    public void drawOutPoint(Graphics kp)
    {
        kp.FillRectangle(new SolidBrush(Color), new Rectangle(Bod, Size));
    }
}

Thank you very much!

let the apple get the width and high from the form using

double width= this.width;

and the same for high

or you can try this

public Apple(double width,double height)
{
\\your code and assign the values 
}

and in the form you define the object

apple ap=new applw(this.width,this.height);

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