【问题标题】:C#: Form width used in class does not refresh when form resizedC#:在调整表单大小时,类中使用的表单宽度不会刷新
【发布时间】:2016-08-02 07:31:36
【问题描述】:

我正在做一些学校作业,我想知道你是否能解决这个问题。 我得到了我的 form.cs 代码和我的类 apple.cs(一些蛇果生成) 我选择我的苹果是在表单宽度/高度上随机生成的,但是当我调整该表单的大小时,它会一直在表单的旧“边界”内生成。

我可以重新加载它吗?

 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));
    }
}

非常感谢!

【问题讨论】:

    标签: c# forms class refresh


    【解决方案1】:

    或者你可以试试这个

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

    并以您定义对象的形式

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

    【讨论】:

    • 谢谢你是最棒的:)!
    【解决方案2】:

    让苹果使用

    从表单中获取宽度和高度
    double width= this.width;
    

    高也一样

    【讨论】:

    • 我没有这样做吗?你能解释一下吗:)?
    • 对不起,我不小心打了帖子我编辑了帖子,你可以从我看到的方法中得到它宽度和高度
    • 也许太多了,但你认为可以为我输入吗?:D 我不是高级程序员,这让我有点困惑 :) 抱歉
    • 此评论不是您要找的,请参阅其他评论,抱歉让您感到困惑
    • 对不起,我也不是那么先进,但我只是分享了我认为会很有帮助的想法
    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多