Software Design Engineer
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|
| 28 | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Search
Navigation
Categories
Blogroll
|

Tuesday, November 18, 2008
Resetting the Selected Item of a Silverlight 2 ListBox Control
Silverlight version: 2.0.31005.0
If you are trying to reset the selected item of a ListBox control using either the SelectedItem or SelectedIndex properties, it will not work with the version of Silverlight stated above. From what I can glean in the Silverlight forums, this method did at one time work (previous release candidate maybe) and is also a known issue that is being addressed (for the current version).
In the meantime, you can adapt your application to use another existing control or custom control, or you could do something like the following:
Option 0: Reset the selected item or index outside of your SelectionChanged event handler. One way to do this is to use a DispatcherTimer. When the ListBox SelectionChanged event fires, start the timer. In the Tick handler, stop the timer and perform the reset (set SelectedItem to null or SelectedIndex to -1).
Drawbacks: this isn’t unexpected, but resetting the selected item is reflected by the ListBox UI, so the last selected ListBox item may not be as noticeable to the user. If you want to show the last selected item AND enable clicking on the same item multiple times in sequence, then see the option below.
Option 1: You can also reset the selected item without resorting to the use of an additional timer. Simply take advantage of the Dispatcher.BeginInvoke method like in the following code ('lb' refers to a ListBox instance):
Dispatcher.BeginInvoke(new Action(delegate() { lb.SelectedItem = null; }));
OR
Dispatcher.BeginInvoke(() => { lb.SelectedItem = null; });
Option 2: Create a custom list box class that inherits from ListBox – let’s call it CustomListBox. Override the GetContainerForItemOverride method to return a custom list box item class (CustomListBoxItem) rather than the default ListBoxItem instance. Create a new public event called CustomSelectionChanged which will serve as the replacement for SelectionChanged.
public class CustomListBox : ListBox
{
//fired every time a list box item is clicked
public event SelectionChangedEventHandler CustomSelectionChanged;
protected override DependencyObject GetContainerForItemOverride()
{
CustomListBoxItem item = new CustomListBoxItem(this);
if (base.ItemContainerStyle != null)
{
item.Style = base.ItemContainerStyle;
}
return item;
}
public void OnCustomSelectionChanged(CustomListBoxItem item)
{
List<CustomListBoxItem> addedItems =
new List<CustomListBoxItem>();
addedItems.Add(item);
SelectionChangedEventHandler handler = CustomSelectionChanged;
if (handler != null)
{
handler(this, new SelectionChangedEventArgs(
new List<CustomListBoxItem>(), addedItems));
}
}
}
In CustomListBoxItem, which should inherit from ListBoxItem, create a constructor that takes a CustomListBox instance as a parameter and stores is for later use. Finally, override the OnMouseLeftButtonDown method and fire the CustomSelectionChanged event of the containing CustomListBox.
public class CustomListBoxItem : ListBoxItem
{
CustomListBox _customListBoxContainer;
// Needed to avoid error in case of instantiation in XAML. If
// CustomListBoxItems are added in XAML directly,
// CustomListBox will act like a normal ListBox and only allow
// each item to be selected once.
public CustomListBoxItem()
{ }
public CustomListBoxItem(CustomListBox customListBox)
{
this._customListBoxContainer = customListBox;
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (this._customListBoxContainer != null)
{
this._customListBoxContainer.OnCustomSelectionChanged(this);
}
}
}
Download example: ResetListBoxExample.zip
11/18/2008 12:13:25 PM (Pacific Standard Time, UTC-08:00)
Shrewd Programming

Wednesday, November 29, 2006
Open folders in a command window quickly in Vista
There is a nice shortcut in Vista that allows you to open a command window with the path set to a specific folder at startup. This can be done by holding Shift and right-clicking on a folder in Explorer. When the context menu appears, you can select "Open Command Window Here". Alternatively, you could use the Application key + Shift followed by the 'W' key.

On a related note, you can no longer drag and drop a folder from Explorer into a command window in order to paste the path information.
11/29/2006 4:27:31 PM (Pacific Standard Time, UTC-08:00)
Compelling News

