【问题标题】:C# using a new class to utilise a forms controls?C# 使用新类来利用表单控件?
【发布时间】:2011-12-08 16:40:14
【问题描述】:

我想新建一个动态图表控件。

此图表将有大量代码用于计算数据,然后填充图表的属性。

我的表单类变得相当大,我现在正在考虑将我要创建的所有这些新代码放入一个新类中(可能称之为“DynmChart”)。

通常要使用控件,我会创建一个新类,然后在新类中实例化表单。

但我的表单类是使用 program.c 作为其主类的 winforms 项目的一部分。 main 包含 application.run Form1。

那么我将如何创建计算然后填充表单中的控件的所有代码等。

这是我所有的代码。

2 类

Form1 程序

namespace RepSalesNetAnalysis
{
public partial class Form1 : Form
{
    float top = 0;
    float tmid = 0;
    float bmid = 0;
    float bottom = 0;
    float tempTop = 0;
    float tempMidt = 0;
    float tempMidB = 0;
    float tempBot = 0;
    string meh;   
    DataTable pgTable = new DataTable();

    public Form1()
    {
        InitializeComponent();
        pictureBox2.Visible = false; 

    }

    //button to run SalesFigures
    private void button1_Click_1(object sender, EventArgs e)
    {
        checkBox1.Checked = true;
        string acct = accCollection.Text;
        Task t = new Task(() => GetsalesFigures(acct));
        t.Start(); 
    }

    //invoke method for setting the picture box visible(grabs the control out of the form to use in a thread)
    private void SetPictureBoxVisibility(bool IsVisible)
    {
        if (pictureBox2.InvokeRequired)
        {
            pictureBox2.Invoke(new Action<bool>(SetPictureBoxVisibility), new Object[] { IsVisible });
        }
        else
        {
            pictureBox2.Visible = IsVisible;
        }
    }

    //invoke method for a check box toggle(to be used for testing
    private void SetCheckBoxValue(bool IsChecked)
    {
        if (checkBox1.InvokeRequired)
        {
            pictureBox2.Invoke(new Action<bool>(SetCheckBoxValue), new Object[] { IsChecked });
        }
        else
        {
            checkBox1.Checked = IsChecked;
        }
    }

    //invoke method to add customers and pass it to the sales method
    private void AddItem(string value)
    {
        if (accCollection.InvokeRequired)
        {
            accCollection.Invoke(new Action<string>(AddItem), new Object[] { value });
        }
        else
        {
            accCollection.Items.Add(value);
        }
    }

    //invoke method to set the datagrid properties
    private void SetDataGrid(bool AutoGenerateColumns, Object DataSource, String DataMember, DataGridViewAutoSizeColumnsMode Mode)
    {
        if (this.dataGridView1.InvokeRequired)
        {
            this.dataGridView1.Invoke(new Action<bool, Object, String, DataGridViewAutoSizeColumnsMode>(SetDataGrid),
                                      AutoGenerateColumns, DataSource, DataMember, Mode);
        }
        else
        {
            this.dataGridView1.AutoGenerateColumns = AutoGenerateColumns;
            this.dataGridView1.DataSource = DataSource;
            this.dataGridView1.DataMember = DataMember;
            dataGridView1.AutoResizeColumns(Mode);
        }
    }

    //on form load run the accounts too combco box method
    private void Form1_Load(object sender, EventArgs e)
    {
        AutofillAccounts();
    }

    //method for task to get sales info
    private void GetsalesFigures(string Acct)
    {
        try
        {
            string myConn = "Server=sgsg;" +
                            "Database=shaftdata;" +
                            "uid=bsgsg;" +
                            "pwd=drsgsg;" +
                            "Connect Timeout=120;";

            string acct;// test using 1560
            SqlConnection conn = new SqlConnection(myConn);
            SqlCommand Pareto = new SqlCommand();
            BindingSource bindme = new BindingSource();
            SqlDataAdapter adapt1 = new SqlDataAdapter(Pareto);
            DataSet dataSet1 = new DataSet();
            DataTable table1 = new DataTable();

            acct = Acct;

            string fromDate = this.dateTimePicker1.Value.ToString("MM/dd/yyyy");
            string tooDate = this.dateTimePicker2.Value.ToString("MM/dd/yyyy");

            Pareto.Connection = conn;
            Pareto.CommandType = CommandType.StoredProcedure;
            Pareto.CommandText = "dbo.GetSalesParetotemp";
            Pareto.CommandTimeout = 120;

            Pareto.Parameters.AddWithValue("@acct", acct);
            Pareto.Parameters.AddWithValue("@from", fromDate);
            Pareto.Parameters.AddWithValue("@too", tooDate);

            SetCheckBoxValue(true);
            SetPictureBoxVisibility(true);

            adapt1.Fill(dataSet1, "Pareto");

            SetCheckBoxValue(false);
            SetPictureBoxVisibility(false);

            SetDataGrid(true, dataSet1, "Pareto", DataGridViewAutoSizeColumnsMode.AllCells);

            dataGridView1.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCells);
        }
        catch (Exception execc)
        {
            MessageBox.Show("Whoops! Seems we couldnt connect to the server!"
                            + " information:\n\n" + execc.Message + execc.StackTrace,
                            "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        } 
    }

    //method non-task to get customer info
    private void AutofillAccounts()
    {
        try
        {
            string myConn1 = "Server=sgsdg;" +
                                "Database=AutoPart;" +
                                "uid=bsgdg;" +
                                "pwd=dsgsg;" +
                                "Connect Timeout=6000;";
            SqlConnection conn1 = new SqlConnection(myConn1);
            conn1.Open();
            SqlCommand accountFill = new SqlCommand("SELECT keycode FROM dbo.Customer", conn1);
            SqlCommand pgFill = new SqlCommand("SELECT DISTINCT pg FROM dbo.Product", conn1);


            SqlDataReader readacc = accountFill.ExecuteReader();

            while (readacc.Read())
            {
                AddItem(readacc.GetString(0).ToString());
            }
            conn1.Close();
            ////////////////////////////////////////
            conn1.Open();
            SqlDataReader readpg = pgFill.ExecuteReader();

            while (readpg.Read())
            {
                cmbPrdGrp.Items.Add(readpg.GetString(0).ToString());
            }
            conn1.Close();
        }
        catch(Exception exc1)
        {
            MessageBox.Show("Whoops! Seems we couldnt connect to the server!"
                            + " information:\n\n" + exc1.Message + exc1.StackTrace,
                            "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }
    //spare method for testing
    private void spare()
    {
        chartControl1.Series.Clear();
        chartControl2.Series.Clear();

        meh = cmbPrdGrp.Text;

        bottom = 0;
        bmid = 0;
        tmid = 0;
        top = 0;

        double countTot = 0;
        double countPg = 0;
        double percent = 0;
        //ok lets set the properties on click
        //get pareto data from datagrid using count
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            //count total
            countTot++;
            if ((string)row.Cells["Pg"].Value == meh)
            {
                //need to some how count pgs hmmmm
                //countpg
                countPg++;
                if ((int)row.Cells["Pareto"].Value <= 50)
                {
                    //top 50
                    //top++;
                    tempTop = Convert.ToSingle(row.Cells["Qty"].Value);
                    top += tempTop;
                }
                else
                    if (((int)row.Cells["Pareto"].Value > 50) && ((int)row.Cells["Pareto"].Value <= 100))
                    {
                        //50-100
                        tempMidt = Convert.ToSingle(row.Cells["Qty"].Value);
                        tmid += tempMidt;
                    }
                    else
                        if (((int)row.Cells["Pareto"].Value > 100) && ((int)row.Cells["Pareto"].Value <= 200))
                        {
                            //100-200
                            tempMidB = Convert.ToSingle(row.Cells["Qty"].Value);
                            bmid += tempMidB;
                        }
                        else
                        {
                            //its over 200!!!!!!!!!!!!!!
                            tempBot = Convert.ToSingle(row.Cells["Qty"].Value);
                            bottom += tempBot;
                        }
            }

        }

        textBox1.Text = top.ToString();
        textBox2.Text = tmid.ToString();
        textBox3.Text = bmid.ToString();
        textBox4.Text = bottom.ToString();

        //calc percent
        percent = (countPg / countTot);
        //String.Format("{%#0:00}", percent);
        //display counts as percentage of total
        textBox5.Text = countTot.ToString();
        textBox6.Text = percent.ToString("p1");

        double[] yValues = { bottom, bmid, tmid, top };
        string[] xNames = { "Greater than 200", "Between 200-100", "Between 100-50", "Below 50" };

        //chart1.Series[0].Points.DataBindXY(xNames, yValues);

        DataTable chartTable = new DataTable("Table1");

        // Add two columns to the table.
        chartTable.Columns.Add("Names", typeof(string));
        chartTable.Columns.Add("Value", typeof(Int32));
        chartTable.Rows.Add("Below 50", top);
        chartTable.Rows.Add("Between 50-100", tmid);
        chartTable.Rows.Add("Between 100-200", bmid);
        chartTable.Rows.Add("Greater than 200", bottom);

        Series series1 = new Series("Series1", ViewType.Pie3D);
        Series series2 = new Series("Series2", ViewType.Bar);

        chartControl2.Series.Add(series1);
        chartControl1.Series.Add(series2);
        series1.DataSource = chartTable;
        series2.DataSource = chartTable;
        series1.ArgumentScaleType = ScaleType.Qualitative;
        series2.ArgumentScaleType = ScaleType.Qualitative;
        series1.ArgumentDataMember = "names";
        series2.ArgumentDataMember = "names";
        series1.ValueScaleType = ScaleType.Numerical;
        series2.ValueScaleType = ScaleType.Numerical;
        series1.ValueDataMembers.AddRange(new string[] { "Value" });
        series2.ValueDataMembers.AddRange(new string[] { "Value" });

        //series1.Label.PointOptions.PointView = PointView.ArgumentAndValues;
        series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;
        series2.LegendPointOptions.PointView = PointView.ArgumentAndValues;
        series1.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
        series2.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
        series1.LegendPointOptions.ValueNumericOptions.Precision = 0;
        series2.LegendPointOptions.ValueNumericOptions.Precision = 0;

        // Adjust the value numeric options of the series.
        series1.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
        //series2.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
        series1.Label.PointOptions.ValueNumericOptions.Precision = 0;
        //series2.Label.PointOptions.ValueNumericOptions.Precision = 0;

        // Adjust the view-type-specific options of the series.
        ((Pie3DSeriesView)series1.View).Depth = 20;
        ((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[0]);
        ((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[1]);
        ((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[2]);
        ((Pie3DSeriesView)series1.View).ExplodedPoints.Add(series1.Points[3]);
        ((Pie3DSeriesView)series1.View).ExplodedDistancePercentage = 20;
        //((BarSeriesView)series2.View).


        chartControl2.Legend.Visible = true;
        chartControl1.Legend.Visible = true;

        //series1.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.Currency;

    }

    //spare button for testing
    private void tempButton_Click(object sender, EventArgs e)
    {
        spare();
        SpendsAnalysis();
    }

    private void Close_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void Piebutton_Click(object sender, EventArgs e)
    {
        /*Rectangle tabArea;
        RectangleF tabTextArea;

        Bitmap B = new Bitmap(250, 250, PixelFormat.Format32bppArgb);

        tabArea = new Rectangle(1, 1, 240, 240);
        tabTextArea = new RectangleF(1, 1, 240, 240);

        using (Graphics g = Graphics.FromImage(B))
        {
            int i1 = bottom;
            int i2 = bmid;
            int i3 = tmid;
            int i4 = top;

            float total = i1 + i2 + i3 + i4;
            float deg1 = (i1 / total) * 360;
            float deg2 = (i2 / total) * 360;
            float deg3 = (i3 / total) * 360;
            float deg4 = (i4 / total) * 360;

            Font font = new Font("Arial", 10.0f);
            SolidBrush brush = new SolidBrush(Color.Red);
            Pen p = new Pen(Color.Empty, 0);

            Brush b1 = new SolidBrush(Color.DarkRed);
            Brush b2 = new SolidBrush(Color.DarkOrange);
            Brush b3 = new SolidBrush(Color.DarkGray);
            Brush b4 = new SolidBrush(Color.DarkViolet);

            //g.DrawRectangle(p, tabArea);

            g.DrawPie(p, tabTextArea, 0, deg1);
            g.FillPie(b1, tabArea, 0, deg1);
            g.DrawPie(p, tabTextArea, deg1, deg2);
            g.FillPie(b2, tabArea, deg1, deg2);
            g.DrawPie(p, tabTextArea, deg2 + deg1, deg3);
            g.FillPie(b3, tabArea, deg2 + deg1, deg3);
            g.DrawPie(p, tabTextArea, deg3 + deg2 + deg1, deg4);
            g.FillPie(b4, tabArea, deg3 + deg2 + deg1, deg4);

            //set picturebox3 as data source??
            pictureBox3.Image = B;

            textBox1.Text = top.ToString();
            textBox2.Text = tmid.ToString();
            textBox3.Text = bmid.ToString();
            textBox4.Text = bottom.ToString();
            //chart1.DataBind(x);
        }*/
    }

    //need to create a more dynamic chart that will give details via PG.
    //iether by internal filtering OR via queries(queries take time)

    private void ChartByPrdGrp()
    {
        //sort data from frid via grp
        foreach (DataGridViewRow pgrow in dataGridView1.Rows)
        {
            if ((string)pgrow.Cells["Pg"].Value == meh)
            {
                //add this row to new table called boots
                pgTable.Rows.Add(dataGridView1.Rows);//this?
                pgTable.Rows.Add(pgrow);//or this?
            } 
        }
    }

    private void SpendsAnalysis()
    {
        float tempQtypg = 0;
        float tempPricepg = 0;
        double tempTotpg = 0;
        double totalpg = 0;
        float tempQty = 0;
        float tempPrice = 0;
        float tempTot = 0;
        float total = 0;
        float qtyPg = 0;
        float qty = 0;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            tempQty = Convert.ToSingle(row.Cells["Qty"].Value);
            tempPrice = Convert.ToSingle(row.Cells["Unit"].Value);

            tempTot = tempQty * tempPrice;
            total += tempTot;
            qty += tempQty;

            if ((string)row.Cells["Pg"].Value == meh)
            {
                //tempQty = (float)row.Cells["Qty"].Value;
                tempQtypg = Convert.ToSingle(row.Cells["Qty"].Value);
                tempPricepg = Convert.ToSingle(row.Cells["Unit"].Value);

                tempTotpg = tempQtypg * tempPricepg;
                totalpg += tempTotpg;
                qtyPg += tempQtypg;
            }
        }
        textBox12.Text = total.ToString("c");
        textBox7.Text = totalpg.ToString("c");
        textBox13.Text = qty.ToString();
        textBox8.Text = qtyPg.ToString();
    }
  }
}

程序类是我的主类:

namespace RepSalesNetAnalysis
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1()); 
    } 
  }
}

任何帮助都会很棒! 非常感谢!

【问题讨论】:

