Archive for the ‘C#’ Category

Load user controls dynamically with ASP.NET

ctrl_homegadget ctrl
= (ctrl_homegadget) LoadControl("ctrl_homegadget.ascx");
ctrl.Border = false;
ctrl.Title = "Hello";
pnlGedgets.Controls.Add(ctrl);

VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

Strip SQL Comments using RegularExpression with C#

Here is the code:

Regex r
= new Regex(@"(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(–.*)");
textBox2.Text
= r.Replace(textBox1.Text, string.Empty);

VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

C# typeof equivalent in Visual Basic

Visual Basic.NET equivalent of C#’s typeof is GetType and NOT TypeOf

VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

How to dynamically make an InfoPath form

An InfoPath template have an extension of .XSN which is simply a .CAB file. If you extract it, you will see a set of files. Basically there will be:

manifest.xsf
myschema.xsd
sampledata.xml
template.xml and
view1.xsl

 
I found a non-straight forward way to create the files if you have some fixed form structure. Here are the actions [...]

Leave a Comment

Get list of WinForms Controls from an assembly

Here is the sample code.

private void ShowControlsFromAssembly(string assemblyfile)
{
Assembly assembly = Assembly.LoadFrom(assemblyfile);
Type[] et = assembly.GetExportedTypes();
[...]

Leave a Comment

ASP.NET - Download as ZIP

DotNetZipLib is one of the best compression/zip libraries for dotnet I found. You can download it from CodePlex.
Here is a snippet which demonstrates how to use DotNetZipLib to zip and force download directly to browser.

ZipFile zip = new ZipFile();
 
// Add files to zip
zip.AddFile(@"C:\blah.jpg", string.Empty);
zip.AddFile(@"C:\blee.jpg", string.Empty);
 
// save to memory stream instead of a direct file
MemoryStream ms [...]

Leave a Comment

Return DataTable from Web Service

I know this is an old theme but I had to write a sample app for demonstration in a tech forum. So reproducing here also.
Importantly, the DataTable/DataSet must be serializable.
ASMX Code:

public class Service1 : System.Web.Services.WebService
{
 
[WebMethod]
[...]

Leave a Comment

c# Listbox – change color of items

Again old theme but seems still people want this. Here is the code for doing this:

private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = new
string[] { "asdfljk", "sdfgsdfg", "xcv", "wrwerwer" };
[...]

Leave a Comment

Internet connection system tray notifier

Here is a tool for you to check if internet connection available or not. The tool uses below code for connection detection:
Download URL: http://kidoos.net/media/p/598.aspx

private bool IsInternetAvailable()
{
bool ret = false;
 
[...]

Comments (2)

C# CopyFromScreen Animation

Here is a code snippet:

private void Form1_Load(object sender, EventArgs e)
{
this.Show();
Graphics myGraphics = this.CreateGraphics();
 
[...]

Leave a Comment