C# Quick Reference
C# Quick Reference
Gayle Laakmann
foreach foreach (DataRow drow in table.Rows) { ... } List List<string> mylist = new List<string>(); mylist.Add("hello"); Dictionary Dictionary<string, int> map = new Dictionary<string, int>(); map["hello"] = 5; Interacting Between Forms In Form1.cs SecondForm secondform = new SecondForm(this); In SecondForm.cs public SecondForm(System.Windows.Forms.Form p) { parentForm = p; } private void SecondForm(object sender, CancelEventArgs e) { parentForm.secondFormClosing(); } Painting a Gradient private void CustomControl_Paint(object s, PaintEventArgs e) { Color b = Color.LightBlue; Color w = Color.White; Rectangle r = new Rectangle(0, 0, this.Width, this.Height); LinearGradientBrush b = new LinearGradientBrush(r, b, w, 90); e.Graphics.FillRectangle(b, rect); }
Programmatically Adding Controls Label lbl = new Label(); lbl.Location = new Point(5, 10); this.Controls.Add(mylabel); Converting Ints to String int num = 13; num.ToString() Converting Strings to Ints string numstring = "123"; int result; bool succeeded = int.TryParse(numstring, out result); if (succeeded) { // this was actually a number }
Timer Timer minTimer = new Timer(); // or drag from Form Designer minTimer.Interval = 60000; minTimer.Tick += new System.EventHandler(this.minTimer_Tick); minTimer.Start(); private void minTimer_Tick(object sender, System.EventArgs e) { minTimer.Stop(); }
Comparing String Values string s = "hello"; string t = "hello"; if (s == t) { //s and t have the same value //s.Equals(t) is the same thing }
Operator Overloading public static Gayle operator +(Gayle m1, Gayle m2) {
Enumerations public enum Justification { Left, Right, Center }; public bool isLeft() { if (this.alignment == Justification.Left) { return true; } return false; } Get and Set (Properties) public static int MinimumAge { get { return minimum_age; } set //implicitly called when on "MinimumAge = 10" { if(value < 0) { minimum_age = 0; } else { minimum_age = value; } } }
Calling SQL-Getting Single Value string q = select max(title) from Blogs; SqlCommand command = new SqlCommand(q, BlinkySQL); myCommand.Connection.Open(); string title = command.ExecuteScalar(); command.Close();
Threading public delegate void Scruptious(string snacks); Thread myThread; private void startMyThread() { roses = new Scruptious(this.Airport); ThreadStart marge = new ThreadStart(Bubbles); myThread= new Thread(marge); myThread.Start(); } private void Airport(string word, int number) { //update the UI, or do what you want. //This happens in the main UI thread } private void Bubbles() { object[] parameters = {"Apples"}; this.Invoke(this.roses, parameters); } private void StopMyThread() { myThread.Abort(); }
Calling SQL-Execute Non-Query string q = delete from Blogs; SqlCommand command = new SqlCommand(q, BlinkySQL); BlinkySQL.Connection.Open(); command.ExecuteNonQuery(); BlinkySQL.Connection.Close();
Webservice edu.upenn.cis.wren.Blinky myBlinkySvc = new edu.upenn.cis.wren.Blinky(); /* user exists in system */ if (myBlinkySvc.UserExists(this.textBoxName.Text)) { /* successfully logged in */ if (myBlinkySvc.PasswordOK(username, password)) { ... } }
SQL-Insert insert into Blogs (UserID, BlogTitle) values (1234, 'Yo') SQL-Delete delete from Blogs where BlogID = 37515961 SQL-Select select UserID as 'User', max(datePosted) from Blogs where UserID <> 'Pat' group by UserID
SQL-Update update Blogs set BlogTitle = 'hello' where BlogTitle like '%hi%'
XMLWriting public void Save() { XmlDocument doc = new XmlDocument(); XmlElement element = doc.CreateElement("contact"); doc.AppendChild(element); XmlAttribute attr = doc.CreateAttribute("id"); attr.Value = "1234"; element.Attributes.Append(attr); XmlText txt = doc.CreateTextNode("Gayle"); element.AppendChild(txt); XmlTextWriter wr = new XmlTextWriter("c:\\xml.txt", Encoding.UTF8); wr.Formatting = Formatting.None; doc.Save(wr); wr.Close(); }
File IOWriting StreamWriter f = new StreamWriter(pth, false); try { f.WriteLine("stuff to write to file"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (f != null) { f.Close(); } }
RegistryReading RegistryKey regKey = Registry.LocalMachine; regKey = Registry.LocalMachine; regKey = regKey.CreateSubKey("Software\\Glaak\\Fetcha"); object o = regKey.GetValue("lastUser"); if ((o != null) && (o is string)) { string name = (string) o; }
RegistryWriting RegistryKey regKey = Registry.LocalMachine; regKey = Registry.LocalMachine; regKey = regKey.CreateSubKey("Software\\Glaak\\Fetcha"); regKey.SetValue("lastUser", name);