c#中使用了一个新的类来利用窗体控件

本文关键字:控件 窗体 一个 | 更新日期: 2023-09-27 18:17:28

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

这个图表后面会有很多代码来进行数据计算,然后填充图表的属性。

我的表单类变得相当大,我现在想把所有这些新代码我要创建一个新的类(可能叫它"DynmChart")。

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

但是我的form类是winforms项目的一部分,它使用program.c作为它的主类。

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

这是我所有的代码。

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

程序类是我的MAIN类:

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#中使用了一个新的类来利用窗体控件

如果我没理解错的话…

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

但是,如果你坚持目前的方法:

你可以使用封装来分离Form1类(或者你的UserControl,如果你走那条路)和一个新的Calculation类之间的责任。UI类将负责显示任务,而Calculation类将负责数字处理。只需在Form1/你的UserControl中创建一个计算实例,并使用Calculation实例来返回你需要的各种计算的结果。

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

另一种方法,如果你需要大量的UI代码,它膨胀你的类文件,是使用部分类。