Friday, March 1

Hibernate Example (Retrieve data from database)


package com.hibernate.example;




import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestEmployee {

public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure();
config.addAnnotatedClass(Employee.class);

new SchemaExport(config).create(true,true);

Employee emp1 = new Employee();
emp1.setEmpname("Umair");
Employee emp2 = new Employee();
emp2.setEmpname("Baig");


SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();


session.beginTransaction();
session.save(emp1);
session.save(emp2);


session.getTransaction().commit();
Query query = session.createQuery("From Employee");
List emp = query.list();


for(int i=0;i<emp.size();i++){
Employee empUser = (Employee) emp.get(i);
System.out.println("Employee Name : "+empUser.getEmpname()+"\n");
}
session.close();
}
}

HIbernate Example (Java Application)


package com.hibernate.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class Employee {
private int empid;
private String empname;

@Id
@GeneratedValue
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}

}




package com.hibernate.example;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestEmployee {

public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure();
config.addAnnotatedClass(Employee.class);

new SchemaExport(config).create(true,true);

Employee emp1 = new Employee();
emp1.setEmpname("Umair");
Employee emp2 = new Employee();
emp2.setEmpname("Baig");


SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();


session.beginTransaction();
session.save(emp1);
session.save(emp2);


session.getTransaction().commit();
session.close();


}

}


Monday, January 21

Print Document, Data Grid View in c# windows form application

Download source code: Download Here
C# 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;
using System.Windows.Forms;

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


            dataGridView1.Columns.Add("SrNO", "Sr No.");
            dataGridView1.Columns.Add("Empname", "Employee Name");



        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AllowUserToAddRows = false;



            dataGridView1.Rows.Clear();

            dataGridView1.Rows.Add();
            dataGridView1.Rows[0].Cells[0].Value = 1;
            dataGridView1.Rows[0].Cells[1].Value = "Umair";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                System.Windows.MessageBox.Show("Data is Empty");
            }
            else
            {
                printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font fh = new System.Drawing.Font(new FontFamily("Arial"), 12);
            Bitmap dataGridViewImage = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 15, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(dataGridViewImage, 0, 15);
            
            e.Graphics.DrawString("add1", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 200)));
            e.Graphics.DrawString("add2", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 215)));
            e.Graphics.DrawString("add3", fh, Brushes.Black, new System.Drawing.Point(0, (this.dataGridView1.Height + 230)));
            
        }

    }
}

Thursday, January 17

Filing in C# WPF Application

Download Source Code: FileCode
Xaml :

<Window x:Class="FileHandling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="190,106,0,0" Name="filedatatxt" VerticalAlignment="Top" Width="120" />
        <Button Content="Write Text" Height="23" HorizontalAlignment="Left" Margin="235,135,0,0" Name="writeBtn" VerticalAlignment="Top" Width="75" Click="writeBtn_Click" />
    </Grid>
</Window>




C#:


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FileHandling
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();




            // Reading from file which is located in debug folder
            // Begins
            string[] lines = File.ReadAllLines("Highscore.txt");
            foreach (string tempLines in lines) {
                MessageBox.Show(tempLines);
            }
            // Ends
        }

        private void writeBtn_Click(object sender, RoutedEventArgs e)
        {
            StreamWriter writeFile = new StreamWriter("Highscore.txt",true);
            writeFile.WriteLine(filedatatxt.Text);
            writeFile.Close();
        }
    }
}

Blur Effect in C# WPF Xaml

XAML Code :


<Window x:Class="blurEffects.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Height="40" Width="100" Content="Blur effect with radius 5">
            <Button.Effect>
                <BlurEffect Radius="5"></BlurEffect>
            </Button.Effect>
        </Button>
    </Grid>
</Window>

Cool Bounce Animation in C# and Xaml WPF

