CodeKeep C# Feed Giugno 17th, 2008
CodeKeep C# Feed Giugno 17th, 2008
->
Description: To Open form by creating instance of it.
Link: http://www.codekeep.net/snippets/eac1283b-10ef-4314-a427-c88fb31f4205.aspx
private void addUserToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MidAtlanticBaseClasses.BaseClass.objfrmAddUser == null || MidAtlanticBaseClasses.BaseClass.objfrmAddUser.IsDisposed == true)
{
MidAtlanticBaseClasses.BaseClass.objfrmAddUser = null;
MidAtlanticBaseClasses.BaseClass.CreateUserInstance("frmAddUser");
}
//MidAtlanticBaseClasses.BaseClass.UserEditMode = 0;
//((MidAtlanticUser.AddUser)MidAtlanticBaseClasses.BaseClass.objfrmAddUser).Mode = (int)MinAtlanticBusinessLayer.clsUserInformation.enUserEditMode.Add;
MidAtlanticBaseClasses.BaseClass.objfrmAddUser.MdiParent = this;
// frmAddUser.Mode = (int)MinAtlanticBusinessLayer.clsUserInformation.enUserEditMode.Add;
PropertyInfo[] objProperties = MidAtlanticBaseClasses.BaseClass.objfrmAddUser.GetType().GetProperties();
foreach (PropertyInfo objProperty in objProperties)
{
if (objProperty.Name == "FormMode")
{
objProperty.SetValue(MidAtlanticBaseClasses.BaseClass.objfrmAddUser, (int)MinAtlanticBusinessLayer.clsUserInformation.enUserEditMode.Add, null);
break;
}
}

