Using the PTVS for a Silverlight Project

My book, “Professional IronPython” doesn’t discuss how to work with technologies such as Silverlight, so I thought I’d see how hard it would be given that PTVS comes with support for a Silverlight project. This post adds to my previous post about PTVS. Of course, you’ll need to have PTVS installed on your system before you begin working with this example.

To begin this example, you start Visual Studio as normal and choose File | New to create a new project. The New Project dialog box should contain an entry for an IronPython Silverlight Web Page template as shown here. If you don’t see this template, then you probably don’t have IronPython 2.7 installed on your system and can use the instructions found in the IronPython 2.7 and PTVS blog entry to install it.

Silverlight01

Give your application a name (the example uses the default of SilverlightPage1) and click OK. You’ll see a new project appear like the one shown here:

Silverlight02

What I find interesting is that there is a designer provided for this template, but none provided for the Windows Forms application. I tried the Designer out and it works well. There isn’t any difference in creating a Silverlight application using this template than any other language I’ve tried. However, the list of controls you can access is limited. Here’s what the default Toolbox entries look like:

Silverlight03

I tried adding a control to the list, just to see what would happen. When you open the Choose Toolbox Items dialog box, you’ll see that all of the normal controls are already selectedthey simply don’t appear in the Toolbox. I even tried checking a few controls on the Silverlight-specific tab without any result. So, my thought is that you’re limited to the controls show. If you know otherwise, please contact me at John@JohnMuellerBooks.com. Even so, you can create a business application without any problem using the controls provided.

The Silverlight front end for your project uses HTML-like statements, as you might expect. You create linkage between the Web page and your IronPython code behind just as you would any other language. Here’s what the default sample code looks like:

<script type="text/python" src="SilverlightPage1.py"></script>

The code behind looks just like any other IronPython code you might have created in the past. Here’s the default example code:

def SayHello(s,e):
    window.Alert("Hello, World!")
  
document.Button1.events.onclick += SayHello

In short, you don’t have to worry about any odd programming techniques to make this example work. I was frankly surprised at just how nicely things do work with this template. There are a few oddities you’ll encounter. The first is that when you press F5 to start debugging your application, the IDE starts the Chiron development server, rather than the default. That’s not too odd, but it’s something you should know about.

The second is that the application doesn’t automatically appear in your browser. So, you’re waiting for something to happen and nothing does. Instead, you must right click the server entry in the Notification Area and choose Open in Web Browser from the context menu. At this point, I received a directory listing like the one shown here, rather than the application I expected.

Silverlight04

Clicking SilverlightPage1.html did display the application. In fact, it displayed a lot more. I also received an IronPython Console I could use for debugging tasks, as shown here:

Silverlight05

As shown in the figure, you can minimize the IronPython Console when not needed, so you can see your application better. Overall, I was very impressed with the Silverlight template offering and think it’s a great addition to the IronPython developer’s toolbox. Be sure to look for additional PTVS entries in my blog as time permits and to let me know your thoughts about it!

Using the PTVS for a Windows Forms Project

Chapter 8 of Professional IronPython demonstrates how to create a Windows Forms application using IronPython 2.6. As mentioned in my previous post about IronPython, IronPython 2.7 provides something new in the way of integration, PTVS. You’ve already discovered the means to create a console application using PTVS in my second post on this topic. This post goes a step further and demonstrates how to create a Windows Forms application using the same approach you’d normally use with any other .NET language, such as Visual Basic .NET or C#. I assume you already have PTVS installed on your system.

Of course, you begin by opening Visual Studio 2010 as normal. The New Project dialog box should contain a WinForms Application template in the IronPython folder. If you don’t see the correct template, then you probably don’t have IronPython 2.7 installed with Visual Studio 2010 (see my previous post for details). Call this project PythonApplication2 as shown here:

WinForm01

Click OK and Visual Studio creates the solution for you. The next step is to recreate the display shown in Listing 8-1 of the book. I had truly hoped that PTVS would provide Designer support, but it doesn’t. Here’s what you get as output from the template:

WinForm02

Now, it is disappointing that you don’t get a Designer, but the template does provide you with some helpful information and it’s better than what you started with in Chapter 8. The updated code is easier to read and requires that you perform less typing than shown in Figure 8-1. Here’s the new version of Listing 8-1.

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
 
from System.Windows.Forms import *
from System.Drawing import *
 
class MyForm(Form):
    def __init__(self):
         
        # Add a tooltip control.
        Tips = ToolTip()
 
        # Configure btnOK
        btnOK = Button()
        btnOK.Text = "&OK"
        btnOK.Location = Point(263, 13)
        Tips.SetToolTip(btnOK, 'Displays an interesting message.')
 
        # Configure btnCancel
        btnCancel = Button()
        btnCancel.Text = "&Cancel"
        btnCancel.Location = Point(263, 43)
        Tips.SetToolTip(btnCancel, 'Cancels the application.')
 
        # Configure lblMessage
        lblMessage = Label()
        lblMessage.Text = 'This is a sample label.'
        lblMessage.Location = Point(13, 13)
        lblMessage.Size = Size(120, 13)
 
        # Configure the form.
        self.ClientSize = Size(350, 200)
        self.Text = 'Simple Python Windows Forms Example'
        self.AcceptButton = btnOK
        self.CancelButton = btnCancel
 
        # Add the controls to the form.
        self.Controls.Add(btnOK)
        self.Controls.Add(btnCancel)
        self.Controls.Add(lblMessage)
 
form = MyForm()
Application.Run(form)

This code works precisely the same as the code described in Listing 8-1. You still see the same dialog box as output as shown here:

WinForm03

It isn’t anything fancy, but the example does demonstrate that you can create a Windows Forms application using straightforward code and less typing than the example in Listing 8-1. Of course, this form requires event handlers, just like the ones used for the example in Chapter 8. Of course, you begin by creating the required linkage after you initialize the form as shown here:

# Add the event handler linkage.
btnOK.Click += btnOK_Click
btnCancel.Click += btnCancel_Click

After which, you add the event handler code shown here:

# Define the event handlers.
def btnOK_Click(*args):
     
    # Display a message showing we arrived.
    MessageBox.Show('Hello!')
 
def btnCancel_Click(*args):
     
    # Close the application.
    form.Close()

At this point, you have the same first example shown in Chapter 8. Of course, Chapter 8 contains a number of other examples that demonstrate other techniques; all of which you can easily move to the new environment provided by PTVS. Here’s the source code for the entire example: Program.py. So, what do you think of the new Windows Forms project support provided by PTVS? Let me know at John@JohnMuellerBooks.com.

IronPython 2.7 and PTVS

The number of new features for Professional IronPython keep increasing as I discover more about IronPython updates from the community. You may have read my PTVS post the other day and wondered just how it was that the PTVS installer didn’t detect my IronPython 2.6.2 setup. It turns out that the PTVS installation currently has some problems. The best course of action is to uninstall PTVS and your IronPython 2.6.2 installation as well. (Make sure you reboot your system after you uninstall the old products.) What you really need is to download and install IronPython 2.7 Release Candidate 2.

Start the installation after you complete the download. You’ll see the normal licensing agreement and so on. However, the important feature to observe are the options on the Custom Setup page shown here:

IP2702

Notice the Tools for Visual Studio entry. Installing this feature ensures that you get all four of the templates in Visual Studio 2010. Once you complete the installation, you can check it out in Visual Studio. Here’s the updated New Project dialog box from my copy of Visual Studio 2010 Ultimate:

IP2701

So, now I have a number of new toys to play with. Future blog entries will describe what I find when I try the other templates out. In the meantime, I’d love to hear about your experiences with PTVS at John@JohnMuellerBooks.com.

 

Using the PTVS for a Simple Project

Well, I installed the PTVS I mentioned in yesterday’s post on my system and gave it a whirl. I must admit to being a little underwhelmed after the hype on the CodePlex site. However, the download does help and if you want to use Visual Studio for your development platform you should get it.  The process for creating a project is certainly easier than then one described in Chapter 2 of my book (although, that process still works fine for existing projects). The first thing you’ll notice after installation is that you get a new installed template as shown here.

PTVS01

As you can see, you do get all of the functionality that you’d normally get in a Visual Studio project, including the ability to add your application to source control, so this is a good start. After you configure your solution, the IDE creates it for you and you see a single file with some test code in it like this.

PTVS02

Don’t get the idea that you can simply click Start Debugging at this point and see something interesting. Before you can do anything, you have to configure the interpreter. Choose Tools > Options to display the Options dialog box. In the Options dialog box, select the Python\Interpreter Options folder. Here’s what the options look like; I’ve already configured mine for use on a 64-bit Windows 7 system.

PTVS03

