更新另一个类的值

时间:2018-08-22 02:58:07

标签: c# winforms

我有这段代码可以使用ShapeClass.cs绘制一个矩形。但是我想使用主窗体中的按钮事件来更新Rectangle参数。我怎么做? PaintRectangle类仅从主窗体获取文本框上的初始值。 ................................................... ................................................... ................................................... ................

主修班

    #region //Passing of values
    public string ShapeWidth() { return textBox_Shape_Width.Text;}
    public string ShapeHeight() { return textBox_Shape_Height.Text; }
    #endregion

    private void btn_Draw_Click(object sender, EventArgs e)
    {
        ShapeClass s = new ShapeClass();
        var value = s.diffVar[0];
        MessageBox.Show(value.ToString());
        pictureBox_Canvas.Paint += paintRect;
        pictureBox_Canvas.Refresh();
    }
    #region //Methods for Hide and Show
    public void HideButtons()
    {
        btn_Rectangle.Visible = false;
        btn_Square.Visible = false;
        btn_Ellipse.Visible = false;
        btn_Circle.Visible = false;
        btn_Triangle.Visible = false;
    }
    public void ShowButtons()
    {
        btn_Rectangle.Visible = true;
        btn_Square.Visible = true;
        btn_Ellipse.Visible = true;
        btn_Circle.Visible = true;
        btn_Triangle.Visible = true;
    }
    public void HideSettings()
    {
        btn_Draw.Visible = false;
        btn_Accept.Visible = false;
        btn_Cancel.Visible = false;
        btn_Reset.Visible = false;
        btn_Accept.Visible = false;
        trackBar_Size.Visible = false;
        trackBar_Stroke.Visible = false;
        trackBar_Corner.Visible = false;
        label_Corner.Visible = false;
        label_Height.Visible = false;
        label_Size.Visible = false;
        label_Stroke.Visible = false;
        rb_Both.Visible = false;
        rb_Height.Visible = false;
        rb_Width.Visible = false;
        textBox_Shape_Height.Visible =false;
        textBox_Shape_Width.Visible = false;
        lbl_Width.Visible = false;
    }
    public void ShowSettings()
    {
        btn_Draw.Visible = true;
        btn_Accept.Visible = true;
        btn_Cancel.Visible = true;
        btn_Reset.Visible = true;
        btn_Accept.Visible = true;
        trackBar_Size.Visible = true;
        trackBar_Stroke.Visible = true;
        trackBar_Corner.Visible = true;
        label_Corner.Visible = true;
        label_Height.Visible = true;
        label_Size.Visible = true;
        label_Stroke.Visible = true;
        rb_Both.Visible = true;
        rb_Height.Visible = true;
        rb_Width.Visible = true;
        textBox_Shape_Height.Visible = true;
        textBox_Shape_Width.Visible = true;
        lbl_Width.Visible = true;
    }
    #endregion

    #region //Rectangle Creation and Scaling
    private void btn_Rectangle_Click(object sender, EventArgs e)
    {
        HideButtons();
        ShowSettings();
    }

    public void paintRect(object sender, PaintEventArgs e)
    {
        ShapeClass s = new ShapeClass();
        s.paintRectangle(e);
    }
    #endregion

    #region //Show and Hide Grid (Checkbox Event)
    //Drawing the Grid
    private void checkBox_Grid_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox_Grid.Checked == true)
        {
            DrawGrid();
        }
        else if (!checkBox_Grid.Checked == true)
        {
            pictureBox_Canvas.Refresh();
            HideGrid();
        }
        else
            return;
    }
    private void DrawGrid()
    {
        Graphics grid = Graphics.FromImage(bm);
        int numOfCells = 301;
        int cellSize = 5;
        Pen p = new Pen(Color.LightSlateGray);
        p.Width = 0.5F;

        for (int y = 0; y < numOfCells; ++y)
        {
            grid.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
        }

        for (int x = 0; x < numOfCells; ++x)
        {
            grid.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
        }
        pictureBox_Canvas.Image = bm;
    }
    //Hides the Grid
    private void HideGrid()
    {
        Graphics grid = Graphics.FromImage(bm);
        Pen p = new Pen(Color.White);
        p.Width = 0.5F;
        int numOfCells = 301;
        int cellSize = 5;
        for (int y = 0; y < numOfCells; ++y)
        {
            grid.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
        }

        for (int x = 0; x < numOfCells; ++x)
        {
            grid.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
        }
        pictureBox_Canvas.Image = bm;
    }

    #endregion

    #region //Square Creation and Scaling

    private void btn_Square_Click(object sender, EventArgs e)
    {
        HideButtons();
        ShowSettings();
    } 

    #endregion

    #region //Circle Creation and Scaling
    private void btn_Circle_Click(object sender, EventArgs e)
    {
        HideButtons();
        ShowSettings();
    }
    #endregion

    #region //Ellipse Creation and Scaling
    private void btn_Ellipse_Click(object sender, EventArgs e)
    {
        HideButtons();
        ShowSettings();
    }
    #endregion

    #region //Triangle Creation and Scaling
    private void btn_Triangle_Click(object sender, EventArgs e)
    {
        HideButtons();
        ShowSettings();
    }
    #endregion

    #region //Accept Button
    private void btn_Accept_Click(object sender, EventArgs e)
    {
        HideSettings();
        ShowButtons();
    }
    #endregion

    #region //Reset Button
    private void btn_Reset_Click(object sender, EventArgs e)
    {
        HideSettings();
        ShowButtons();
    }
    #endregion

    #region //Cancel Button
    private void btn_Cancel_Click(object sender, EventArgs e)
    {
        HideSettings();
        ShowButtons();
    }
    #endregion

    #region //VALIDATIONS
    private void textBox_Shape_Width_TextChanged(object sender, EventArgs e)
    {
        if(Convert.ToInt32(textBox_Shape_Width.Text)>128)
        {
            MessageBox.Show("Width cannot exceed 128.");
            textBox_Shape_Width.Text = 128.ToString();
        }
    }

    private void textBox_Shape_Height_TextChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(textBox_Shape_Height.Text) > 128)
        {
            MessageBox.Show("Height cannot exceed 128");
            textBox_Shape_Height.Text = 128.ToString();
        }
    }
    #endregion

    private void trackBar_Size_ValueChanged(object sender, EventArgs e)
    {

    }





}

}