CodeKeep C# Feed Giugno 17th, 2008
Description: MidAtlantic Base Class
Link: http://www.codekeep.net/snippets/0800d7c5-35ab-4d69-8b32-ad3d1beb3cf8.aspx
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Reflection;
using System.Resources;
namespace MidAtlanticBaseClasses
{
public class BaseClass
{
//Property Declaration - SystemConfiguration
/// <summary>
/// public and static variable for MidAtlantic Connection String
/// </summary>
public static String strConnectingString = "";
public static String strDatabseName = "MidAtlanticAdmin";
public static AppSettingsReader objSettingReader;
public static string LoginId = string.Empty;
public static string UserName = string.Empty;
public static bool IsApplicationExitFlag = false;
//Reference Declaration - System Configurations
/// <summary>
/// public static variable for the System Configurations form
/// </summary>
public static Form objfrmSystemConfigurations = null;
//Reference Declaration - User Component
/// <summary>
/// public static variable for the Login form
/// </summary>
public static Form objfrmLogin = null;
public static Form objfrmAddUser = null;
public static Form objfrmUserManagement = null;
public static Form objfrmChangePassword = null;
/// <summary>
/// Public static variable for Manage Categories
/// </summary>
public static Form objfrmManageCategory = null;
public static Form objfrmAddEditCategory = null;
/// <summary>
/// Public static variable for Company Management
/// </summary>
public static Form objfrmManageCompany = null;
public static Form objfrmAddEditCompany = null;
/// <summary>
/// Public static variable for product Management
/// </summary>
public static Form objfrmAddEditProduct = null;
/// <summary>
/// This method is used to create the instance of User Module
/// </summary>
/// <param name="strClassName"></param>
public static void CreateUserInstance(String strClassName)
{
try
{
String strAssemblyPath = "";
objSettingReader = new AppSettingsReader();
strAssemblyPath = objSettingReader.GetValue("MidAtlanticUser", strAssemblyPath.GetType()).ToString();
strAssemblyPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + strAssemblyPath;
if (System.IO.File.Exists(strAssemblyPath))
{
Assembly userAssembly = Assembly.LoadFile(strAssemblyPath);
System.Type[] ExistingTypes = userAssembly.GetTypes();
foreach (Type type in ExistingTypes)
{
if (type.Name.Equals(strClassName))
{
switch (type.Name)
{
case "frmLogin":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmLogin == null)
{
MidAtlanticBaseClasses.BaseClass.objfrmLogin = ((Form)Activator.CreateInstance(type));
}
break;//
}
case "frmAddUser":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmAddUser == null)
{
MidAtlanticBaseClasses.BaseClass.objfrmAddUser = ((Form)Activator.CreateInstance(type));
}
break;
}
case "frmUserManagement":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmUserManagement == null)
{
MidAtlanticBaseClasses.BaseClass.objfrmUserManagement = ((Form)Activator.CreateInstance(type));
}
break;
}
}
}
}
}
}
catch (Exception objEx)
{
MidAtlanticDataLibrary.Database.logManager objLogManager = new MidAtlanticDataLibrary.Database.logManager();
objLogManager.AddErrorLogToDatabase(objEx, MidAtlanticBaseClasses.BaseClass.strConnectingString);
objLogManager = null;
}
}
/// <summary>
/// This Method is used to create Company management objects
/// </summary>
/// <param name="strClassName"></param>
public static void CreateCompanyInstance(String strClassName)
{
try
{
String strAssemblyPath = "";
objSettingReader = new AppSettingsReader();
strAssemblyPath = objSettingReader.GetValue("CompanyManagement", strAssemblyPath.GetType()).ToString();
strAssemblyPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + strAssemblyPath;
if (System.IO.File.Exists(strAssemblyPath))
{
Assembly userAssembly = Assembly.LoadFile(strAssemblyPath);
System.Type[] ExistingTypes = userAssembly.GetTypes();
foreach (Type type in ExistingTypes)
{
if (type.Name.Equals(strClassName))
{
switch (type.Name)
{
case "frmManageCompany":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmManageCompany == null)
{
MidAtlanticBaseClasses.BaseClass.objfrmManageCompany = ((Form)Activator.CreateInstance(type));
}
break;
}
case "AddEditCompany":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmAddEditCompany == null)
{
MidAtlanticBaseClasses.BaseClass.objfrmAddEditCompany = ((Form)Activator.CreateInstance(type));
}
break;
}
}
}
}
}
}
catch (Exception objEx)
{
MidAtlanticDataLibrary.Database.logManager objLogManager = new MidAtlanticDataLibrary.Database.logManager();
objLogManager.AddErrorLogToDatabase(objEx, MidAtlanticBaseClasses.BaseClass.strConnectingString);
objLogManager = null;
}
}
/// <summary>
/// This Method is used to create Product management objects
/// </summary>
/// <param name="strClassName"></param>
public static void CreateProductInstance(String strClassName)
{
try
{
String strAssemblyPath = "";
objSettingReader = new AppSettingsReader();
strAssemblyPath = objSettingReader.GetValue("productManagement", strAssemblyPath.GetType()).ToString();
strAssemblyPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + strAssemblyPath;
if (System.IO.File.Exists(strAssemblyPath))
{
Assembly userAssembly = Assembly.LoadFile(strAssemblyPath);
System.Type[] ExistingTypes = userAssembly.GetTypes();
foreach (Type type in ExistingTypes)
{
if (type.Name.Equals(strClassName))
{
switch (type.Name)
{
case "frmAddEditProduct":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmAddEditProduct == null)
{
MidAtlanticBaseClasses.BaseClass.objfrmAddEditProduct = ((Form)Activator.CreateInstance(type));
}
break;
}
//case "AddEditCompany":
// {
// if (MidAtlanticBaseClasses.BaseClass.objfrmAddEditCompany == null)
// {
// MidAtlanticBaseClasses.BaseClass.objfrmAddEditCompany = ((Form)Activator.CreateInstance(type));
// }
// break;
// }
}
}
}
}
}
catch (Exception objEx)
{
MidAtlanticDataLibrary.Database.logManager objLogManager = new MidAtlanticDataLibrary.Database.logManager();
objLogManager.AddErrorLogToDatabase(objEx, MidAtlanticBaseClasses.BaseClass.strConnectingString);
objLogManager = null;
}
}
/// <summary>
/// This Method is used to create MidAtlanticSystem System Configurations objects
/// </summary>
/// <param name="strClassName"></param>
public static void CreateSystemConfigurationsInstance(String strClassName)
{
try
{
String strAssemblyPath = "";
objSettingReader = new AppSettingsReader();
strAssemblyPath = objSettingReader.GetValue("MidAtlanticSystem", strAssemblyPath.GetType()).ToString();
strAssemblyPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + strAssemblyPath;
if (System.IO.File.Exists(strAssemblyPath))
{
Assembly userAssembly = Assembly.LoadFile(strAssemblyPath);
System.Type[] ExistingTypes = userAssembly.GetTypes();
foreach (Type type in ExistingTypes)
{
if (type.Name.Equals(strClassName))
{
switch (type.Name)
{
case "frmSystemConfig":
{
if (MidAtlanticBaseClasses.BaseClass.objfrmSystemConfigurations == null || MidAtlanticBaseClasses.BaseClass.objfrmSystemConfigurations.IsDisposed == true)
{
MidAtlanticBaseClasses.BaseClass.objfrmSystemConfigurations = ((Form)Activator.CreateInstance(type));
}
break;
}
}
}
}
}
}
catch (Exception objEx)
{
MidAtlanticDataLibrary.Database.logManager objLogManager = new MidAtlanticDataLibrary.Database.logManager();
objLogManager.AddErrorLogToDatabase(objEx, MidAtlanticBaseClasses.BaseClass.strConnectingString);
objLogManager = null;
}
}
/// <summary>
/// Function to Reterive System Configuration values from the specified Resource File.
/// </summary>
public static Boolean GetSystemConfigurationValues()
{
ResourceReader objResource = null;
String strDatabaseConnection = "";
String strDatabaseUsername = "";
String strDatabasePassword = "";
Boolean blnIsValid = true;
try
{
System.IO.FileInfo objFileInfo = new System.IO.FileInfo(@AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Mid-Atlantic.Mid-AtlanticResource.resources");
if (objFileInfo.Exists == false)
{
return false;
}
objResource = new ResourceReader(@AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Mid-Atlantic.Mid-AtlanticResource.resources");
foreach (DictionaryEntry objEntry in objResource)
{
switch (objEntry.Key.ToString())
{
case "Mid-AtlanticDatabaseSource":
{
strDatabaseConnection = objEntry.Value.ToString();
break;
}
case "Mid-AtlanticDatabaseUsername":
{
strDatabaseUsername = objEntry.Value.ToString();
break;
}
case "Mid-AtlanticDatabasePassword":
{
strDatabasePassword = objEntry.Value.ToString();
break;
}
}
}
if (strDatabaseConnection.Trim().Length > 0)
{
strConnectingString = "Server=" + strDatabaseConnection.Trim() + ";Initial Catalog="+MidAtlanticBaseClasses.BaseClass.strDatabseName+";User ID=" + strDatabaseUsername.Trim() + ";Password=" + strDatabasePassword.Trim() + ";";
}
}
catch (Exception objEx)
{
blnIsValid = false;
MidAtlanticDataLibrary.Database.logManager objLog = new MidAtlanticDataLibrary.Database.logManager();
objLog.AddErrorLogToDatabase(objEx, MidAtlanticBaseClasses.BaseClass.strConnectingString);
objLog = null;
}
finally
{
if (objResource != null) objResource.Close();
objResource = null;
}
return blnIsValid;
}
}
}