I found that IntelliSense worked great. For example, when I typed raw_input(, I automatically received the proper help as shown here.

PTVS04

I played around with the IDE quite a lot more and was impressed with what the IDE does now that it didn’t do in the past. Of course, I’m going to have to play a lot more before I feel comfortable with everything this add-in can do.

So, where was the disappointment factor? Well, the first issue is that I was supposed to receive a total of four template types with IronPython according to the ReadMe.html file that comes with the product. I’m hoping there is a simple fix for this issue because I’d really like to tell you about the other templates that PTVS supports. The second issue is that the IDE didn’t automatically recognize my interpreter as it should. I’m assuming this is the reason why I didn’t receive the additional template. I’ll report back about these issues and show you more about PTVS as time permits. In the meantime, let me know your thoughts about PTVS at John@JohnMuellerBooks.com.

 

IronPython Finally Has Visual Studio Support

Months ago when I wrote Professional IronPython, I had to show you all kinds of workarounds for seemingly simple problems because the Visual Studio IDE didn’t provide the support required to do things like create an IronPython project.  For example, in Chapter 8 I have to show you how to create a Windows Forms application without using the visual designer.  That’s right, you need to write all of the component code manually, rather than rely on the GUI.  Microsoft’s decision not to support IronPython and IronRuby any longer seemed to put a nail in a great product’s coffin and I thought that perhaps the days of IronPython were numbered.

Fortunately, I was wrong. Microsoft has finally decided to release a beta add-in for Visual Studio 2010 that provides IDE support for both CPython and IronPython called Python Tools for Visual Studio (PTVS). You’re not getting any sort of new functionality from the language perspective.  The add-on relies on the underlying language features to perform its work.  The benefit to using this add-in is that it allows you to use the IDE to perform tasks such as creating an application using a template, rather than coding everything by hand.

Of course, the add-in provides far more functionality than simply creating projects.  The fact that I now get IntelliSense support is amazing.  You don’t know how helpful an IDE feature is until you try to write code without it.  Over the years, I’ve become somewhat addicted to IntelliSense because it helps me “remember” what it is that I want to do next.  Otherwise, I have to sit there and think about how things are supposed to go together; not always an easy task when you regularly work with multiple languages.

The add-in must be striking a chord with everyone.  It was only released on the 7th and there have already been 1,380 downloads (as of yesterday when I downloaded my copy).  If you program with IronPython and you often use IronPython to overcome procedural language limitations, this is a must have add-in for Visual Studio.

I’ll be working with this add-in over the coming weeks and will report back on what I find.  In the meantime, I’d love to hear your input on it at John@JohnMuellerBooks.com.

Working with Low Level Code

Working with low level code is becoming less necessary as Microsoft continues to improve the .NET Framework, but you sometimes still need to resort to direct Win32 API access using P/Invoke.  My book, “.NET Framework Solutions: In Search of the Lost Win32 API,” is a great place to learn about P/Invoke and the significant number of ways you can use it to access Windows features that Microsoft hasn’t made available in the .NET Framework yet.  For example, you’ll find a thorough discussion of the Windows messaging system in Chapter 4.  However, the discussion is a bit lengthy because there is so much you can do with the Windows messaging system.

One of the questions I get asked quite often is whether there is a quick start sort of guide I can recommend for working with the Windows messaging system.  With that in mind, I wrote a series of four DevSource articles some time ago.  Here’s the complete article list:


These four articles provide quite a bit of information about Windows messages that you might not know from a .NET perspective.  Using these techniques can save you considerable time, especially when you need to interact with other applications.  In fact, the final article reveals secrets you can use to interact with applications when you don’t have the source code; a significant problem for most developers.  So, how do you use P/Invoke?  Have you had to resort to P/Invoke to work with Windows 7 features that haven’t been added to the .NET Framework yet?  Let me know at John@JohnMuellerBooks.com.

 

Using the FindStr Utility to Locate Errors in Visual Studio

There are cases where the error message that you obtain from Visual Studio is less than helpful. Let’s begin the scenario by saying a coworker wants you to test some code. According to the code worker, the application works fine, but when you try it on your system, you get an odd sort of error message like the one shown here:

Error: Could not find a part of the path ‘C:\MyProj\Projects\TestMe\TestMe\Properties\MyManifest.xml’.

Here’s the frustrating part. You look in Solution Explorer and MyManifest.xml is in plain sight. So, now you start to wonder what’s going on because the IDE should be able to find the file. Here’s the key for this particular example. Instead of putting the project in C:\MyProj, you placed it in C:\MyFriendsProj. In this case, you don’t have a C:\MyProj folder on your machine.  So, when the IDE looked for MyManifest.xml in that particular folder, it couldn’t find it and displayed an error.  What to do?  You could search for this problem all day long and never find it. Often, the problem is one where the path has been hard coded into one or more of the Visual Studio support files.

The answer is to use the FindStr utility at the command line to find files that have the MyProj folder hard coded in them.  You open a command prompt and type FindStr /m /s /C:”MyProj” *.* and press Enter (where the /m command line switch tells FindStr to supply only filenames, the /s command line switch searches subdirectories, and the /C: command line switch performs a literal search).  Here’s some typical output.

Projects\TestMe\Bin\Debug\TestMe.dll
Projects\TestMe\Bin\Debug\TestMe.pdb
Projects\TestMe\obj\Debug\App.g.cs
Projects\TestMe\obj\Debug\App.g.i.cs
Projects\TestMe\obj\Debug\DesignTimeResolveAssemblyReferences.cache
Projects\TestMe\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
Projects\TestMe\obj\Debug\Form1.g.cs
Projects\TestMe\obj\Debug\Form1.g.i.cs
Projects\TestMe\obj\Debug\TestMe.csproj.FileListAbsolute.txt
Projects\TestMe\obj\Debug\TestMe.dll
Projects\TestMe\obj\Debug\TestMe.pdb
Projects\TestMe\obj\Debug\XapCacheFile.xml
Projects\TestMe.suo

All of these files have the MyProj folder hard coded in them.  By deleting these files and rebuilding the project, you can get rid of the error.  You can’t use the Visual Studio search features to find this problem because Visual Studio doesn’t look inside all of the files that it should—it only looks in source code files for the most part and only the source code files that are actually part of your project.  FindStr looks inside every file, no matter what its purpose might be.

What unique uses have you found for FindStr in managing your application development? Let me know at John@JohnMuellerBooks.com.