Archive for Giugno, 2008

Form Class Members

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;
 

XmlSerializer (de)serialize

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;
	}
}

IPHostEntry

CodeKeep C# Feed Giugno 16th, 2008

Description: Get the name of box calling your page.

Link: http://www.codekeep.net/snippets/802fdfc2-b303-46c0-aafd-58cf945fe2ac.aspx

System.Net.IPHostEntry tmpEntry = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_HOST"]);

event log catch block

CodeKeep C# Feed Giugno 16th, 2008

Description: catch block that writes to the event log if the event log source exists.

Link: http://www.codekeep.net/snippets/d78d6529-4ae6-4979-bb36-97dec24c6958.aspx

catch (Exception ex)
        {
            if (EventLog.SourceExists(ConfigurationManager.AppSettings["EventLogSource"]))
            {
                List<string> exVarList = new List<string>();
                exVarList.Add(ex.Source + ":");
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Exception:==================");
                sb.AppendLine(ex.ToString());
                sb.AppendLine("Variable List:==============");
                foreach (string s in exVarList)
                {
                    sb.AppendLine(s);
                }
                EventLog eventLog = new EventLog(ConfigurationManager.AppSettings["EventLogName"],
                                                         ConfigurationManager.AppSettings["EventLogMachine"],
                                                         ConfigurationManager.AppSettings["EventLogSource"]);

                eventLog.WriteEntry(sb.ToString(), EventLogEntryType.Warning);
            }
        }

event log catch block

CodeKeep C# Feed Giugno 16th, 2008

Description: catch block that writes to the event log if the event log source exists.

Link: http://www.codekeep.net/snippets/d78d6529-4ae6-4979-bb36-97dec24c6958.aspx

catch (Exception ex)
        {
            if (EventLog.SourceExists(ConfigurationManager.AppSettings["EventLogSource"]))
            {
                List<string> exVarList = new List<string>();
                exVarList.Add(ex.Source + ":");
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Exception:==================");
                sb.AppendLine(ex.ToString());
                sb.AppendLine("Variable List:==============");
                foreach (string s in exVarList)
                {
                    sb.AppendLine(s);
                }
                EventLog eventLog = new EventLog(ConfigurationManager.AppSettings["EventLogName"],
                                                         ConfigurationManager.AppSettings["EventLogMachine"],
                                                         ConfigurationManager.AppSettings["EventLogSource"]);

                eventLog.WriteEntry(sb.ToString(), EventLogEntryType.Warning);
            }
        }

event log catch block

CodeKeep C# Feed Giugno 16th, 2008

Description: catch block that writes to the event log if the event log source exists.

Link: http://www.codekeep.net/snippets/d78d6529-4ae6-4979-bb36-97dec24c6958.aspx

catch (Exception ex)
        {
            if (EventLog.SourceExists(ConfigurationManager.AppSettings["EventLogSource"]))
            {
                List<string> exVarList = new List<string>();
                exVarList.Add(ex.Source + ":");
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Exception:==================");
                sb.AppendLine(ex.ToString());
                sb.AppendLine("Variable List:==============");
                foreach (string s in exVarList)
                {
                    sb.AppendLine(s);
                }
                EventLog eventLog = new EventLog(ConfigurationManager.AppSettings["EventLogName"],
                                                         ConfigurationManager.AppSettings["EventLogMachine"],
                                                         ConfigurationManager.AppSettings["EventLogSource"]);

                eventLog.WriteEntry(sb.ToString(), EventLogEntryType.Warning);
            }
        }

test

Inline If

SQL ASP.NET connectstring

CodeKeep C# Feed Giugno 13th, 2008

Description: string connectstring

Link: http://www.codekeep.net/snippets/9b5dffe5-da3f-4354-b2dc-3904654510c3.aspx

string connectstring = "Server="";Database="";User ID="";Password="";Trusted_Connection=False'";

PowerModeChanged

CodeKeep C# Feed Giugno 11th, 2008

Description: SystemEvents.PowerModeChanged

Link: http://www.codekeep.net/snippets/c3385f67-6c9b-4fce-8bae-2a4a5ba46883.aspx

SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged)

« Prev - Next »