Cognitive Mileage

Archive for the ‘Iron Python Studio’ Category

This was recently a problem for me when I wanted to use Python 2.5’s string Templates and random number generation with Iron Python Studio but couldn’t. Here’s how you enable the libraries in Iron Python Studio (IPS):

1. Install the version of Python (CPython) you want to use.
2. Direct IPS to the Python directory you are using by inserting the following lines into the beginning of the .py file you wish to use the modules in:

import sys
sys.path.append("C:\\Python25\\Lib")

For your own work, just change “”C:\\Python25\\Lib” to point to the Lib folder of whatever version of Python (in whatever location) you want to use.

One of the number one problems for someone trying to implement syntax highlighting on a .NET platform is that when updating the RichTextBox to have the updated highlights, there is flickering. You have to highlight and change every keyword every time the user enters a new character (the TextChanged event).

There are two common methods. The first is to simply repaste all of the code into the RichTextBox and change the text formatting as you paste. The second is to highlight all the pieces of code and color or uncolor them as needed. Both cause undesirable flicker.

Stopping the RichTextBox Window Update without Win32
The flicker comes because the RichTextBox is updating every time you highlight a new word. The only solutions I’ve seen people offer to stop this is to DllImport Win32 libraries and do a LockWindowUpdate on the RichTextBox or send it a WM_SETREDRAW message.

Most people, upon discovering it and being given no alternative, find that solution disappointing. There should be a purely .NET solution, they say.

I’ve found one. I had to: I’m using IronPython Studio and, AFIAK, it can’t do DllImport.

The Solution
This is written in Python but can easily be translated to C# or VB.

def _rTextBox_TextChanged(self, sender, e):
    # Create new (invisible) RichTextBox for processing
    rt = RichTextBox()
    # Make sure the font is the same
    rt.Font = self._rTextBox.Font
    # But only paste unformatted Text
    # to erase formatting on non-keywords
    rt.Text = self._rTextBox.Text
    # Save cursor position
    user_pos = self._rTextBox.SelectionStart

    # Do text highlighting on invisible RichTextBox
    …

    # Now paste formatted text back into original (visible) RichTextBox
    self._rTextBox.Rtf = rt.Rtf
    # Reset cursor position
    self._rTextBox.SelectionStart = user_pos

The basic idea is that you create your own (invisible) RichTextBox within the code, copy the Text (unformatted text) from the visible RichTextBox into it, highlight the keywords, then paste the Rtf (formatted text) back into the visible RichTextBox. That way there is no flickering while you are doing the highlighting.

Drawbacks
There is still some minor flickering when the text gets large enough to consume more than a page. Also, the scroll bar position is reset when you edit text above the bottom line of the text.