Trouble with the ListView SelectedIndexChanged event

- Monday, October 08, 2007

I've been messing with a System.Windows.Forms.ListView control lately. I have a list of items in a standard ListView control. If the user selects an item, I have some other controls to be shown. If the user deselects the item in the ListView I want these other controls to be hidden.

Now, whenever a user changes the selected item in the ListView control, the SelectedIndexChanged event is called twice: once for deselecting the item (SelectedItems.Count == 0) and once for selecting the new item. This causes the "other controls" to flicker: they first get hidden, then show up again.

Despite some existing solutions using a Timer (here) I analyzed the available events of a ListView. Click and MouseClick are useless since they only occur when the user clicks an item, not when deselecting. MouseUp looks promising since it also fires when the user clicked somewhere outside the items. I have not found a key stroke to deselect an item in a ListView, so MouseUp should be sufficient to check for deselection.

Using that I create a new ListViewEx derived from ListView with two need events: ItemsDeselected and ItemSelected. The originally SelectedIndexChanged event is left untouched.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Instyler.Controls
{
class ListViewEx : ListView
{
private bool _enableDeselectionEvent = false;

protected override void OnSelectedIndexChanged( EventArgs e )
{
_enableDeselectionEvent = ( SelectedItems.Count == 0 );

if ( SelectedItems.Count != 0 )
OnItemSelected( this, new EventArgs() );

base.OnSelectedIndexChanged( e );
}


protected override void OnMouseUp( MouseEventArgs e )
{
if(_enableDeselectionEvent)
OnItemsDeselected( this, new EventArgs() );

base.OnMouseUp( e );
}


#region Our new events

public delegate void ItemsDeselectedHandler( object sender, EventArgs e );

[Description("Event raised when items got deselected"), Category("Behavior")]
public event ItemsDeselectedHandler ItemsDeselected = null;
protected virtual void OnItemsDeselected( object sender, EventArgs e )
{
if ( ItemsDeselected != null )
ItemsDeselected( sender, e );
}


public delegate void ItemSelectedHandler( object sender, EventArgs e );
[Description("Event raised when a new item got selected."), Category("Behavior")]
public event ItemSelectedHandler ItemSelected = null;
protected virtual void OnItemSelected( object sender, EventArgs e )
{
if ( ItemSelected != null )
ItemSelected( sender, e );
}

#endregion
}
}

Feel free to use that code.

Labels:


Automatic shutdown if no client is available

- Saturday, May 12, 2007

I have a home file server that shall not run 24/7. So I enabled Wake-On-Lan which was working great. What I missed was some kind of Shutdown-On-Idle. After a little thought I came up with the idea that the server may shutdown when all possible clients are no longer available. So I wrote a little DOS batch script that pings all clients and shuts down the server when there is no one available.

Here's the script:

@echo off

CALL :ping client1
CALL :ping client2

ECHO SHUTTING DOWN IN 30 SECONDS
psshutdown -s -f -t 30
GOTO :EOF

:ping
ECHO Pinging %1 ...
PING -n 1 -w 3000 %1 |find "Reply"
IF errorlevel 1 GOTO :EOF
ECHO %1 is online. Not shutting down...
EXIT

Just save it as a .bat file and schedule it to run every hour or so. I used psshutdown.exe instead of the standard windows shutdown.exe because that one cause an error on my server.

Labels:


Beyond Java

- Friday, December 22, 2006

A friend of mine (Norbert) has recommended to me to read the book "Beyond Java" (Amazon).

The book is about why Java - better: J2EE is not a good web-development platform anymore. And why Ruby On Rails is so much better. Now I must say the author (Bruce Tate) makes some good points about how bad using J2EE with Spring, JSF and whatsoever has become. But as one of the Amazon reviewers calls it the book is "narrowly-focussed". The is really just about web-development and nothing else. And for that, RoR is the authors uber-solution. I'm not so sure...

But... I will give RoR a try and so I already got my next book to concentrate on: "Ruby On Rails: Up and Running" (Amazon), also from Bruce Tate.

Labels:


Mac OS X from a Software Architect's view

- Sunday, May 28, 2006

I'm now a Mac user. And I really like it.

As a Software Architect I would like to shout out to everyone who hasn't done it yet: If you design software and especially user interfaces, get familiar with the Mac OS for at least a day. You will throw away all the old concepts you had.

Here's what I learned so far:
  • Windows users tend to need to customize everything. Well, only Linux-users really can. And Mac users don't want to. They like it the way it is. Somewhere I read "why do you want to change everything if it is just working fine?" So what do Preferences dialogs look like on a Mac: as empty as possible and just the settings you really need to take care of.
  • Dialogs on a Mac do not have OK, Cancel or Apply buttons. Instead the settings are applied real time. Undo capability is not available, so your application should take care of via a menu entry or something. A Live Preview feature like in Office 2007 is becoming very popular and reduces unnecessary clicks enormously. Message Boxes should not have Yes/No. buttons. Nobody likes reading message box texts, so make the buttons say something like "Don't save" or "Replace destination file". Vista will do a much better job here.
  • Add 3D, fading, glossy, reflection effects where you can. This is the big thing about the Mac. You have seen Front Row. You know the magnify effects of the Dock. You have seen Steve Jobs presentations created with Keynote. That all just looks amazing. Copy it!
Conclusion: I have been testing Vista Beta 2 and Mac OS X in the last two weeks. The Wooow effect on the Mac was much higher.

Labels: