Tuesday, 1 May 2012

TextBox Colored Border

How to change Textbox border color when user enters in to it
Step 1:
Open visual studio ->New project-> Window form application
Add textbox from toolbox to the form and write the  following code

Form1.cs


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;

namespace textboxcolorborder
{
    public partial class Form1 : Form
    {
        bool focus = false;

        public Form1()
        {
            InitializeComponent();
           

        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            focus = true;
            this.Refresh();

        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            focus = false;
            this.Refresh();

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (focus)
            {
                textBox1.BorderStyle = BorderStyle.None;
                Pen p = new Pen(Color.Red);
                Graphics g = e.Graphics;
                int variance = 3;
                g.DrawRectangle(p, new Rectangle(textBox1.Location.X - variance, textBox1.Location.Y - variance, textBox1.Width + variance, textBox1.Height + variance));
            }
            else
            {
                textBox1.BorderStyle = BorderStyle.FixedSingle;
            }

        }

       
    }
}

 
Demo:

 

Using store procedure for retrieving records from database

How to use Stored Procedures to Retrieve the value to Grid view from database in Asp.net.

 

Here is the example for binding Gridview using stored procedure in asp.net. In this we can select the records by unique value in table and view in gridview. By entering the user number we can get the user details in gridview. It is just like getting the marks list when hall-ticket number is entered. we can use this application for such purpose.
To implement this concept you need to follow the below steps :
Step1 :
First you need to design a table in Sql Database to save the records in database and create a Stored Procedure.
Step2:
Create a new Asp.net website in Visual Studio and write the following source code in the design part of the Default.aspx page.

Source code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: 'Times New Roman', Times, serif; font-size: 25px; font-weight: bold; font-style: italic">
        Stored Procedures<br />
        <br />
    </div>
    <asp:Label ID="Label1" runat="server" Text="Enter User No."></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Enter" />
    <asp:RangeValidator ID="RangeValidator1" runat="server"
        ControlToValidate="TextBox1" ErrorMessage="1 - 5" MaximumValue="5"
        MinimumValue="1"></asp:RangeValidator>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
        GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    </form>
</body>
</html>

Step3:
Now open the Default.aspx.cs page and write the following source code.

Default.aspx.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=sridhar;Initial Catalog=Register;User ID=sa;Password=123");
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("userdetails",conn);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.Add(new SqlParameter("@pno", SqlDbType.Int));
        da.SelectCommand.Parameters["@pno"].Value = (TextBox1.Text).Trim();
        DataSet ds = new DataSet();
        da.Fill(ds,"person");
        GridView1.DataSource = ds.Tables["person"];
        GridView1.DataBind();
        da.Dispose();
        conn.Close();
    }
}


Step4:
Now build the Solution and Debug it for the output.

Output :

How to show Clock with system time using with Timer control in windows forms C#.net || Clock Example with System Time in window forms dot net.

Introduction:

The following article describes how to display clock in a window form using timer control.
Step 1:
Open Visual Studio->new Project->Window Form application

 Step 2: 
Go to Solution explorer right click on project solution add new item->User control with name "Clocktimer".
Add timer control  from toolbox to user control and write the following code in clocktimer.cs

