Tag Archive for .net

5 Cool WPF features for WinForms Devs

Today i have finally booked my exam for the MCTS 70-511. Booking the exam made me reflect and just how much i had discovered about WPF since starting to revise for the exam. When i started, i had only had around a year experience with WinForms and C# and around 0 weeks of WPF development.  So here are 10 things i have discovered for anyone else who might be still working with WinForms.

 

1. XAML, XAML, XAML!

With WinForms you have two choices, you can use the interface designer and drag and drop components into your form or you can create and configure your components in code. Which ever one of those you choose, it will chuck up some C# in a class somewhere. Then WPF comes along looking all pretty with its XAML code. XAML is very similar to HTML in the sense you have elements and a hierarchy. If you have worked with something like android development this kind of XML/HTML style interface design will be familiar.

Why its cool:  Its Cool because it gives you much more control and structure to your interface. More importantly it makes increasingly readable layout code.

Find out more at: XAML Overview (WPF)

 

2. Control Templates, Gray is so last year!

When working with WinForms, if you want to customize the way a control (e.g. a button) looks you are only really faced with changing simple properties like the foreground, background, font etc. WPF has changed all that, with control templates you can visually change the control completely. You can choose what your control will show and how it will do it, you can place other controls within your control, e.g. placing a checkbox within a button.

Why its Cool: No more boring gray controls, you can make controls look however you want and its pretty simple to do.

Find out more: Customizing the Appearance of an Existing Control

 

3. Data Binding, The best of WPF by far

It would be a terrible crime not to talk about Data Binding when talking about WPF. Once you start using Data Binding your wonder why you spend so long writing endless SQL statements in click events. The idea of Data binding is to take away a lot of the pain of updating your source when you make a change in your application. Data binding will with a little encouragement detect when a user changes a field in your UI and will then proceed to update that value within your object which will more then likely be in a collection which will also be notified of the change. This means instead of having code all over the place updating your source (for example SQL Update statements) you can just have simple update code within your object model (your class you use to create your Customer Object for example) that does all the magic.

Why its Cool: It will save you a fair chunk of time doing boring update code. I also find it forces you into using OO design which can only be a good thing.

Find out more: Data Binding Overview

 

4. ClickOnce, Deployment without the hassle.

I know what your thinking, “you can deploy WinForms applications with ClickOnce”. That is correct but i don’t think it gets the attention its due so i am giving it 5 minutes of fame. If you have ever had to deploy your application to more then a few people, you would of had to create an installer. Unless your a programming God you would properly have to send out a new version of your application to fix some bugs. This can be painful, sometimes users might not of got the memo, some users might be scared to death of Installers after they opened that questionable attachment in their emails. ClickOnce may not be very configurable but it does offer a massive time safer. A integrated Updating functionality. With ClickOnce you can set a minimum required Version, you can get the application to check for updates when the app is opened or when the user exits. You can even create check for updates functionality within your application.

Why its cool: It makes updating your user base much much easier. This will save you a lot of time. It can also automatically publish itself to your FTP or network location.

Find Out more: ClickOnce Deployment Overview

 

5. Resource Dictionaries, CSS for .NET

XAML allows you to create styles and brushes. So what? well resource dictionaries are a revolution in recycling. You can define all your styling information (what color your buttons background is, what size font to use) within a resource dictionary, you can then  reuse the resource dictionary not only within the same application but also if you place the resource dictionary in a separate Library you can then use those styles within other projects. This is pretty handy if you are likely to produce a number of applications that will share a consistent style as you only have to define the style once and all your applications will use the styling information. Resource dictionaries are also very versatile, you can set a style that will  automatically change every control of that type or you can give a resource a key and then reference that key within the controls you want to use the style.

Why its Cool: Its all about recycling, recycling saves time. what more do you want!

Find out more: Resources Overview

 

I hope that’s helped inspire those of you who are still using WinForms to move over to the WPF side.

C# Modifying a Application Settings Connection String

On the application i have currently been working on, i discovered a small issue. The connection string the application uses is stored as a Application setting. This means that its read-only. Its important to set the connection string as a ConnectionString type within the Application settings so that its secure. But what happens if the connection string changes? This isn’t so bad in a internal application as you can just rollout a new build, but what if each user is likely to have a different connection string.

To change the connection string, help (as usual) came from stack overflow Programmatically change connection string.

That was all fine and dandy but when i went to go and use my application setting by going:

Properties.Settings.Default.ConString;

It returned the old unchanged connection string. the simple way round it was instead of getting my connection string using the code above i had to do this:

System.Configuration.Configuration config2 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 pconnectionString = config2.ConnectionStrings.ConnectionStrings["ConString"].ToString();

Bingo! that worked!

 

 

WPF / C# notifcations pop up

There becomes a point in a lot of applications where you hit the notification wall. I have recently been working on an RSS Reader to practice some concepts that are covered in the MCTS (Windows Application Development 70-511) exam.  I got the application working with data bindings and all the other WPF Wizardry and decided to start using it. I currently use Outlook to read RSS Feeds so i thought it would be worth seeing if what i had created was any better.  I stumbled across a real issue for me. It didn’t notify me when it had updated. This isn’t really a big issue is it? Well it really was, because i have become very much hooked to applications telling me when something has happened for example, email, tweetdeck, MSN Messenger etc. Like below.

TweetDeck Notification window

A prime example of a notification window

 

Rather then give up with my application i thought why not add my own notifications to it! So here is a little guide on how to add a notification popup to your application.

 

the form

No massive surprise, but we start by creating a form or should i say window as its WPF. the window will be what is displayed when the notification happens.

Heres the one i made:

WPF Notification Popup

The design i can leave to your imagination, but there are a few properties within the window we need to set to make it look right. Here is the XAML snippet for window

<Window x:Class="RSSBuddy.notificationWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="notificationWindow"  ResizeMode="NoResize" ShowInTaskbar="False"
        Topmost="True" WindowStyle="None" HorizontalAlignment="Right"
        VerticalAlignment="Bottom">

 

So lets explain some of the things we are doing:

ResizeMode = NoResize

This is a pretty simple one, it just means the user wont be able to resize the notification window, which is fine because why would they want to?

ShowInTaskbar = False

Again, pretty simple, we don’t want this window to have an icon in the taskbar because its not really a window people will see for long.

Topmost = True

This property is pretty important to our notification window. the property forces the window right to the front of the screen in front of any open applications. If we didn’t set this to true, the notification window will end up getting lost behind other applications.

WindowStyle = none

This property means that we don’t have the border around the window or the close button and title. We set this to none so we can do our own title bar that fits better to the application.
HorizontalAlignment= Right & VerticalAlignment= Bottom

Both these properties are in charge of placing the window in the bottom right corner.

 Wait a minute…

If you now create your notification window and run it, something rather bizarre will happen. Your notification window will appear not in the very bottom right of the screen where you wanted it. This happens because it positions itself at the bottom right of its parent window NOT the screen. The way i overcome this was to do a bit of code tweaking before i opened the window:

  note = new notificationWindow(newitemcount);
  note.Top = main.ActualHeight - note.Height - 15;
  note.Left = main.ActualWidth - note.Width - 30;
  note.Show();

 

This code is what i use to open the notification window when something has happened. firstly we create the notification window object. Now to get over the problem of the misplacement. I go and manually set the notification windows Top and Left Properties. I set them by getting my parent screens ActualHeight (my parent screen is set to Maximise by default) and taking away the Height of my notification window and then and extra 15 for good measure. This results in the notification window appearing where it should!  the bottom right corner.

NOTE: If your parent screen is not Maximized you could use the following instead of the parent windows ActualHeight and ActualWidth (thanks to: this)

System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight