Can Of Code

Tag Archives: .net

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

 

Posted in c#, Software, WPF | Tagged , , , | 3 Comments

C# Quick Tip 1 – The Using statement

If you have ever dealt with C# enough to hook up to a database and use a DataReader you would of properly seen a Exception along the lines of “There is already an open DataReader associated with…” (unless your some kind of programming god!). You would get this exception  because you had forgot to close a previous DataReader somewhere, annoying right?

Well i had this problem a little while ago but it was happening because an exception was getting thrown which meant it skipped right past my .Close() call. It was at this point i found out about the joyous using statement.

So Here it is:

using (SqlDataReader reader = com.ExecuteReader())
{
       // Some wizzy Database reading
}

Its the same as:

SqlDataReader reader = com.ExecuteReader();

 // Some wizzy Database reading

reader.Close();

But… In the second example, if the code is within a Try..Catch and the wizzy database reading  code throws an exception. it will miss the .Close() statement and then you’ve got  to go and find out why.  The first example will automatically close the reader as soon as it leaves the block! which save you the pain! and i think it looks much cleaner then having .Close() statements everywhere.

If you enjoy reading the MSDN site here is a link to the using statements page

Posted in c#, Software | Tagged , , , , | Leave a comment

WPF – How To Create Image Rollover Buttons

When i first decided to write this blog entry i took a rather angry tone. I was trying to achieve a image rollover effect that is common place in the web world. But i was failing badly in the world of WPF. As hard as i tried googling, it seamed that nobody knew how to do it, or if they did they didn’t take the time to let the rest of us know.

A few weeks after  i decided to have a second attempt and what do you know, i found it pretty simple. So here is how to create WPF Rollover image buttons.

The way i finally achieved the rollover images is pretty simple, i create a ControlTemplate then add a Trigger  to change the background when the user moves the mouse over the button.

Heres the sample code:

<Window.Resources>
   <ImageBrush x:Key="RO1"
             ImageSource="/HTMLRolloverWPF;component/Images/4.ico" />
   <ImageBrush x:Key="RO2"
             ImageSource="/HTMLRolloverWPF;component/Images/3.ico" />

        <ControlTemplate TargetType="Button" x:Key="imageRoll">
            <Grid Name="gridy" Background="{StaticResource RO1}">
                <ContentPresenter  />
            </Grid>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="gridy" Property="Background"
                                                Value="{StaticResource RO2}" />
                </Trigger>
            </ControlTemplate.Triggers>

        </ControlTemplate>
</Window.Resources>
<Grid>
   <Button Content="Button"  Template="{StaticResource imageRoll}" />
</Grid>

The first part of that XAML code is where I create a ControlTemplate. You can place the ControlTemplate in
which ever Resources collection that is most relevant. I have also set the target type and given the whole thing a key.

The Next chunk of code is where we add a Grid to the ControlTempalte. We will use the grid to display the background. We set the background of the grid to a imagebrush resource we defined earlier in the resource collection.

Now for the bit that does all the work, the Trigger!  We set a trigger to activate when the “IsMouseOver” Property is True. Then we use a setter to change the background of the grid to our rollover ImageBrush which in the example is called RO2.

Its as simple as that!  If you don’t want to do a image rollover you can just change the background to a different color which is just as easy.

Posted in Software, WPF | Tagged , , , | 2 Comments