Source Code : Download here
Video Tutorial :
C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MainMenuTutorial
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            startMenuAnimation(label1, new Thickness(93, -30, 0, 0), label1.Margin);
            startMenuAnimation(label2, new Thickness(198, -30, 0, 0), label2.Margin);
            startMenuAnimation(label3, new Thickness(303, -30, 0, 0), label3.Margin);
        }
        public void startMenuAnimation(Label label, Thickness initialThickness, Thickness finalThickness) {
            ThicknessAnimation dropButtons = new ThicknessAnimation();
            dropButtons.From = initialThickness;
            dropButtons.To = finalThickness;
            BounceEase bounceEaseAnimation = new BounceEase();
            dropButtons.EasingFunction = bounceEaseAnimation;
            bounceEaseAnimation.Bounciness = 3;
            label.BeginAnimation(MarginProperty, dropButtons);
        }
        public void moveAnimation(Label label, Thickness initialMargin, Thickness finalMargin) {
            ThicknessAnimation ButtonMovement = new ThicknessAnimation();
            ButtonMovement.From = initialMargin;
            ButtonMovement.To = finalMargin;
            ButtonMovement.Duration = TimeSpan.FromMilliseconds(200);
            label.BeginAnimation(MarginProperty, ButtonMovement);
        }
        public void moveAnimation(Label label, Thickness initialMargin)
        {
            ThicknessAnimation ButtonMovement = new ThicknessAnimation();
            ButtonMovement.To = initialMargin;
            ButtonMovement.Duration = TimeSpan.FromMilliseconds(200);
            label.BeginAnimation(MarginProperty, ButtonMovement);
        }
        private void label1_MouseEnter(object sender, MouseEventArgs e)
        {
            moveAnimation(label1, label1.Margin, new Thickness(label1.Margin.Left, (label1.Margin.Top - 5), label1.Margin.Right, label1.Margin.Bottom));
        }

        private void label2_MouseEnter(object sender, MouseEventArgs e)
        {
            moveAnimation(label2, label2.Margin, new Thickness(label2.Margin.Left, (label2.Margin.Top - 5), label2.Margin.Right, label2.Margin.Bottom));
        }

        private void label3_MouseEnter(object sender, MouseEventArgs e)
        {
            moveAnimation(label3, label3.Margin, new Thickness(label3.Margin.Left, (label3.Margin.Top - 5), label3.Margin.Right, label3.Margin.Bottom));
        }

        private void label3_MouseLeave(object sender, MouseEventArgs e)
        {
            moveAnimation(label3, new Thickness(303, 243, 0, 0));
        }

        private void label1_MouseLeave(object sender, MouseEventArgs e)
        {
            moveAnimation(label1, new Thickness(93, 243, 0, 0));
        }
        private void label2_MouseLeave(object sender, MouseEventArgs e)
        {
            moveAnimation(label2, new Thickness(198, 243, 0, 0));
        }
    }
}



Xaml Code:

<Window x:Class="MainMenuTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <Grid>
        <Label Content="Menu Button 1" MouseEnter="label1_MouseEnter" MouseLeave="label1_MouseLeave" Height="28" HorizontalAlignment="Left" Margin="93,243,0,0" Name="label1" VerticalAlignment="Top" Width="Auto" BorderThickness="2" HorizontalContentAlignment="Center" FontWeight="Bold" FontStretch="Condensed">
            <Label.Effect>
                <DropShadowEffect BlurRadius="10" Color="Red" ShadowDepth="2" />
            </Label.Effect>
            <Label.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#B9310000" Offset="0" />
                    <GradientStop Color="#C3B44343" Offset="1" />
                </LinearGradientBrush>
            </Label.BorderBrush>
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFB44343" Offset="0" />
                    <GradientStop Color="#FF310000" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
            <Label.Foreground>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0" />
                    <GradientStop Color="#FF767676" Offset="1" />
                </LinearGradientBrush>
            </Label.Foreground>
        </Label>
        <Label BorderThickness="2" MouseEnter="label2_MouseEnter" MouseLeave="label2_MouseLeave" Content="Menu Button 2" FontStretch="Condensed" FontWeight="Bold" Height="28" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="198,243,0,0" Name="label2" VerticalAlignment="Top" Width="Auto">
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFB44343" Offset="0" />
                    <GradientStop Color="#FF310000" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
            <Label.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#B9310000" Offset="0" />
                    <GradientStop Color="#C3B44343" Offset="1" />
                </LinearGradientBrush>
            </Label.BorderBrush>
            <Label.Effect>
                <DropShadowEffect BlurRadius="10" Color="Red" ShadowDepth="2" />
            </Label.Effect>
            <Label.Foreground>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0" />
                    <GradientStop Color="#FF767676" Offset="1" />
                </LinearGradientBrush>
            </Label.Foreground>
        </Label>
        <Label BorderThickness="2" MouseEnter="label3_MouseEnter" MouseLeave="label3_MouseLeave" Content="Menu Button 3" FontStretch="Condensed" FontWeight="Bold" Height="28" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="303,243,0,0" Name="label3" VerticalAlignment="Top" Width="Auto">
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFB44343" Offset="0" />
                    <GradientStop Color="#FF310000" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
            <Label.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#B9310000" Offset="0" />
                    <GradientStop Color="#C3B44343" Offset="1" />
                </LinearGradientBrush>
            </Label.BorderBrush>
            <Label.Effect>
                <DropShadowEffect BlurRadius="10" Color="Red" ShadowDepth="2" />
            </Label.Effect>
            <Label.Foreground>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0" />
                    <GradientStop Color="#FF767676" Offset="1" />
                </LinearGradientBrush>
            </Label.Foreground>
        </Label>
    </Grid>
</Window>