clocktimer.cs
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace clockdisplay
{
    public partial class clocktimer : UserControl
    {
        const float PI = 3.141592654F;

        DateTime dateTime;

        float fRadius, fCenterX, fCenterY, fCenterCircleRadius, fHourLength;
        float fMinLength, fSecLength, fHourThickness, fMinThickness, fSecThickness;
        bool bDraw5MinuteTicks = true;
        bool bDraw1MinuteTicks = true;
        float fTicksThickness = 2;

        Color hrColor = Color.DarkGoldenrod;
        Color minColor = Color.DarkMagenta;
        Color secColor = Color.DarkSlateBlue;
        Color circleColor = Color.DarkSeaGreen;
        Color ticksColor = Color.Red;

        //    private System.Windows.Forms.Timer timer1;
       // private System.ComponentModel.IContainer components;
        public clocktimer()
        {
            InitializeComponent();
        }

        private void clocktimer_Load(object sender, EventArgs e)
        {
            dateTime = DateTime.Now;
            this.clocktimer_Resize_1(sender, e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.dateTime = DateTime.Now;
            this.Refresh();
        }
        private void DrawLine(float fThickness, float fLength, Color color, float fRadians, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(color, fThickness),
                fCenterX - (float)(fLength / 9 * System.Math.Sin(fRadians)),
                fCenterY + (float)(fLength / 9 * System.Math.Cos(fRadians)),
                fCenterX + (float)(fLength * System.Math.Sin(fRadians)),
                fCenterY - (float)(fLength * System.Math.Cos(fRadians)));
        }

        private void DrawPolygon(float fThickness, float fLength, Color color, float fRadians, System.Windows.Forms.PaintEventArgs e)
        {

            PointF A = new PointF((float)(fCenterX + fThickness * 2 * System.Math.Sin(fRadians + PI / 2)),
                (float)(fCenterY - fThickness * 2 * System.Math.Cos(fRadians + PI / 2)));
            PointF B = new PointF((float)(fCenterX + fThickness * 2 * System.Math.Sin(fRadians - PI / 2)),
                (float)(fCenterY - fThickness * 2 * System.Math.Cos(fRadians - PI / 2)));
            PointF C = new PointF((float)(fCenterX + fLength * System.Math.Sin(fRadians)),
                (float)(fCenterY - fLength * System.Math.Cos(fRadians)));
            PointF D = new PointF((float)(fCenterX - fThickness * 4 * System.Math.Sin(fRadians)),
                (float)(fCenterY + fThickness * 4 * System.Math.Cos(fRadians)));
            PointF[] points = { A, D, B, C };
            e.Graphics.FillPolygon(new SolidBrush(color), points);
        }


      
        public Color HourHandColor
        {
            get { return this.hrColor; }
            set { this.hrColor = value; }
        }

        public Color MinuteHandColor
        {
            get { return this.minColor; }
            set { this.minColor = value; }
        }

        public Color SecondHandColor
        {
            get { return this.secColor; }
            set
            {
                this.secColor = value;
                this.circleColor = value;
            }
        }

        public Color TicksColor
        {
            get { return this.ticksColor; }
            set { this.ticksColor = value; }
        }

        public bool Draw1MinuteTicks
        {
            get { return this.bDraw1MinuteTicks; }
            set { this.bDraw1MinuteTicks = value; }
        }

        public bool Draw5MinuteTicks
        {

            get { return this.bDraw5MinuteTicks; }
            set { this.bDraw5MinuteTicks = value; }
        }

        private void clocktimer_Paint_1(object sender, PaintEventArgs e)
        {
            float fRadHr = (dateTime.Hour % 12 + dateTime.Minute / 60F) * 30 * PI / 180;
            float fRadMin = (dateTime.Minute) * 6 * PI / 180;
            float fRadSec = (dateTime.Second) * 6 * PI / 180;

            DrawPolygon(this.fHourThickness, this.fHourLength, hrColor, fRadHr, e);
            DrawPolygon(this.fMinThickness, this.fMinLength, minColor, fRadMin, e);
            DrawLine(this.fSecThickness, this.fSecLength, secColor, fRadSec, e);


            for (int i = 0; i < 60; i++)
            {
                if (this.bDraw5MinuteTicks == true && i % 5 == 0) // Draw 5 minute ticks
                {
                    e.Graphics.DrawLine(new Pen(ticksColor, fTicksThickness),
           fCenterX + (float)(this.fRadius / 1.50F * System.Math.Sin(i * 6 * PI / 180)),
           fCenterY - (float)(this.fRadius / 1.50F * System.Math.Cos(i * 6 * PI / 180)),
           fCenterX + (float)(this.fRadius / 1.65F * System.Math.Sin(i * 6 * PI / 180)),
           fCenterY - (float)(this.fRadius / 1.65F * System.Math.Cos(i * 6 * PI / 180)));
                }
                else if (this.bDraw1MinuteTicks == true) // draw 1 minute ticks
                {

                    e.Graphics.DrawLine(new Pen(ticksColor, fTicksThickness),
          fCenterX + (float)(this.fRadius / 1.50F * System.Math.Sin(i * 6 * PI / 180)),
          fCenterY - (float)(this.fRadius / 1.50F * System.Math.Cos(i * 6 * PI / 180)),
          fCenterX + (float)(this.fRadius / 1.55F * System.Math.Sin(i * 6 * PI / 180)),
          fCenterY - (float)(this.fRadius / 1.55F * System.Math.Cos(i * 6 * PI / 180)));
                }
            }

            //draw circle at center
  e.Graphics.FillEllipse(new SolidBrush(circleColor), fCenterX - fCenterCircleRadius / 2, fCenterY - fCenterCircleRadius / 2, fCenterCircleRadius, fCenterCircleRadius);

        }

        private void clocktimer_Resize_1(object sender, EventArgs e)
        {
             this.Width = this.Height;
            this.fRadius = this.Height / 2;
            this.fCenterX = this.ClientSize.Width / 2;
            this.fCenterY = this.ClientSize.Height / 2;
            this.fHourLength = (float)this.Height / 3 / 1.85F;
            this.fMinLength = (float)this.Height / 3 / 1.20F;
            this.fSecLength = (float)this.Height / 3 / 1.15F;
            this.fHourThickness = (float)this.Height / 100;
            this.fMinThickness = (float)this.Height / 150;
            this.fSecThickness = (float)this.Height / 200;
            this.fCenterCircleRadius = this.Height / 50;
            timer1.Start();
        }

     }
}

 Step 3: 
 
Go to Form1[design].cs and add panel from toolbox then write the following code in Form1.cs 

Form1.cs

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;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            clocktimer obj = new clocktimer();
            panel1.Controls.Add(obj);
        }
    }
}
 
DEMO: