Search code examples
c#visual-studio-2012messagebox

VS2012 C# WPF MessageBox quick typing method error


I'm writing a program in Visual Studio 2012 C# using WPF where I like to use MessageBoxes for some quick debugging and would usually type

mb [tab][tab]

To get the following message box code to appear

global::System.Windows.Forms.MessageBox.Show("Test");

However, I get the error message

The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

The code

global::System.Windows.MessageBox.Show("Test");

Works perfectly though I'm getting sick and tired of having to remove .Forms every time I make a message box.

Is there anyway to change the

mb [tab][tab]

Behaviour to output

global::System.Windows.MessageBox.Show("Test");

instead of

global::System.Windows.Forms.MessageBox.Show("Test");

?

It's very painful

Also, what is the term for these (mb [tab][tab]) types of shortcuts?


Solution

  • That's code snippet. Why not create your own code snippet?

    Step 1: Copy this code to a new file and save it with .snippet extension.

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>wpfmbox</Title>
            <Shortcut>wpfmb</Shortcut>
            <Description>Code snippet for MessageBox.Show</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>string</ID>
                    <ToolTip>String to display</ToolTip>
                    <Default>"Test"</Default>
                </Literal>
                <Literal Editable="false">
                    <ID>SystemWindowsMessageBox</ID>
                    <Function>SimpleTypeName(global::System.Windows.MessageBox)</Function>
                </Literal>
            </Declarations>
            <Code Language="csharp">
        <![CDATA[$SystemWindowsMessageBox$.Show($string$);$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
    </CodeSnippets>
    

    Step 2: From the Tools -> Code Snippet Manager, choose import and find your .snippet file. Then as the location it is preferred to choose the My Code Snippets folder.

    Step 3: Somewhere in your code, use wpfmb[tab][tab].