CodeKeep C# Feed Giugno 17th, 2008
Description: a simple encryptor/decryptor
Link: http://www.codekeep.net/snippets/bf47b389-24ba-41c5-9b23-2f2ffb64d0c9.aspx
/// <summary>
/// Class to encrypt and decrypt the password
/// </summary>
public static class EncryptorDecryptor
{
public static int key = 3;
/// <summary>
/// Encrypts and decrypts a given string.
/// </summary>
/// <param name="textToEncrypt">The text to encrypt or decrypt.</param>
/// <returns></returns>
public static string EncryptDecrypt(string textToEncrypt)
{
StringBuilder inSb = new StringBuilder(textToEncrypt);
StringBuilder outSb = new StringBuilder(textToEncrypt.Length);
char c;
for (int i = 0; i < textToEncrypt.Length; i++)
{
c = inSb[i];
c = (char)(c ^ key);
outSb.Append(c);
}
return outSb.ToString();
}
}

CodeKeep C# Feed Giugno 17th, 2008
Description: Documented Memebrs of FORM
Link: http://www.codekeep.net/snippets/7f2acf17-0777-450c-a021-d64b5e5a5bf1.aspx
// Add pre-defined Controls to the form.
this.Controls.Add(button1);
// Add button2 to the form.
this.Controls.Add(button2);
// Add Form to a pre-defined panel.
panel1.Controls.Add(this);
//FormBorderStyle Gets or sets the border style of the form.
//
//Define the border style of the form to a FixedToolWindow
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
//
// Define the border style of the form to a dialog box.
this.FormBorderStyle = FormBorderStyle.FixedDialog;
//FormStartPosition Gets or sets the starting position of the form at run time.
//
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
//MaximizeBox Gets or sets a value indicating whether the Maximize button is displayed in the caption bar of the form.
//MinimizeBox Gets or sets a value indicating whether the Minimize button is displayed in the caption bar of the form.
//
// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;
//Referencing Form Class by passing this on instantiation of class
//
//Field variable
public Form2 MyToolWindow;
//
MyToolWindow = new Form2(this, panel1);
//
//Note: Class being called has receiving arguments as below in class constructor
public Form2(Form referencingForm, Panel referencingPanel)
//Show or ShowDialog means to display
//
// Display the form as a modal dialog box.
form1.ShowDialog();
//ShowInTaskbar using boolean
//Gets or sets a value indicating whether the form is displayed in the Windows taskbar.
this.ShowInTaskbar = false;
//Text for title in form (leave propeties Text blank)
//Gets or sets the text associated with this control. (Overrides Control..::.Text.)
//In .NET Compact Framework 3.5, this member is inherited from Control..::.Text.
this.Text = "TitleBar name here...";
//TopLevel Gets or sets a value indicating whether to display the form as a top-level window.
this.TopLevel = false;

