Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:12:33+00:00 2026-05-18T02:12:33+00:00

i created a winforms application. what it does is it queries a database and

  • 0

i created a winforms application. what it does is it queries a database and displays the data on a chart on the screen.

my question is, is it proper to use classes for this? i know the answer is probably yes, but i have no clue how to use a class for this,.

here is my code. please give me a few pieces of advice on how to turn this into a class if you think that is the right thing to do:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        private DataTable qResults = new DataTable();
        private void Form1_Load(object sender, EventArgs e)
        {
           string qcvalues_query = "SELECT DISTINCT name FROM qvalues ORDER by name";
           string analytes_query = "SELECT DISTINCT compound FROM qvalues ORDER by compound";
           string instruments_query = "SELECT DISTINCT instrument FROM batchinfo WHERE instrument <> '' AND instrument is not Null ORDER by instrument";

           dataGridView1.MultiSelect = false;

           cbAnalytes.DisplayMember = "name";
           cbAnalytes.DataSource = ConnectandReadList(qcvalues_query);

           cbQCValues.DisplayMember = "compound";
           cbQCValues.DataSource = ConnectandReadList(analytes_query);

           cbInstruments.DisplayMember = "instrument";
           cbInstruments.DataSource = ConnectandReadList(instruments_query);
        }
        private DataSet GetSeriesValues()
        {

            Series ser = this.chart1.Series["Series1"];

            DataSet dataSet = new DataSet();
            DataTable seriesTable = new DataTable(ser.Name);

            seriesTable.Columns.Add(new DataColumn("No", typeof(int)));
            seriesTable.Columns.Add(new DataColumn("X", typeof(string)));
            seriesTable.Columns.Add(new DataColumn("Y", typeof(double)));

            for (int count = 0; count < ser.Points.Count; count++)
            {
                DataPoint p = ser.Points[count];
                seriesTable.Rows.Add(new object[] { count, p.XValue, p.YValues[0] });
            }

            dataSet.Tables.Add(seriesTable);
            return dataSet;
        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            // Call Hit Test Method
            HitTestResult result = chart1.HitTest(e.X, e.Y);

            // Reset Data Point Attributes
            foreach (DataPoint point in chart1.Series[0].Points)
            {
                point.BackSecondaryColor = Color.Black;
                point.BackHatchStyle = ChartHatchStyle.None;
                point.BorderWidth = 1;
            }

            // If a Data Point or a Legend item is selected.
            if
            (result.ChartElementType == ChartElementType.DataPoint ||
                result.ChartElementType == ChartElementType.LegendItem)

                {
                    try
                    {
                        // Set cursor type 
                        this.Cursor = Cursors.Hand;


                        // Find selected data point
                        DataPoint point = chart1.Series[0].Points[result.PointIndex];

                        // Set End Gradient Color to White
                        point.BackSecondaryColor = Color.White;

                        // Set selected hatch style
                        point.BackHatchStyle = ChartHatchStyle.Percent25;

                        // Increase border width
                        point.BorderWidth = 2;
                    }
                    catch { }
                }
            else
            {
                // Set default cursor
                this.Cursor = Cursors.Default;
            }
        }


        private void InitializeChart()
        {
            chart1.Series["Series1"].ChartType = SeriesChartType.Line;
            chart1.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
            chart1.Series["Series1"].MarkerSize = 8;

            // Set series members names for the X and Y values
            chart1.Series["Series1"].XValueMember = "datapath";
            chart1.Series["Series1"].YValueMembers = "finalconc";



            chart1.DataBind();

            // Calculate Mean
            double mean = chart1.DataManipulator.Statistics.Mean("Series1");

            // Calculate Median
            double median = chart1.DataManipulator.Statistics.Median("Series1");

            // Calculate Standard Deviation from the Variance
            double variance = chart1.DataManipulator.Statistics.Variance("Series1", true);
            double standardDeviation = Math.Sqrt(variance);

            // Set Strip line item
            chart1.ChartAreas[0].AxisY.StripLines[0].IntervalOffset = mean - Math.Sqrt(variance);
            chart1.ChartAreas[0].AxisY.StripLines[0].StripWidth = 2.0 * Math.Sqrt(variance);

            // Set Strip line item
            chart1.ChartAreas[0].AxisY.StripLines[1].IntervalOffset = mean;

            // Set Strip line item
            chart1.ChartAreas[0].AxisY.StripLines[2].IntervalOffset = median;

            DataPoint maxValuePoint = chart1.Series["Series1"].Points.FindMaxByValue();
            DataPoint minValuePoint = chart1.Series["Series1"].Points.FindMinByValue();

            chart1.ChartAreas[0].AxisY.Maximum = maxValuePoint.YValues.Max();
            chart1.ChartAreas[0].AxisY.Minimum = minValuePoint.YValues.Min();

            // Refresh Chart
            chart1.Invalidate();

        }

        private DataTable ConnectandReadList(string query)
        {
            DataTable ds = new DataTable();
            string connection_string = "Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";
            using (var myConnection = new SqlConnection(connection_string))
            {
                myConnection.Open();
                var command = new SqlCommand(query, myConnection);
                var adapter = new SqlDataAdapter(command);
                adapter.Fill(ds);
            }
            return ds;
        }


        private void btnGenerateGraph_Click(object sender, EventArgs e)
        {
            string graph_query = @"SELECT top 1000 reporttime,
                    datapath,
                    finalconc, 
                    instrument 
                    FROM batchinfo  
                    JOIN qvalues ON batchinfo.rowid = qvalues.rowid
                    WHERE compound = '" + cbQCValues.Text + "'" +
                    "AND name = '" + cbAnalytes.Text + "'" +
                    "AND batchinfo.instrument = '" + cbInstruments.Text + "'" +
                    "AND batchinfo.reporttime LIKE '10/%/2010%'";


            qResults = ConnectandReadList(graph_query);
            if (qResults.Rows.Count == 0)
            {
                MessageBox.Show("Your query did not return any results!");
                return;
            }

            chart1.DataSource = qResults;
            InitializeChart();


            dataGridView1.Columns.Clear();
            dataGridView1.DataBindings.Clear();
            dataGridView1.DataSource = qResults;

        }

        private void chart1_MouseDown(object sender, MouseEventArgs e)
        {
            // Call Hit Test Method
            HitTestResult result = chart1.HitTest(e.X, e.Y);

            if (result.ChartElementType == ChartElementType.DataPoint)
            {

                dataGridView1.Rows[result.PointIndex].Selected = true;
                dataGridView1.FirstDisplayedScrollingRowIndex = result.PointIndex;

            }


        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Int32 selectedRowCount =
        dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
            if (selectedRowCount == 1)
            {                                   
                qResults.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                InitializeChart();                          

            }
        }


    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:12:34+00:00Added an answer on May 18, 2026 at 2:12 am

    I’m with slaks on this. Form1 is a class.

    The only real change I would possibly suggest at this point would be to turn that into a composite control. That way you could drop that functionality onto different forms as necessary.

    Check out the MS walkthrough and a smaller article here.

    You might do that for no other reason than to learn something new.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a dialog box in my WinForms application. This has many text
I'm developing a WinForms c# 3.0 application. Our designer created quite a lot of
I want my WinForms application to create an instance of itself for each screen
I have created a C# WinForms application. On my computer the following works: DateTime.ParseExact(13/05/2012,
I have created a .NET C# WinForms application on Win 7 RTM x64, which
I’ve created a winform application that communicates with a WCF service. The winform displays
I am writing WinForms application, and so far it contains 37 forms. This is
This is getting extremely irritating. Right now I have a winforms application, and things
Does anyone know how to use the RegisterHotKey/UnregisterHotKey API calls in a console application?
I am developing a WinForms application and want to send a binary image data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.