Friday, August 27, 2010

Quickly change Themes in Visual Studio 2008

I like dark backgrounds when I code; my co-workers don’t. I want to be able to change the screen back and forth from Darkness and Light. So I created a couple of toolbar buttons to switch themes (don’t use the tool until I say you can do so).

  1. I save my current theme (Tools => Import and Export Settings… => Export … => Next => Next => "C:\VS_Themes\Darkness.vssettings" => Finish).
  2. Create a standard theme (Tools => Import and Export Settings… => Reset all Settings).
  3. Save the standard theme (same steps as 1 but call it "C:\VS_Themes\Default.vssettings")
  4. Open Macros IDE (((Alt+F11) || (Tools => Macros => Macros IDE …))
  5. Create a module (Right Click on MyMacros => Add => Add Module => Name it "LoadThemes")
  6. Type the following VB.NET code into the module:
  7. Public Module LoadThemes 
        Public Sub SetThemeToDarkness()
            LoadThemeByFilename("C:\VS_Themes\Darkness.vssettings")
        End Sub<
        Public Sub SetThemeToDefault()
            LoadThemeByFilename("C:\VS_Themes\Default.vssettings")
        End Sub<
        Private Sub LoadThemeByFilename(ByVal fileName As String)
            DTE.ExecuteCommand("Tools.ImportandExportSettings", "/import:""" &
                fileName & """")
        End Sub
    End Module
    
  8. And I hooked them to buttons on my tookbar (Tools => Customize => New… "Theme" => Commands => Select Macros => Drag your macros onto the new Toolbar).
  9. Resave the Darkness Theme (The toolbar is part of the theme, otherwise when you use the tool bar to switch themes, you will lose the toolbar)
  10. Do the same thing for the Default Theme.

Now you are set.

Sunday, July 18, 2010

Avoid Duplicate Detail Forms in MDI programs

In a MDI Windows Forms/CRUD application, I don't want to display the same Detail form more than once at the same time. What if the user saves the address on one form and the email on another? Even if you can trust the user to do the right thing, it just feels icky to have more than 1 form for "John Smith" up at the same time.

It is less dangerous to have duplicate list forms, but I still don't like to gunk up the application with extra forms hanging around.

This code is a modernization of some code I used a couple of years ago. I am using Generics and Extension methods to reduce the amount of code I used in the old .NET 2.0 code!

using System.Windows.Forms;
namespace AvoidDupeForms
{
    /// <summary>
    /// A Detail Form is a form that can display a single record.
    /// In this example, there can me more than 1 of these loaded
    /// at once, as long as a record is only loaded once.
    /// </summary>
    public interface IDetailForm
    {
        /// <summary>
        /// Get the Id loaded in this form
        /// </summary>
        int Id { get; }
        /// <summary>
        /// Set the id for the form to load
        /// </summary>
        /// <param name="id">Id # to be loaded</param>
        void SetId(int id);
    }
    public static class FormsUtil
    {
        /// <summary>
        /// Reveal a form, load a new one if one doesn't yet exist 
        /// in the context of this MDI 
        /// </summary>
        /// <typeparam name="T">The type of the form to be revealed</typeparam>
        /// <param name="thisForm">
        /// The calling form (used to get MdiParent to be searched)
        /// </param>
        /// <example>
        /// <![CDATA[
        /// // As an extention method
        /// this.RevealForm<ListForm>();
        /// // The oldfashioned way
        /// FormsUtil.RevealForm<ListForm>(this);
        /// ]]>
        /// </example>
        public static void RevealForm<T>(this Form thisForm) 
            where T : Form, new()
        {
            Form MdiParent = thisForm.GetMdiParent();
            if (MdiParent != null)
            {
                foreach (Form frm in MdiParent.MdiChildren)
                {
                    if (frm.GetType() == typeof(T))
                    {
                        frm.BringToFront();
                        return;
                    }
                }
                T flf = new T();
                flf.MdiParent = MdiParent;
                flf.Show();
            }
        }
        /// <summary>
        /// Reveal a form with a given detailId, load a new one if 
        /// one doesn't yet exist in the context of this MDI 
        /// NOTE: > 1 instance of T can exist, but only
        /// 1 fore each IDetailForm.Id
        /// </summary>
        /// <typeparam name="T">The type of the form to be revealed</typeparam>
        /// <param name="thisForm">
        /// The calling form (used to get MdiParent to be searched)
        /// </param>
        /// <param name="detailId">The ID of the Detail record to be displayed</param>
        /// <example>
        /// <![CDATA[
        /// // As an extention method
        /// this.RevealDetailForm<DetailForm>(42);
        /// // The oldfashioned way
        /// FormsUtil.RevealDetailForm<DetailForm>(this, 42);
        /// ]]>
        /// </example>
        public static void RevealDetailForm<T>(this Form thisForm, int detailId) 
            where T : Form, IDetailForm, new()
        {
            Form MdiParent = thisForm.GetMdiParent();
            if (MdiParent != null)
            {
                foreach (Form frm in MdiParent.MdiChildren)
                {
                    if (frm.GetType() == typeof(T))
                    {
                        T saFrm = (T)frm;
                        if (saFrm.Id == detailId)
                        {
                            saFrm.BringToFront();
                            return;
                        }
                    }
                }
                T modify = new T();
                modify.MdiParent = MdiParent;
                modify.SetId(detailId);
                modify.Show();
            }
        }
        /// <summary>
        /// Gets the MdiParent for a given form
        /// (it could be self)
        /// </summary>
        /// <param name="thisForm">
        /// The calling form (used to get MdiParent to be searched)
        /// </param>
        /// <returns>
        /// The MdiParent of thisForm, or null if thisForm has no parent
        /// </returns>
        /// <example>
        /// <![CDATA[
        /// // As an extention method
        /// Form MdiParent = thisForm.getMdiContainer();
        /// // The oldfashioned way
        /// Form MdiParent = FormsUtil.getMdiContainer(thisForm);
        /// ]]>
        /// </example>
        public static Form GetMdiParent(this Form thisForm)
        {
            if (thisForm.IsMdiContainer)
                return thisForm;
            if (thisForm.IsMdiChild)
                return thisForm.MdiParent;
            return null;
        }
    }
}

Saturday, June 26, 2010

Back in Time

I took this picture this year on Indiana Street in Spokane.


 
Posted by Picasa

Saturday, June 05, 2010

Unscientific polling in the internet

I see the following tweet:

@ChrisLove: Do You Prefer a Stick Shift or Automatic? 
[Stick Shift: http://twp.li/6lbj || Automatic: http://twp.li/p8y4 ]
#twittpoll

(I really like the way he used “||” instead of “or”.) Being a stick shift enthusiast (ok weirdo), I clicked on http://twp.li/6lbj without thinking about it; when the page loaded I saw that the stick shift was beating automatic 80 to 20. This doesn’t match what I see in the world.

The problem is that people who prefer stick care deeply, but people who prefer automatic don’t feel strongly about their preference (unless they are forced to drive a stick).

This example is probably harmless. However, both the Republicans and Democrats in the US are using this kind of polling to push their point of view.

Monday, May 17, 2010

Portland Code Camp and Me

I will be presenting Introduction to Linq and Linq2DataSets an the Portland Code Camp from 9:00 to 10:15 AM on Saturday, May 22, 2010 (Session 0x01) in Room 13.

Thursday, April 08, 2010

Tuesday, March 30, 2010

My Boise Code Camp Presentation Notes

If you are looking for my Linq2DataSets presentation from Saturday's code camp, I am in the progress of getting them ready and finding a place to post item.