Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sean Harvell sean.harvell.ws

« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS 

Simple input box...

// Simple input box class: call by InputBox.ShowInputBox()

	public class InputBox : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.Container components = null;

		private InputBox()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			//
			// textBox1
			//
			this.textBox1.Location = new System.Drawing.Point(16, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(256, 20);
			this.textBox1.TabIndex = 0;
			this.textBox1.PasswordChar='*';
			this.textBox1.Text = "textBox1";
			this.textBox1.KeyDown += new
				System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
			//
			// InputBox
			//
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 53);
			this.ControlBox = false;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.textBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "InputBox";
			this.Text = "Password";
			this.StartPosition = FormStartPosition.CenterParent;
			this.ResumeLayout(false);			

		}

		private void textBox1_KeyDown(object sender,
			System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
				this.Close();
			if(e.KeyCode == Keys.Escape)
			{
              	textBox1.Text="";
				this.Close();
			}				
		}

		public static string ShowInputBox()
		{
			InputBox box = new InputBox();
			box.ShowDialog();
			return box.textBox1.Text;
		}    
	}

Basic Registry Access Example

// Basic Registry Access Example

RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\Company\Priduct\Version");
if (key!=null)
{
string ini = key.GetValue("localinifile","").ToString();
key.Close();
}

CSharp INI File handler class

// C# INI File handler class

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniFile(string INIPath)
        { path = INIPath; }

        public void IniWriteValue(string Section, string Key, string Value)
        { WritePrivateProfileString(Section, Key, Value, this.path); }

        public string IniReadValue(string Section, string Key, string Default)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 1024, this.path);
            return (temp.Length>0) ? temp.ToString() : Default;
        }
    }
}

Quick singleton util class

// Quick singleton util class

	public sealed class Util
	{		
		Util(){}
				
		public static Util I
		{ get { return Nested.I; } }
    
		class Nested
		{
			static Nested() {}
			internal static readonly Util I = new Util();
		}
	}

Simple JS Include function

// Simple JS Include function... add a root.js to the base lib directory...

function $include(path)
{
var x, base, src = "root.js", scripts = document.getElementsByTagName("script");
for (x=0; x<scripts.length; x++){if (scripts[x].src.match(src)){ base = scripts[x].src.replace(src, "");break;}}
document.write("<" + "script src=\"" + base + path + "\"></" + "script>");
}

Top N Rows

// Top N Rows

--SQL: 
TOP 3 * from mytable
--ACCESS: 
select top 3 * from (select * from mytable)
--ORACLE: 
ROWID <=3

Conditional Null Check

Conditional Null Check

--SQL: 
coalesce(field1, 0)
--ACCESS: 
iif(isnull(field1), 0, field1)
--ORACLE: 
coalesce(field1, 0)

DBMAIL

// sql server 2005 sample dbmail send

EXEC msdb.dbo.sp_send_dbmail @profile_name = 'dbmailprofile', @recipients = 'me@co.com,you@cocom', @body = 'the body', @subject = 'the subject'

Simple Raise Error

// simple raise error

IF UPDATE(lastname)
BEGIN
	RAISERROR ('cannot change lastname (source = instead of)', 16, 1)
	ROLLBACK TRAN
	RETURN
END

SQL Server 2000 RegEx

// SQL 2K RegEx Compat Code

CREATE FUNCTION dbo.find_regular_expression
	(
		@source varchar(5000),
		@regexp varchar(1000),
		@ignorecase bit = 0
	)
RETURNS bit
AS
	BEGIN
		DECLARE @hr integer
		DECLARE @objRegExp integer
		DECLARE @objMatches integer
		DECLARE @objMatch integer
		DECLARE @count integer
		DECLARE @results bit
		
		EXEC @hr = sp_OACreate 'VBScript.RegExp', @objRegExp OUTPUT
		IF @hr <> 0 BEGIN
			SET @results = 0
			RETURN @results
		END
		EXEC @hr = sp_OASetProperty @objRegExp, 'Pattern', @regexp
		IF @hr <> 0 BEGIN
			SET @results = 0
			RETURN @results
		END
		EXEC @hr = sp_OASetProperty @objRegExp, 'Global', false
		IF @hr <> 0 BEGIN
			SET @results = 0
			RETURN @results
		END
		EXEC @hr = sp_OASetProperty @objRegExp, 'IgnoreCase', @ignorecase
		IF @hr <> 0 BEGIN
			SET @results = 0
			RETURN @results
		END
			
		EXEC @hr = sp_OAMethod @objRegExp, 'Test', @results OUTPUT, @source
		IF @hr <> 0 BEGIN
			SET @results = 0
			RETURN @results
		END
		EXEC @hr = sp_OADestroy @objRegExp
		IF @hr <> 0 BEGIN
			SET @results = 0
			RETURN @results
		END
	RETURN @results
	END
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS