Showing posts with label Windows Applications. Show all posts
Showing posts with label Windows Applications. Show all posts

How to Change button image on MouseHover,MouseLeave and MouseClick in windows forms C#.net || Button Events in Window form C#.net


In this I am showing about to change button image when we mouse hover on button,mouse leave on button and mouse click.

For that take one Form and add one button control from toolbox and add three images as normal Login button name as login_normal  ,mousehover Login button name as  login_hover  and mouseclick Login button image name as login_pressed for changing button images.

Then select button control properties and set image as normal image button.

Then write the following code in different events:


//for changing button image when mouse hover

private void btnLogin_MouseHover(object sender, EventArgs e)
        {
            this.btnLogin.Image = Properties.Resources.login_hover;
            this.Cursor = Cursors.Hand;   //to change cursor symbol

        }

//for changing button image when mouse leave

        private void btnLogin_MouseLeave(object sender, EventArgs e)
        {
            this.btnLogin.Image = Properties.Resources. login_normal ;
            this.Cursor = Cursors.Arrow; //to change cursor symbol
        }

//for changing button image when mouse click

        private void btnLogin_MouseDown(object sender, MouseEventArgs e)
        {
            this.btnLogin.Image = Properties.Resources. login_pressed;
            this.Cursor = Cursors.Arrow; //to change cursor symbol
        }

Then run application and see output with button image changing on mouse hover, mouse leave and mouse click.




How to set Splash Screen to window application with Timer control in C#.net


In this post I am showing about to set SplashScreen to any Windows forms application in C#.net.
For that first take one form name as SplashScreen in that take one picturebox control from toolbox and add any required image to picturebox.
Again take one form name as Login which you want to develop for your application in that add one Timer control set Timer control property Interval as 3000 then write the following code in Login.cs:

public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            Thread th = new Thread(new ThreadStart(SplashScreen));
            th.Start();
            Thread.Sleep(3000);
            th.Abort();
            Thread.Sleep(1000);
        }
        public void SplashScreen()
        {

            SplashScreen sp = new SplashScreen();
            sp.ShowDialog();


        }
}

Run your Login form then first you will get SplashScreen Image then after Login page will come.



How to Login a application by pressing Enter key in the Keyboard after entering username and password using windows forms in C#.net || Login application by pressing enter key from keyboard with out using mouse in windows forms C#.net


To login any application by pressing Enter key in the keyboard after entering username and password  for that first I am taking two labels, two textboxes and one login button for design.
The design is looks like below:



Then write the following code in Code behind side:
First write the code for login button as per your requirement
  private void btnLogin_Click(object sender, EventArgs e)
        {

         
            //Login Code


         }
First check enter key ASCII value with pressing key from keyboard.
Enter key ASCII value is 13.
To know all keyboard keys ASCII values Click Here.

Then write the following code in  password textbox key press event :
private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals(Convert.ToChar(13)))
            {
                btnLogin_Click(sender, e);
            }
        }


Then run your application and give username and password then click enter key then automatically it will login without  using mouse. 



How to Insert, Edit, Update and Delete Data with DataGridView in Windows Form C#.net ||Inserting , Updating and Deleting with DataGridView in Windows forms C#.net


In this article I am showing to Inserting , Editing , Updating and Deleting options with DataGridview.
For that I am Designing form with two textboxes with Name and Location ,DataGridview to display data and four buttons to Save , Edit , Update and Delete.
To do this just follow below steps:
·         In form load I am binding the data from database.
·         In save button click event saving data to database which are inserted into the name and location textboxes.
·         In Delete button click event  Deleting the selected row data in DataGridview from database.
·         In Edit button Click event filling the selected data from Gridview into Name and location textboxes.
·         In Update Button click event updating data which are edited the name and location textboxes.
Write the following code in Form.cs :
Form.cs Code :

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.Configuration;
using System.Data.SqlClient;

namespace savedata
{
    public partial class Form1 : Form
    {

        SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["Sqlcon"].ConnectionString);
        public Form1()
        {
            InitializeComponent();
            Bind();

        }

        private void Clear()
        {
            txtName.Text = string.Empty;
            txtLocation.Text = string.Empty;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert Into Test_Data(Name,Location) Values (@Name,@Location)", con);
            cmd.Parameters.AddWithValue("Name", txtName.Text);
            cmd.Parameters.AddWithValue("Location", txtLocation.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Inserted sucessfully");
            Bind();
            Clear();
        }

        private void Bind()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from Test_Data", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            SqlCommand delcmd = new SqlCommand();
            if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
            {
                delcmd.CommandText = "DELETE FROM Test_Data WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
                con.Open();
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
                con.Close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Row Deleted");
            }
            Bind();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Update Test_Data set Name=@Name,Location=@Location Where(Name=@Name)", con);
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Location", txtLocation.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("updated......");
            con.Close();
            Bind();
            Clear();
        }

        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            txtName.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            txtLocation.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
        }
    }
}


Then run the application you will get output like below:



How to Bind Data to ComboBox from Database in Windows Forms application C#.net || How to Set Default Select Item in ComboBox 0th index.


To bind data to ComboBox in Windows forms follow the steps below:

First Design form with One ComboBox from toolbox in visualstudio.
Then write one ComboBoxBind() method in code behind side and call that method in form load.


Form.cs Code :

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.Data.SqlClient;

namespace combox_binding
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=naresh;Initial Catalog=employee;User ID=sa;Password=123");
        public Form1()
        {
            InitializeComponent();
            ComboBoxBind ();
        }

        private void ComboBoxBind ()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select empID,empname from emp", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataRow dr;
            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "---Select an item---" };
            dt.Rows.InsertAt(dr, 0);
            comboBox1.DisplayMember = "empname";
            comboBox1.ValueMember = "empID";
            comboBox1.DataSource = dt;
            con.Close();
        }

    }
}

Then run the application and see the data will be bind to ComboBox.

OutPut :