C#使用新类使用表单控件?

C#使用新类使用表单控件?,c#,winforms,oop,charts,C#,Winforms,Oop,Charts,我想创建一个新的动态图表控件 这个图表后面有很多代码,用于计算数据,然后填充图表的属性 我的表单类越来越大,我现在正在考虑将我要创建的所有新代码放在一个新类中(可能称之为“DynmChart”) 通常要使用控件,我会创建一个新类,然后在新类中实例化表单 但是我的表单类是winforms项目的一部分,该项目使用program.c作为其主类。 main包含application.run Form1 那么,我将如何创建所有计算并填充表单中控件的代码等呢 这是我所有的代码 2类 表格1 节目 命名空间R

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

这个图表后面有很多代码,用于计算数据,然后填充图表的属性

我的表单类越来越大,我现在正在考虑将我要创建的所有新代码放在一个新类中(可能称之为“DynmChart”)

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

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

那么,我将如何创建所有计算并填充表单中控件的代码等呢

这是我所有的代码

2类

表格1 节目

命名空间RepSalesNetAnalysis
{
公共部分类Form1:Form
{
浮顶=0;
浮动tmid=0;
浮动bmid=0;
浮底=0;
浮点数=0;
浮点tempMidt=0;
浮点tempMidB=0;
float-tempBot=0;
字符串meh;
DataTable pgTable=新DataTable();
公共表格1()
{
初始化组件();
pictureBox2.可见=假;
}
//按钮运行SalesFigures
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
checkBox1.Checked=true;
字符串acct=accCollection.Text;
任务t=新任务(()=>GetsalesFigures(acct));
t、 Start();
}
//调用方法将图片框设置为可见(将控件从窗体中抓取出来以在线程中使用)
私有void setPictureBoxibility(布尔值可见)
{
如果(pictureBox2.InvokeRequired)
{
调用(新操作(setPictureBoxibility),新对象[]{IsVisible});
}
其他的
{
pictureBox2.Visible=可见;
}
}
//为复选框切换调用方法(用于测试
私有void SetCheckBoxValue(布尔值已检查)
{
如果(复选框1.invokererequired)
{
调用(新操作(SetCheckBoxValue),新对象[]{IsChecked});
}
其他的
{
checkBox1.Checked=IsChecked;
}
}
//调用方法来添加客户并将其传递给sales方法
专用void附加项(字符串值)
{
if(accCollection.invokererequired)
{
调用(新操作(AddItem),新对象[]{value});
}
其他的
{
accCollection.Items.Add(增值);
}
}
//调用方法来设置datagrid属性
私有void SetDataGrid(布尔自动生成列、对象数据源、字符串数据成员、DataGridViewAutoSizeColumnsMode)
{
if(this.dataGridView1.invokererequired)
{
this.dataGridView1.Invoke(新操作(SetDataGrid),
自动生成列、数据源、数据成员、模式);
}
其他的
{
this.dataGridView1.AutoGenerateColumns=AutoGenerateColumns;
this.dataGridView1.DataSource=数据源;
this.dataGridView1.DataMember=DataMember;
dataGridView1.AutoResizeColumns(模式);
}
}
//在表单加载时运行accounts To combco box方法
私有void Form1\u加载(对象发送方、事件参数e)
{
自动填充帐户();
}
//任务获取销售信息的方法
私有void GetsalesFigures(字符串帐户)
{
尝试
{
字符串myConn=“Server=sgsg;”+
“数据库=shaftdata;”+
“uid=bsgsg;”+
“pwd=drsgsg;”+
“连接超时=120;”;
string acct;//使用1560进行测试
SqlConnection conn=新的SqlConnection(myConn);
SqlCommand Pareto=新SqlCommand();
BindingSource bindme=新的BindingSource();
SqlDataAdapter Adapte1=新的SqlDataAdapter(Pareto);
DataSet dataSet1=新数据集();
DataTable1=新的DataTable();
acct=acct;
string fromDate=this.dateTimePicker1.Value.ToString(“MM/dd/yyyy”);
字符串tooDate=this.dateTimePicker2.Value.ToString(“MM/dd/yyyy”);
帕累托连接=连接;
Pareto.CommandType=CommandType.StoredProcess;
Pareto.CommandText=“dbo.GetSalesParetotemp”;
Pareto.CommandTimeout=120;
Pareto.Parameters.AddWithValue(“@acct”,acct);
Pareto.Parameters.AddWithValue(“@from”,fromDate);
Pareto.Parameters.AddWithValue(“@too”,tooDate);
SetCheckBoxValue(真);
SetPictureBoxibility(真);
1.填充(数据集1,“帕累托”);
SetCheckBoxValue(假);
SetPictureBoxibility(假);
SetDataGrid(true,dataSet1,“Pareto”,DataGridViewAutoSizeColumnsMode.AllCells);
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch(异常execc)
{
Show(“哎呀!好像我们无法连接到服务器!”
+“信息:\n\n”+execc.Message+execc.StackTrace,
“致命错误”,MessageBoxButtons.OK,MessageBoxIcon.Stop);
} 
}
//获取客户信息的非任务方法
私有无效自动填充帐户()
{
尝试
{
string myConn1=“Server=sgsdg;”+
“数据库=自动分割;”+
“uid=bsgdg;”+
“pwd=dsgsg;”+
“连接超时=6000;”;
SqlConnection conn1=新的SqlConnection(myConn1);
conn1.Open();
SqlCommand accountFill=newsqlcommand(“从dbo.Customer中选择keycode”,conn1);
SqlCommand-pgF
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()); 
    } 
  }
}