Thursday, June 22, 2006
Web time machine!
At SharpLogic Software, we have a wide variety of backgrounds as well as talents. Since I came aboard in early 2005, I have had the opportunity to interface with a number of different characters. One side-effect that happens after meeting and working with people who are truly passionate about technology and gifted with their work is that you realize just how much YOU have to learn. Of course, there are skills which are not easily learned - and are truly gifts some are born with. For me, and many others who say something like "I apologize for the drawing, I am not an artist..." during a presentation, no amount of practice will render me with the ability to create a beautiful Web page. I might be able to learn a thing or two about layout and read about user interface design, but I will still probably pick neon green text on a black background with an orange border. Luckily for us we have people on staff who effortlessly bridge the gap between artistic design and technical functionality.
The reason that I bring this topic up is because being around these Web geniuses has made me think back to years gone by when I learned a little HTML and for some reason thought it wise to create my own homepage. I jokingly thought to myself "I sure hope that nobody decided to archive that content from the Web past". Tonight I did a quick search for such a thing and came across http://archive.org which, among other things, allows you to search their archived index of some Web sites from the past. This includes domains that you may have owned at one point and are now in the hands of someone else. It is not a complete archive, but it sure is fun looking back at old content many Web developers may have thought was gone. It is also neat to simply see the evolution of a site over time. Take a spin and see if you can find any interesting archived pages.
Check out an example from Microsoft from October 1996 at http://web.archive.org/web/19961022175331/http://www.microsoft.com/
MSN.com from 1996, http://web.archive.org/web/19961025231757/http://msn.com/
Google.com from 1998, http://web.archive.org/web/19981202230410/http://www.google.com/
Only a few sites that I have tried are actually blocked from being searched. I do not know if this is an opt-out mechanism offered to site owners or something else entirely.
6/22/2006 11:18:50 PM (Pacific Standard Time, UTC-08:00)
Worldly Observations

Wednesday, September 28, 2005
Funniest Algorithm Names
So I was looking at the list of algorithms page on Wikipedia, located at http://en.wikipedia.org/wiki/List_of_algorithms, the other day when I noticed a few funny sounding ones:
Blum Blum Shub – a pseudorandom number generator (named after the creators)
Bogosort – sort that generates random permutations until it finds “the one”
Blowfish – type of block cipher
Doomsday – calculates the day of the week of a given date (apparently each year has a doomsday, the day of the week on which the last day of February falls)
Leave a comment if you know of others :)
9/28/2005 1:29:45 AM (Pacific Standard Time, UTC-08:00)
Worldly Observations
SharpLogic Summer BBQ
It has actually been a couple of months since we all got together for some hamburgers and it turns out that some pictures were taken, unbeknownst to me at the time, while we played basketball at SharpLogic Arena.
I am actually quite proud of the first picture because I am able to successfully demonstrate my remarkable ability of holding a basketball over my head. Notice that both Ezju and Chris are fascinated by my feat.

After a long awkward moment of holding the ball over my head I decided that I should move closer to the basket for a higher percentage shot. Closer = higher percentage right?

So I moved closer to the basket and boldly lifted the basketball over my head once again. Unfortunately, I quickly ran into another problem, that being the backboard. You see, the basket is read-only from this side. Thanks to Ezju for pointing that out to me.
The highlight of the day came when Ed showed off his force powers as he slowly moved a levitating basketball towards the hoop without lifting a finger. Notice that Ezju is there to help supervise the action once again.

9/28/2005 12:40:03 AM (Pacific Standard Time, UTC-08:00)
Compelling News

Tuesday, September 27, 2005
Amusing Exception Message
I was recently working with GDI+ drawing when I found the need to modify a Pen's Width property. I often use the Pens class as a matter of convenience so my first thought was to simply write the following:
Pens.Yellow.Width = 3;
However, that did not work too well as I was greeted with the exception "You may not change this Pen because it does not belong to you." When I read the message I immediately realized that I had made a mistake, but the wording of the exception message also made me laugh a little. It sounds like the Drawing namespace is a little kid that doesn't want me to play with its toys, although in that case it would probably just say "Mine!"

In case you are wondering, the Pens class houses a number of static properties representing a number of colors, including Yellow. The static Yellow property will create a new immutable Pen object with width 1 when you first use it and then store it in a hash table for future use. Future calls to Pens.Yellow will get the same Pen object that was previously stored in the hash table. This is done because GDI Pens are a limited resource that needs to be conserved.
9/27/2005 11:11:15 PM (Pacific Standard Time, UTC-08:00)
Shrewd Programming

Wednesday, July 27, 2005
Return Me.PortlandCodeCampExperience
Portland Code Camp v1.0 was unleashed July 23 and 24 and I am happy that I was able to attend. I went to a number of sessions covering the following topics:
• Visual Studio Tools for Office (VS 2005)
• Intercepting API Calls
• ASP.NET 2.0 Web Parts
• SQL 2005 Development
• .NET Windows Forms Tips & Tricks
• Extensible .NET Applications
• ASP.NET Tips & Tricks
• Forensic Development
• Versions Gone Wild
Proof I was there (not that anyone really cares):

