Tuesday 28 August 2007

WPF Calculator






In my last post I went through creating my first application in WPF and straight away I started thinking about creating a much more visually attractive application, so I decided to create another basic application, this time a simple calculator.

Using Orcas Beta 2 I was able to create a WPF form with 2 text boxes for user input, a combo box, button and a label for the result of the calculations.

I first started out with the combo box displaying text to signify different calculations (e.g. +, -, /,*) however as I was making the rest of the application found myself getting more confident and decided to add images to the combo box so that a user can select an image representing there choice. Implementing this was much easier that I first thought it would be and using only a couple of lines of code in the XAML file and altering some of the code behind file I was able to use the image combo box to select the type of calculation. The XAML for this is:


<ComboBox Margin="40,107.457,0,79.968" Width="120" Name="comboBox1" HorizontalAlignment="Left">

<Image Name="add" Source ="images\add.jpg" />
<Image Name="minus" Source ="images\minus.jpg" />
<Image Name="multiply" Source ="images\multiply.jpg" />
<Image Name="divide" Source ="images\divide.jpg" />

</ComboBox>




And in the code for this to find which of these images your user selected simply find the index of the items in the combo box.






All the images that I have used in the combo box were created in Expressin Design and Exported as jpg files. With creating the images myself I was able to directly match the hex color value for the rest of the application to this creating a unified feel to the program.



Overall I dont think it was a bad effort at all for an hours work, an experienced programmer would be able to create some amazing things with WPF can't wait till it hits the mainstream and all released products look this good.


No comments: