Saturday, 7 March 2015

Display Date and Time in StatusStrip / StatusBar in Windows Form Application

Generally, You have seen many applications in which Date and Time is displayed in the StatusBar. In this Article, We will see How to Display Date and Time in StatusStrip / StatusBar in Windows Form Application.

Let's Begin:
1. Open Visual Studio and Create a New Windows Form Application.
2. Drop StatusStrip Control from the ToolBox.
3. Click on down arrow of StatusStrip and add StatusLabel on it (named as toolStripStatusLabel1).
Set Text of toolStripStatusLabel1 as Empty or "".
4. Drop the Timer control from the ToolBox -> Go to Properties of timer1 control -> Set Interval to 1000 milliseconds and Enabled to True.
5. Create Tick event of timer1 control by double clicking on the timer1 control and Add following line of code:
using System;
using System.Windows.Forms;

namespace StatusBarDateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Timer Tick Event
        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Today.ToLongDateString();
        }
    }
}
Preview:

Display Current Date and Time:
using System;
using System.Windows.Forms;

namespace StatusBarDateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Timer Tick Event
        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.ToString();
        }
    }
}
Preview:
I hope you like it. Thanks.
[Download Source Code via Google Drive]

Sunday, 1 March 2015

Chart Control in Windows Form Application

In this Article, We will see How to use the Chart control in Windows Form Application. As we know that Charts and Graphs makes data easier to understand and interpret. Chart is used to present the data in visual form.

Let's Begin:
1. Open Visual Studio (I am using Visual Studio 2012) and Create new Windows Form Application (.NET Framework 4).
2. Drop Chart control from Toolbox present in the Data tab.

3. Go to Chart properties -> Click on Series.
Change the Name of Series. Here, I set Name as Salary. We can also change the ChartType as well as appearance from the Series Collection Editor.
Go to Form1.cs Code and add these line of code:
using System;
using System.Windows.Forms;

namespace DemoChart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillChart();
        }
        //fillChart method
        private void fillChart()
        {
            //AddXY value in chart1 in series named as Salary
            chart1.Series["Salary"].Points.AddXY("Ajay", "10000");
            chart1.Series["Salary"].Points.AddXY("Ramesh", "8000");
            chart1.Series["Salary"].Points.AddXY("Ankit", "7000");
            chart1.Series["Salary"].Points.AddXY("Gurmeet", "10000");
            chart1.Series["Salary"].Points.AddXY("Suresh", "8500");
            //chart title
            chart1.Titles.Add("Salary Chart");
        }
    }
}
Preview:

Displaying Data in Chart control from the Database in Windows Form Application:
1. Create a Database (named as Sample). Add a Table tbl_EmpSalary. The following is the table schema for creating tbl_EmpSalary.
Follow above steps. Add these lines of Code in Form1.cs Code:
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace DemoChart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillChart();
        }
        //fillChart method
        private void fillChart()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
            DataSet ds = new DataSet();
            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter("Select Name,Salary from tbl_EmpSalary", con);
            adapt.Fill(ds);
            chart1.DataSource = ds;
            //set the member of the chart data source used to data bind to the X-values of the series
            chart1.Series["Salary"].XValueMember = "Name";
            //set the member columns of the chart data source used to data bind to the X-values of the series
            chart1.Series["Salary"].YValueMembers = "Salary";
            chart1.Titles.Add("Salary Chart");
            con.Close();

        }
    }
}
Final Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+