首页 > 解决方案 > 更新另一个类的值

问题描述

我有这段代码使用 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);
        }
    }
}

标签: c#winforms

解决方案


添加另一个函数来更改参数。例如:

void UpdateParameters()
{
  ChangeParameters();
}

给你另一个建议:你可以编辑这个问题来添加代码。


推荐阅读