Category: Misc

Event handling (consuming) between forms

How to handle event:

objEditClassForm = new EditClass();
objEditClassForm.FormClosed += new FormClosedEventHandler(this.EditClassForm_Closed);

To explain in details:

objEditClassForm => is an object of the class EditClass (which inherits from the Form class)

So, in short, “objEditClassForm” is a simple windows form!
FormClosed => is a property of the class EditForm, this property is an “event” property

It is defined as follows:

public event FormClosedEventHandler FormClosed;

– This is code is defined in the System.Windows.Forms.Form class (built-in in Visual Studio)
– We can see that the property is declared as event
– We can also see that they type of the event is pre-defined (FormClosedEventHandler)

it is also built-in in Visual Studio in System.Windows.Forms.FormClosedEventHandler file, and is simply defined as:

public delegate void FormClosedEventHandler(object sender, FormClosedEventArgs e);

We can see that defining an event can be extremely simple!!!

+= sign => it is a necessary syntax when defining an event handler for the event

FormClosedEventHandler => see above

this.EditClassForm_Closed => is a function that will handle our event

In my example, it looks as follows:

private void EditClassForm_Closed(object sender, EventArgs e){
_load_all();
}

NOTES:

If you close (dispose of) the class that was assigned an event handler, it will not contain the event handler after that.

You will have to do it again!!!

Another example from MSDN is here:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

public class MyForm : Form
{
private TextBox box;
private Button button;
public MyForm() : base()
{
box = new TextBox();
box.BackColor = System.Drawing.Color.Cyan;
box.Size = new Size(100,100);
box.Location = new Point(50,50);
box.Text = “Hello”;
button = new Button();
button.Location = new Point(50,100);
button.Text = “Click Me”;

// To wire the event, create
// a delegate instance and add it to the Click event.
button.Click += new EventHandler(this.Button_Clicked);
Controls.Add(box);
Controls.Add(button);
}

// The event handler.
private void Button_Clicked(object sender, EventArgs e)
{
box.BackColor = System.Drawing.Color.Green;
}

// The STAThreadAttribute indicates that Windows Forms uses the
// single-threaded apartment model.
[STAThreadAttribute]
public static void Main(string[] args)
{
Application.Run(new MyForm());
}

}

@-quoted string literals

The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@”c:\Docs\Source\a.txt” rather than “c:\\Docs\\Source\\a.txt”

To include a double quotation mark in an @-quoted string, double it:

@”””Ahoy!”” cried the captain.” -> “Ahoy!” cried the captain.

Regular expressions in C#

How to: Verify That Strings are in Valid E-Mail Format

bool IsValidEmail(string strIn){
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @”^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$”);
}

Operators in brief

Operator Description
* Postfix unary operator signifying zero or more occurrences of the operand.
+ Postfix unary operator signifying one or more occurrences of the operand.
? Postfix unary operator signifying zero or one occurrence of the operand.
| Binary infix operator meaning “or”.
() Used to group items.

Escaping Characters

Ordinary characters, other than the ones in the table below, match themselves. That is, an “a” in a regular expression specifies that input should match an “a”.

Another significant difference in writing handwriting regular expressions is that you can only escape a limited set of characters within a regular expression. The following table outlines the allowed set of characters that can be escaped. Any other attempt to escape a character will result in an error being thrown by the recognizer.

Character
\*
\+
\?
\(
\)
\|
\\
\!
\.
\[
\]
\^
\{
\}
\$
\#

OpenXML file manipulation

OpenXML file manipulation
Top  Previous  Next
http://www.rootsilver.com/2008/11/office-2007-open-xml-global-string-replace

Code to replace strings with desired content.
Very handy for template based editing.
There is already existing project in RAC/OpenXML

using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Packaging; //C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\windowsbase.dll

public class ReplacementUtility {
public static void Main {
ReplacementUtility utility = new ReplacementUtility();
//Word 2007
string templateFile = “c:\\template.docx”;
string newFile = “c:\\widgets.docx”;

File.Copy(templateFile, newFile);
utility.UpdateOpenXmlDocument(newFile, “Acme Inc”, “Wally’s Widgets”);

//Excel 2007
templateFile = “c:\\template.pptx”;
newFile = “c:\\widgets.pptx”;

File.Copy(templateFile, newFile);
utility.UpdateOpenXmlDocument(newFile, “Acme Inc”, “Wally’s Widgets”);
}

public void UpdateOpenXmlDocument(string fileName, string searchRegexp, string replacement) {
Regex regex = new Regex(searchRegexp, RegexOptions.Multiline|RegexOptions.IgnoreCase);
using (Package package = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) {
foreach (PackagePart part in package.GetParts()) {
if (part.Uri.ToString().IndexOf(“.xml”) != -1
||
part.Uri.ToString().IndexOf(“.rels”) != -1) {

XmlDocument document = new XmlDocument();
document.Load(part.GetStream());

if (regex.IsMatch(document.InnerXml)) {
document.InnerXml = regex.Replace(document.InnerXml, replacement);
document.Save(part.GetStream(FileMode.Create, FileAccess.Write));
}
}
}
}
}
}