To get a fresh GUID (An unique global number) use System.Guid
Guid guid = System.Guid.NewGuid();
To get a fresh GUID (An unique global number) use System.Guid
Guid guid = System.Guid.NewGuid();
Although .NET is great in hiding whether a process is 32 or 64 bits sometimes you still need to know this. Most likely when using interop. You cannot interop a 64 bit .NET and a 32 bit dll and vice versa!
Fortunately the IntPtr.Size property can be used for this.
if ( IntPtr.Size == 8 ) { // Pointer is 8 bytes -> 64 bit } if ( IntPtr.Size == 4 ) { // Pointer is 4 bytes -> 32 bit }
System.Environment.TickCount
Use System.Environment.TickCount to get the number of milliseconds expired since session startup. Although this number is expressed in ms its actual resolution may be lower. A test on a I7 2.8GHZ CPU / Windows 7 64 Bit shows it’s interval is about 15 ms, also this value will wrap around, something to take in consideration.
DateTime.Now.Ticks
You can also use the DateTime.Now.Ticks which reports the number of ‘ticks’ in a 100ns resolution since 12:00:00 midnight, January 1, 0001. Being a 64 bit value it will not wrap around and is more accurate. Again this is not its true resolution. A test on a I7 2.8GHZ CPU / Windows 7 64 Bit shows it’s interval is 10000 x 100ns = 1 ms.
Exact timing, the StopWatch class
For exact timing purposes you can better use the System.Diagnostics.Stopwatch class. It uses the high resolution timers which are far more better.
Note: On any CPU you are never alone, especially on a single core the threads and processes share the time available. If you want to perform detailed timing on a multi-core CPU you can use the CPU affinity mask trying to reserve a CPU core for your process alone (although you are never really sure the OS doesn’t share).
Comparing two arrays.
What doesn’t work
int[] a = new int[2]{1,2}; int[] b = new int[2]{1,2}; if (a.Equals(b)) { // No match. The Array objects differ although they contain the same value. } b = a; if (a.Equals(b)) { // Match, the Array objects are the same. }
What does work
Strangely enough the Array function does not contain a compare function.
To compare two buffers use the following code
i int i; for (int i=0; i < first.Length; i++) { if (first[i] != second[i]) { return false; } }
Note this code is optimized by the compiler which detects 0 < Length loops.
Some times it is useful to know whether a selection was made from code or by an user since the AfterCheck event of a TreeView control is fired after both.
Use the code below to see how the selection was made:
private void tree1_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action == TreeViewAction.ByKeyboard) { .. } if (e.Action == TreeViewAction.ByMouse) { .. } }
To get the current mouse position use the static method Control.MousePosition
It returns a Point structure in screen coordinates. Use PointToClient method of the control to map it to client coordinates.
Example:
Point cp = panel1.PointToClient(Control.MousePosition);
Note:
The PointToClient result may be outside the control’s bounding area if the mouse was not above the control.
I have found some interesting solutions for this including iterating all controls and check for their focus state.
The ActiveControl property of the form class also seems to work rather well …
Set a form to “autosize” allows the form to shrink or grow by it’s contents. It however does not allow the user to resize the form manually.
To allow the user to resize the form set the form AutoSize property to false in the form onLoad event handler. This allows the form resize initially but user resizable afterwards.
Of course changing the contents of the hosted controls may require you to resize the form. You can set the AutoSize to true before making the changes or use the GetPreferredSize method and resize the form yourself.
To check whether a modifier key is currently in a pressed state use the static function Control. ModifierKeys
Example:
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) { .. }
When a panel has scrollbars ‘default’ drawing on the DC does not work correctly when the upper-left is not at 0,0. Graphics functions draw on the current client area, even when scrolled the upper-left posotion of the client area is still at 0,0.
Use the function TranslateTransform to offset the coordinates.
Example
private void dc_Paint(object sender, PaintEventArgs e) { e.Graphics.TranslateTransform(dc.AutoScrollPosition.X, dc.AutoScrollPosition.Y); //0,0 is now relative to the total scrollable area, not the visible part (client area). }
Notes:
Although this will work great for simple drawing and unnecessary drawing is limited (being clipped ), some form of optimization would speed things up considerably.
To setup a larger DC size as visible set the properties AutoScroll to true and the property AutoScrollMinSize to a size larger then the visible area.