CodeKeep C# Feed Giugno 17th, 2008
Description: using XmlSerializer to serialize / deserialize objects
Link: http://www.codekeep.net/snippets/a253f09f-6276-4ef1-9083-e3557f0a5852.aspx
class Program
{
static void Main(string[] args)
{
TestData data = new TestData();
data.ItemId = 1;
data.ItemDesc = "MyDesc";
// serialize
String XmlizedString = null;
MemoryStream memoryStream1 = new MemoryStream();
XmlSerializer xs1 = new XmlSerializer(typeof(TestData));
XmlTextWriter xmlTextWriter1 = new XmlTextWriter(memoryStream1, Encoding.UTF8);
xs1.Serialize(xmlTextWriter1, data);
memoryStream1 = (MemoryStream)xmlTextWriter1.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream1.ToArray());
Console.WriteLine(XmlizedString);
// deserialize
XmlSerializer xs2 = new XmlSerializer(typeof(TestData));
MemoryStream memoryStream2 = new MemoryStream(StringToUTF8ByteArray(XmlizedString));
XmlTextWriter xmlTextWriter2 = new XmlTextWriter(memoryStream2, Encoding.UTF8);
TestData data2 = (TestData)xs2.Deserialize(memoryStream2);
Console.WriteLine(data2.ItemId);
Console.WriteLine(data2.ItemDesc);
Console.ReadLine();
}
private static String UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(characters);
return (constructedString);
}
private static Byte[] StringToUTF8ByteArray(String pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
}