Now admittedly some of the content was over my head, such as in the Intercepting API Calls session, but that does not mean that I did not take anything away from it. When security experts or DB administrators start talking about their specialties and glossing over many of the prerequisites, it is not hard for me to get a little lost. This session could have used more demos, but I understand that the content was in the nitpicky details which take a lot of time to actually show.
Tips and Tricks sessions are always nice because you can cram so much information into a single hour time slot. These types of sessions are good because they present seeds of information that you can take with you and use later. Details are not as necessary here, but general ideas on where to start with a given problem can be very valuable.
The Extensible .NET Application session was essentially a case study which focused on a specific solution. While it would be nice if the entire range of possible design choices could be covered in more detail, I realize that only so much can happen in one hour. It was nice to get into the details of the code and listen to some of the gotchas that were found during development of a particular solution.
Braindump for other Code Camp related thoughts: donuts, lots of donuts… Seattle to Portland traffic and the reverse = System.OverflowException… Subway for breakfast, lunch, and dinner… Portland has enough bridges…
7/27/2005 1:03:19 AM (Pacific Standard Time, UTC-08:00)
Compelling News

Thursday, July 21, 2005
Napoleon Dynamite and XXX Stole My Thunder
Have you ever felt like you had a good thing going before someone famous decided to give it an entirely new meaning altogether? Believe it or not, it has happened to me but I will not be appearing on Oprah anytime soon.
Here is the story. A few years ago I received a nice orange t-shirt from a family member as a gift, and it said something like “XXX Workwear” on it. So I happily wore it around for a few months, and then Mr. Diesel starred in a movie called XXX. Needless to say, everybody started to make comments when I wore the shirt, which isn’t bad or anything, but I felt the obligation to tell people, “I HAD THE SHIRT BEFORE XXX EXISTED!”
More recently I had a white custom t-shirt made that has black rings around the sleeves and collar and says “I Support Rabid Llamas” on the front. My design even used a nice red font to stand out. Now those of you familiar with the movie Napoleon Dynamite will know about the “Vote for Pedro” t-shirt. Guess what? I made the shirt before the movie was out! When I wear my shirt I am guessing that people think I am copying the movie or something, which is far from the truth.
I am kind of amazed that this situation has happened to me twice in the last few years, and even more surprised that it has been over a t-shirt. Oh well, let those darn movies ruin the meaning of my wardrobe. At least we have all learned something from the movie XXX – you can deflect a missile with a serving platter!
7/21/2005 12:18:04 AM (Pacific Standard Time, UTC-08:00)
Indiscriminate Complaints

Wednesday, July 20, 2005
Funniest .NET languages
What are the funniest .NET language names out there? Here are a few that tickled my funny bone. By the way, I ran them by my wife and she authenticated their funniness. While I do openly poke fun at the names, it is all in good fun. No harm came to the languages used in the making of this posting.
Boo
Common Larceny
Objective Caml
Hotdog
Tachy
TickleSharp
7/20/2005 11:30:01 PM (Pacific Standard Time, UTC-08:00)
Shrewd Programming

Tuesday, June 28, 2005
Visualization Tools for Computer Science Education
One of my favorite subjects within the realm of Computer Science topics is computer graphics and visualization. Part of the draw, especially early on in my school career, was the instant gratification that could be found in implementing graphics code. You get to directly see the results of your labor plus you can easily share it with others who do not understand the implementation details.
There are basically three types of learning methods – auditory, visual, and hands-on. Of course, most people use a combination of all three throughout their life. I am not sure if I am more hands-on or visual, but I am certain that I do not learn best through auditory means. Therefore, unless I am somewhat familiar with a topic to begin with, lectures seem like a waste of time to me, at least the prototypical theory-only college lectures. I take notes, but I have no chance of “getting it” until I sit down and have some hands-on time with the material.
I think that students could benefit greatly from tools that help visualize core computer science topics such as search algorithms, recursion, data structures, etc. Unfortunately, for whatever reason, my instructors did not know that these types of applications existed, and neither did I until recently. There are some very sophisticated scientific and data visualization tools out there, but I am currently just referring to fairly simple instructional tools with good interfaces.
Here is a link to a J# application for visualizing sorting algorithms, recursion, simple image viewing, and an oscilloscope simulation. It allows you to even test your own algorithm implementations and see how they perform (Visual J# .NET Product Team, Microsoft).
https://www.mainfunction.com/DotNetInAction/Technologies/display.aspx?ID=2710&TypeID=2
The next link has some Java applications that help visualize automata and formal languages (Susan Rodger, Duke University).
http://www.cs.duke.edu/~rodger/tools/tools.html
Here is a well written paper titled “Helping Learners Visualize and Comprehend Algorithms” by Hansen, Narayanan, and Schrimpsher.
http://imej.wfu.edu/articles/2000/1/02/index.asp
I am sure that there are more visualization tools out there. So are these tools being used at the high school or even undergraduate college level? Aside from the elected computer graphics classes that I took in college, I do not remember having an instructor utilize visualization software in any way.
6/28/2005 2:16:14 AM (Pacific Standard Time, UTC-08:00)
Worldly Observations