C # используя новый класс, чтобы использовать формы управления? - PullRequest
0 голосов
/ 08 декабря 2011

Я хотел бы создать новый динамический элемент управления диаграммой.

В этой диаграмме будет много кода, который обрабатывает данные и затем заполняет свойства диаграммы.

Мой класс формы становится довольно большим, и сейчас я думаю о том, чтобы поместить весь этот новый код, который я собираюсь создать, в новый класс (возможно, назовите его «DynmChart»).

Обычно для использования элементов управления я создаю новый класс, а затем создаю экземпляр формы в новом классе.

Но мой класс формы является частью проекта winforms, который использует program.c в качестве основного класса. 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()); 
    } 
  }
}

любая помощь будет отличной! Большое спасибо заранее!

1 Ответ

2 голосов
/ 08 декабря 2011

Если я правильно понимаю ваш вопрос ...

DynmChart, вероятно, следует создать как UserControl и использовать в формах там, где это необходимо.

Однако, если вы остаетесь с текущим подходом:

Вы можете использовать Инкапсуляция , чтобы разделить обязанности между классом Form1 (или вашим UserControl, если вы идете по этому маршруту) и новый Calculation класс.Класс пользовательского интерфейса будет отвечать за отображение задач, в то время как класс Calculation будет отвечать за сокращение чисел.Просто создайте экземпляр Вычисления в Form1 / ваш UserControl и используйте экземпляр Calculation, чтобы вернуть результаты различных необходимых вам вычислений.

Если часть пользовательского интерфейса вашего элемента управления становится неуправляемой с точки зрения количествакод в одном файле, первое, что нужно рассмотреть, это использование директив # region , чтобы скрыть части кода, над которым вы не работаете (организовать код в блоки, которые логически связаны друг с другом).

Другой подход, если вам нужно много UI-кода и он раздувает ваш файл классов, это использовать Partial Classes .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...