Archive for August, 2008

How-To: Find a bounding rectangle of WinForms Controls

Thursday, August 21st, 2008

Recently I was refactoring a peace of code which calculates a bounding rectangle of a controls on a form. The initial code has a following form:

int top = Int32.MaxValue; . . .
foreach (Control c in Controls)
{
if (top == Int32.MaxValue || top > c.Top) top = c.Top;
//the same for bottom, left, right
}

After some thinking I come up with the following solution:

List<Control> controls =
new List<Control>(this.Controls.Cast<Control>());

Rectangle rect = controls.Aggregate<Control, Rectangle>(
controls[0].Bounds, (r, c) => Rectangle.Union(r, c.Bounds));

Looks better, yeh?

Script.NET Tutorial->Part 2->Running Scripts

Monday, August 11th, 2008

For testing puproses I have created Script.NET Ide application. It’s purpose is to execute user scripts. It has simple syntax highlight and output panel:

ide_small.gif

ide.gif

For syntax highlighting I have used Puzzle.SyntaxBox component.

Script Ide application may be downloaded by this link.

Exercise. Parsing script

  • Start Script.NET Ide application;

  • There will be default script- “Bubble Sort”;

  • Press F9;

  • Status bar will show the results of parsing.

Exercise. Running script

  • To run the script press F5;

  • Status bar will show results of execution;

  • Output window will shouw script’s output.

The script is able to interact with Script.IDE application using “Console” alias: Console is simply reference to Main Window. You can use “WriteLine” and “Write” functions to output results to output panel.All properties of .NET form are also available:

Console.Text = ‘My Ide’; //Will change window’s caption.

 

Script.NET Tutorial->Part 1->Running Scripts

Wednesday, August 6th, 2008

Due to recent requests from community I will post series of tutorials for Script.NET beginners. This first tutorial will introduce basic concepts of Script.NET language and interoperability with .NET objects and classes.

Prerequesties: .NET Framework 3.5, Visual Studio, Script.NET library.

Exercise 1. Adding scripting to C# application

  • Create Empty C# Console Application
  • Add Reference to IronyScriptDotNet.dll:
    reference.gif
  • Open Program.cs for editing
  • Add using clause: using ScriptNET;
  • In the body of Main function type following code:
    Script.RunCode(”Console.WriteLine(’Hello Script.Net’);”);
  • Run Application by Pressing F5.
  • The message should be produced by the script.

Exercise 2. Querying Results from Script

  • Modify previous example so the body of Main Function will be:
    object rez = Script.RunCode(”1+1;”);
    Console.WriteLine(rez);
  • After execution, Script returns result of the last statement, so the output of this code will be 2.

Exercise 3. Interaction with .NET objects

In this exercise we will create a Script which will return a sum of values stored in List. The list will be usual .NET object and will be passed to the script.

  • Create a list of integers:
    List<int> vals = new List<int>();
    vals.AddRange(new int[] { 1, 2, 3, 4 });
  • Create a Script Object using Compile method:
    Script script = Script.Compile(@”
    rez = 0;
    foreach (number in numbers)
    rez += number;”);
  • This code assumes that array will be passed as “numbers” parameter
  • To interact with script’s variables there is a special object called Context. We will add .NET List to the script’s context:
    script.Context.SetItem(”numbers”, ContextItem.Variable, vals);
  • Now, we are ready to evaluate script:
    object rez = script.Execute();
    Console.WriteLine(rez);
  • The result should be 10
  • This example may be downloaded here. (The same example, but based on the latest version: download)