using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace RegexBuddyRegexClientDemo
{
///
/// Summary description for WinForm.
///
public class FormRegexDemo : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textRegex;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioAsIs;
private System.Windows.Forms.RadioButton radioC;
private System.Windows.Forms.RadioButton radioPascal;
private System.Windows.Forms.RadioButton radioPerl;
private System.Windows.Forms.RadioButton radioPerlOp;
private System.Windows.Forms.RadioButton radioBasic;
private System.Windows.Forms.Button btnInvoke;
public FormRegexDemo()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textRegex = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioBasic = new System.Windows.Forms.RadioButton();
this.radioPerlOp = new System.Windows.Forms.RadioButton();
this.radioPerl = new System.Windows.Forms.RadioButton();
this.radioPascal = new System.Windows.Forms.RadioButton();
this.radioC = new System.Windows.Forms.RadioButton();
this.radioAsIs = new System.Windows.Forms.RadioButton();
this.btnInvoke = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// textRegex
//
this.textRegex.Location = new System.Drawing.Point(8, 8);
this.textRegex.Multiline = true;
this.textRegex.Name = "textRegex";
this.textRegex.Size = new System.Drawing.Size(448, 80);
this.textRegex.TabIndex = 0;
this.textRegex.Text = "regular expression or string";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioBasic);
this.groupBox1.Controls.Add(this.radioPerlOp);
this.groupBox1.Controls.Add(this.radioPerl);
this.groupBox1.Controls.Add(this.radioPascal);
this.groupBox1.Controls.Add(this.radioC);
this.groupBox1.Controls.Add(this.radioAsIs);
this.groupBox1.Location = new System.Drawing.Point(8, 96);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(256, 168);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Text Style";
//
// radioBasic
//
this.radioBasic.Location = new System.Drawing.Point(16, 144);
this.radioBasic.Name = "radioBasic";
this.radioBasic.Size = new System.Drawing.Size(232, 16);
this.radioBasic.TabIndex = 5;
this.radioBasic.Text = "Basic string";
//
// radioPerlOp
//
this.radioPerlOp.Location = new System.Drawing.Point(16, 120);
this.radioPerlOp.Name = "radioPerlOp";
this.radioPerlOp.Size = new System.Drawing.Size(232, 16);
this.radioPerlOp.TabIndex = 4;
this.radioPerlOp.Text = "Perl operator";
//
// radioPerl
//
this.radioPerl.Location = new System.Drawing.Point(16, 96);
this.radioPerl.Name = "radioPerl";
this.radioPerl.Size = new System.Drawing.Size(232, 16);
this.radioPerl.TabIndex = 3;
this.radioPerl.Text = "Perl string";
//
// radioPascal
//
this.radioPascal.Location = new System.Drawing.Point(16, 72);
this.radioPascal.Name = "radioPascal";
this.radioPascal.Size = new System.Drawing.Size(232, 16);
this.radioPascal.TabIndex = 2;
this.radioPascal.Text = "Pascal string";
//
// radioC
//
this.radioC.Location = new System.Drawing.Point(16, 48);
this.radioC.Name = "radioC";
this.radioC.Size = new System.Drawing.Size(232, 16);
this.radioC.TabIndex = 1;
this.radioC.Text = "C string";
//
// radioAsIs
//
this.radioAsIs.Checked = true;
this.radioAsIs.Location = new System.Drawing.Point(16, 24);
this.radioAsIs.Name = "radioAsIs";
this.radioAsIs.Size = new System.Drawing.Size(232, 16);
this.radioAsIs.TabIndex = 0;
this.radioAsIs.TabStop = true;
this.radioAsIs.Text = "As is";
//
// btnInvoke
//
this.btnInvoke.Location = new System.Drawing.Point(272, 104);
this.btnInvoke.Name = "btnInvoke";
this.btnInvoke.Size = new System.Drawing.Size(184, 24);
this.btnInvoke.TabIndex = 2;
this.btnInvoke.Text = "Invoke RegexBuddy";
this.btnInvoke.Click += new System.EventHandler(this.btnInvoke_Click);
//
// FormRegexDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 270);
this.Controls.Add(this.btnInvoke);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.textRegex);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FormRegexDemo";
this.Text = "RegexBuddy Regex Client Demo";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new FormRegexDemo());
}
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(uint hwnd);
// We maintain only one connection to RegexBuddy for the entire application using static members
// If an application creates more than one connection, it will launch more than once instance of RegexBuddy
// Note that even if the user closes the instance of RegexBuddy created by our connection,
// that instance will still remain resident in the background, until our application closes.
// Each call to InitRegex will reuse the same instance of RegexBuddy launched by our connection.
static RegexBuddy.RegexBuddyIntfClass RegexBuddyConnection;
// Since we use one connection for the entire application, we need to remember in which context
// we called InitRegex, so we know how to handle FinishRegex.
static TextBox ActiveRegexBox;
static void FinishRegex(string Regex, uint StringType)
{
if (ActiveRegexBox != null) {
// Retrieve the updated regex from RegexBuddy
ActiveRegexBox.Text = Regex;
// FinishRegex is only called once per call to InitRegex
// So we should clean up this static reference
ActiveRegexBox = null;
}
}
private void btnInvoke_Click(object sender, System.EventArgs e)
{
if (RegexBuddyConnection == null) {
// Connect to RegexBuddy
try {
RegexBuddyConnection = new RegexBuddy.RegexBuddyIntfClass();
} catch (Exception ex) {
MessageBox.Show("Could not connect to RegexBuddy because of the following error:\r\n" +
ex.Message + "\r\n\r\n" +
"Make sure that RegexBuddy is installed on your computer, and has been run at least once.",
"Invoke RegexBuddy");
return;
}
// Connect FinishRegex event handler
// Note: RegexBuddy supports only a single event handler per client connection
// Therefore, you cannot handle both the FinishRegex and FinishAction events
// using the event connection method used here
RegexBuddy.IRegexBuddyIntfEvents_FinishRegexEventHandler FinishRegexEventHandler =
new RegexBuddy.IRegexBuddyIntfEvents_FinishRegexEventHandler(FinishRegex);
RegexBuddyConnection.FinishRegex += FinishRegexEventHandler;
// Make sure our instance of RegexBuddy makes it clear to the user that we're connected to it
RegexBuddyConnection.IndicateApp(Text, (uint)Handle);
}
// Since we use one connection for the entire application, we need to remember in which context
// we called InitRegex, so we know how to handle FinishRegex.
ActiveRegexBox = textRegex;
// Send regex or string with regex to RegexBuddy
uint StringType = 0;
if (radioC.Checked) {
StringType = 1;
} else if (radioPascal.Checked) {
StringType = 2;
} else if (radioPerl.Checked) {
StringType = 3;
} else if (radioPerlOp.Checked) {
StringType = 4;
} else if (radioBasic.Checked) {
StringType = 5;
}
RegexBuddyConnection.InitRegex(textRegex.Text, StringType);
// Bring RegexBuddy to front.
// SetForegroundWindow only works when the calling thread has input focus.
// Since we have input focus when the user clicked our button,
// it is our job to bring RegexBuddy to front.
SetForegroundWindow(RegexBuddyConnection.GetWindowHandle());
}
}
}