Pages

Thursday, June 28, 2012

WPF Menu with Icons

Today I'll show how to build WPF Menu with Icons in XAML. In this example you'll see how to control IsEnabled state of MenuItem from your application. The application looks like this:
WPF Menu with Icons example
Ok, let's see the XAML (MainWindow.xaml):

Thursday, June 21, 2012

WPF TextBox Validation with IDataErrorInfo

There are so many ways for data validation in WPF that sometimes it confuses people. I decided to figure out what's the best way for me to do TextBox validation, which will be enough versatile for different tasks, but also as simple as possible for implementation. I believe a good code must be simple and easy to understand. So today I'll show, for my opinion, the best validation method on TextBox example.

WPF TextBox Validation with IDataErrorInfo

Monday, June 18, 2012

Reversi Game in C# WinForms


Reversi game in C# WinForms

Have you ever played Reversi table game? If not, today you'll play it on your PC!
Check out Reversi game written in C# WinForms using System.Drawing Library.
Today I will not explain the code itself. I don't think it's necessary because the code is pretty simple.
We have a table which is two dimensional array, the rest is logic...
You are welcome to download the source code of this game.

SQL Connection in C# (SqlConnection)

I have a simple SQL script which I need to run on SQL server using C#. The script returns an integer. The best way to do it in C# is to use SqlConnection, SqlCommand and SqlDataReader classes.
Here is the source code how we do it in C#:
using (SqlConnection sqlConnection = 
 new SqlConnection(@"Data Source=SERVER_NAME;Initial Catalog=TABLE_NAME;Integrated Security=True"))
 {
    sqlConnection.Open();
    using (SqlCommand sqlCommand = thisConnection.CreateCommand())
    {
        sqlCommand.CommandText = "SELECT MAX(Salary) Salary FROM STUFF";
    }
    using (SqlDataReader reader = thisCommand.ExecuteReader())
    {
        reader.Read();
        Int32 maxSalary = (Int32)reader["Salary"];
    }
}
As I said before if all you need is to run simple SQL script then this is the best approach for you.
But, If your application will work closely to database, updating and fetching data, then I suggest you to use LINQ to SQL Classes Generation in Visual Studio 2010.

Saturday, June 16, 2012

WPF Binding a Control to Another Control

Today I'll show how to bind a WPF control to another one directly in XAML.
In this example I have a Rectangle control and a Slider.
I want to change Rectangle's width by moving the Slider.
WPF Binding Between Controls Directly in XAML
Let's take a look on XAML:
<Grid>
    <StackPanel>            
        <Rectangle Name="elipse1" 
                   Fill="Black"
                   Margin="10,40"
                   Height="40"
                   Width="{Binding ElementName=slider1, Path=Value}"/>
        <Slider Name="slider1" 
                Minimum="20" 
                Maximum="200"
                Margin="20,0"/>            
    </StackPanel>
</Grid>
The main thing here is actually this string:
    Width="{Binding ElementName=slider1, Path=Value}"
Pretty simple example but I believe it will help to someone.
You are welcome to download the source code (Visual Studio 2010).

Thursday, June 14, 2012

C# Read Excel and Show in WPF DataGrid

In this example I will show how to read data from excel using C# and how to show it in WPF DataGrid.

In order to work with excel in C# you have to add reference to Microsoft.Office.Interop.Excel library.
As I said before I want also show the excel data in WPF DataGrid, so I created ExcelData class which contains Data property. My DataGrid will be bound to this Data property.
Let's see what we got inside of it:

Wednesday, June 13, 2012

WPF Animation (Fifteen Puzzle Game)

I must say WPF is a cool thing! Have you played sometime a Fifteen Puzzle Game?
Check out this Fifteen Puzzle Game as example of WPF Animation.

Fifteen Puzzle Game as example of WPF Animation


LINQ to SQL Classes Generation in Visual Studio 2010

Today I'll explain how to connect to MS-SQL database using LINQ to SQL classes in Visual Studio 2010.
If you building application that will work closely to database, will not only read, but also update the data on SQL server than LINQ to SQL will be extremely comfortable and fast way to work with.
Ok, here is step by step guide how to create LINQ to SQL classes and establish connection with database:

1. Mouse right click on your project in Visual Studio 2010 and choose Add New Item

2. Check LINQ to SQL classes. Visual Studio will generate dbml file for you.

Add New Item - Linq To SQL

Thursday, June 7, 2012

Enum with String (C#)

Sometimes you want to use enum with string values, not exact value of enum, but slightly different.
To achieve this we will create attribute class which will be used for storing string value for each enum.
In addition we need a helper class which will provide possibility to pull out these string values from enum.
OK, here is the attribute class:
public class StringValueAttribute : Attribute
{
    private string _value;
    
    public StringValueAttribute(string value)
    {
        _value = value;
    }
    
    public string Value
    {
        get { return _value; }
    }
}

Sunday, June 3, 2012

WPF Animation (Tower of Hanoi)


A friend of mine implemented well known problem The Tower of Hanoi in WPF.
I think it's a good example of WPF animation, so I decided to share it with you.
Tower Of Hanoi Problem
The Tower of Hanoi Problem
The key of the whole thing is Animate method:

private void Animate(int f, Canvas cn, double x, double y)
{
      Canvas.SetZIndex(cn, Canvas.GetZIndex(cn) * 10);
      DoubleAnimation dax = new DoubleAnimation();
      dax.Duration = new Duration(TimeSpan.FromSeconds(MOVe_SPEED));
      dax.From = cn.RenderTransformOrigin.X;
      dax.To = x;

      DoubleAnimation day = new DoubleAnimation();
      day.Duration = new Duration(TimeSpan.FromSeconds(MOVe_SPEED));
      day.From = cn.RenderTransformOrigin.Y;
      day.To = y;

      TranslateTransform tf = new TranslateTransform();
      cn.RenderTransform = tf;
      tf.SetValue(Canvas.LeftProperty, x);
      tf.SetValue(Canvas.TopProperty, y);
      tf.SetValue(dprop, f);
      tf.SetValue(CanvasProp, cn);

      tf.Changed += new EventHandler(tf_Changed);
      tf.BeginAnimation(TranslateTransform.XProperty, dax);
      tf.BeginAnimation(TranslateTransform.YProperty, day);
}
Please, download the source code here.
Check out another good example of WPF Application which I posted recently - Fifteen Puzzle Game.