Knowledge found and lost while working with Microsoft Dynamics CRM
RSS icon Home icon
  • .NET: Full Proof Method for Setting a Selected List Item

    Posted on March 24th, 2006 mitch Print Print No comments

    Well, I'm not sure it's exactly full proof, but it will keep you from making a stupid mistake in your application's functionality.

    There are several ways to select a list item when you are loading a page or a form. The most common method is to just set the SelectedItem property to be the text that you would like to see defaulted into the dropdown or listbox, like so:

    cboServerSchedule.SelectedItem = sMyString;

    This works find and dandy as long as the value that you are assigning to SelectedItem exactly matches your variable or value. And when I say exactly, I mean just that: exactly.

    I ran into a situation this week where it was possible for the user to edit, outside of the application, the value that I would be using to set SelectedItem of my DropDown. This would cause the application to basically set the default value for the DropDown. Since I had no control over what the user could do, and the setting the application was writing to the registry was case-sensitive, it presented me with a problem.

    That was when I remembered that a very, very, long time ago, I had already solved this problem with just a couple of extra lines of code:

    iFindStr = cboClientSchedule.FindString(sMyString);

    if (iFindStr != -1)
       cboClientSchedule.SelectedIndex = iFindStr;

    Using the FindString function for the DropDown, you are able to perform a case-insensitive search for a selected value. If the value is found within the Items collection, FindString returns that index; otherwise, it returns a -1.

    I simply check the return value and if it's not -1, I assign the SelectedIndex ( instead of SelectedItem ) to that value. The final key to this solution was to set the items collection of my DropDown to be valid values for the application that would later be reading the value from the Registry.

    Development
    1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    1,382 views
  • Fighting fires, and such

    Posted on March 24th, 2006 mitch Print Print No comments

    I learned several important things this morning that I felt I should share with the world at large.  Listed in no particular order:

    1. A $25 microwave doesn't understand that "Reheat" for a Cinnamon Roll means 20 seconds, not 20 minutes.
    2. Sugar burns.
    3. Burning sugar produces yellow smoke.
    4. I don't know if yellow sugar smoke is toxic, but it stinks like heck.
    5. You can't make smoke disappear by spraying "room freshener," so don't try.  Imagine burnt sugar and a burning hospital, and you'll get a visual on the smell.
    6. The fire alarms on the 9th floor of a certain gold-plated building in North Dallas do not consider burning sugar to be a threat.
    7. The fire department will not show up unless a fire alarm goes off.
    8. Burning sugar smells.

     

    Meanderings
    1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    1,365 views