形状类别

namespace SealCreator2._0
{
    public class ShapeClass :Form1
    {

        Form1 f1 = new Form1();
        public double w1,h1, Rect_Width;
        public List<object> diffVar = new List<object>();
        public void paintRectangle(PaintEventArgs e)
        {
            int x, y;
            w1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeWidth()) * 96) / 25.4);
            h1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeHeight()) * 96) / 25.4);
            x = ((484 / 2) - (Convert.ToInt32(w1) / 2)); // Positions the Shape in the center of the PictureBox
            y = ((484 / 2) - (Convert.ToInt32(h1) / 2));// Positions the Shape in the center of the PictureBox
            //Draws a Rectangle
            Pen red = new Pen(Color.Red, 3);
            Rectangle rect = new Rectangle(x, y, Convert.ToInt32(w1), Convert.ToInt32(h1));
            e.Graphics.DrawRectangle(red, rect);
            diffVar.Add(w1);
            diffVar.Add(h1);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

添加另一个功能以更改参数。 例如:

void UpdateParameters()
{
  ChangeParameters();
}

再给您一个建议:您可以编辑此问题以添加代码。

答案 1 :(得分:0)

您可以创建包含PaintEventArgs和参数的其他类。例如:

public class DrawArgs
    {
        // parameter which you can modify base on your logic
        public int width; 
        public int height;
        public int x;     
        public int y;

        //your paint event
        public PaintEventArgs e;
    }

并修改您的ShapeClass.paintRectangle()方法:

public class ShapeClass
        {
            public void paintRectangle(DrawArgs drawArgs)
            {
                //w1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeWidth()) * 96) / 25.4);
                //h1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeHeight()) * 96) / 25.4);
                //x = ((484 / 2) - (Convert.ToInt32(w1) / 2));
                //y = ((484 / 2) - (Convert.ToInt32(h1) / 2));
                // get your params
                var x = drawArgs.x;
                var y = drawArgs.y;
                var w1 = drawArgs.width;
                var h1 = drawArgs.height;

                Pen red = new Pen(Color.Red, 3);
                Rectangle rect = new Rectangle(x, y, Convert.ToInt32(w1), Convert.ToInt32(h1));
                drawArgs.e.Graphics.DrawRectangle(red, rect);
            }
        }

您可以从paintRect method()调用它:

public void paintRect(object sender, PaintEventArgs e)
        {
            ShapeClass s = new ShapeClass();
            var drawArg = new DrawArgs
            {
                e = e,
                // and add your params here
                x = something,
                y = something,
                ///.....
            };

            s.paintRectangle(drawArg);
        }

P / S:这只是一个例子。您可以根据自己的逻辑修改DrawArgs类