I've tried the method you supplied in the comments of SA's solution, and apart from the incorrect variable (I assume "name" is a typo and should be "classname", or vice versa) it compiles fine as an extension method.
It sounds like you're just trying to put it in the wrong place.
I would strongly suggest that you shouldn't put this class in the same file as a designer based class. Indeed, good practice is to define only one class per file - so add another file and put this class in it - e.g. Extended.cs (same name as your class, although I think you should come up with a better name for the class). Although it doesn't have to be in a namespace to compile, it should be - just put it in your application's namespace.
Regards,
Ian.
Updated Example:
HtmlDocumentExtensions.cs
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class HtmlDocumentExtensions
{
public static HtmlElement GetElementByName(this HtmlDocument document, string classname)
{
foreach (HtmlElement element in document.All)
{
if (element.GetAttribute("name").Contains(classname))
{
return element;
}
}
return null;
}
}
}
Somewhere else in the code, in this case in my test form, Form1.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HtmlDocument doc = null;
HtmlElement element = doc.GetElementByName("The name");
}
}
}