|
foreach
|
foreach (DataRow drow in tableRows)
{
...
}
|
|
ArrayList (System.Collections.Hashtable)
|
ArrayList list = new ArrayList();
string s = "hello";
list.Add(s);
|
|
Hashtable (System.Collections.Hashtable)
|
Hashtable htable = new Hashtable();
htable.Add("hello",10);
if (htable.Contains("hello"))
{
int val = (int) htable("hello");
}
|
|
Programmatically Adding Controls
|
Label lbl = new Label();
lbl.Location = new Point(5, 10);
this.Controls.Add(mylabel);
|
|
Pausing / Adding Delay
|
using System.Threading;
public void PauseForALittle()
{
Thread.Sleep(1000);
}
|
|
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
}
|
|
Sorting Ints, Strings, etc
|
ArrayList list = ...
list.Sort();
|
|
|
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);
}
|
|
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();
}
|
|
Enumerators (System.Collections.IEnumerator)
|
IEnumerator IEn = lstviewMessages.SelectedItems.GetEnumerator();
while (IEn.MoveNext())
{
ListViewItem item = (ListViewItem) IEn.Current;
}
|
|
Killing an entire application
|
|
Application.Exit();
|
|