  • 关闭不是因为你的拼写和语法不好,而是因为它太宽泛了......
  • 是的,所以我试图做一个更好的。好点?还是仍然很广泛?

标签: c# winforms oop charts


【解决方案1】:

如果我理解你的问题是正确的......

DynmChart 应该被创建为UserControl 并在需要的地方使用。

但是,如果您坚持当前的方法:

您可以使用Encapsulation 来分隔Form1 类(或者如果您走那条路线,则使用您的UserControl)和新的Calculation 类之间的职责。 UI 类将负责显示任务,而Calculation 类将负责数字运算。只需在 Form1/您的 UserControl 中创建一个 Calculation 实例,然后使用 Calculation 实例返回您需要的各种计算的结果。

如果您的控件的 UI 部分在一个文件中的代码量方面变得难以管理,首先要考虑的是使用 #region 指令来隐藏您不处理的部分代码(组织代码分成逻辑上属于一起的块)。

另一种方法是,如果您需要大量 UI 代码并且它会使您的类文件膨胀,则使用 Partial Classes

【讨论】:

  • 感谢你们的cmets。我已经有了一个部分类,它是 form1 类的一部分,这只是创建了所有控件。
  • 将我所有的数据处理移动到一个单独的类似乎很困难,因为我使用线程中的任务对象。所以我担心如果我开始换入换出,我将无法正常工作。
  • @Steven:从长远来看,如果你花时间学习如何正确组织代码,你会得到更好的结果,即使这个项目需要更长的时间。单个类承担的责任越多(间接地,文件越大),理解和发展自己的代码就越难。
  • 在查看我的代码时,谁能给我一个提示,到目前为止还可以吗?这是让你的眼睛流血吗? “X 在这里会更好”或“在这个 Z 做 Y”等等。
猜你喜欢
  • 1970-01-01
  • 2011-11-22
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多