private static void TestPadding()
{
int ix = 0;
string result = string.Empty;
result = ix.ToString("X");
result = result.PadLeft(8, '0');
System.Diagnostics.Debug.Assert(result.Length == 8);
ix = 1;
result = ix.ToString("X");
result = result.PadLeft(8, '0');
System.Diagnostics.Debug.Assert(result.Length == 8);
ix = 9999;
result = ix.ToString("X");
result = result.PadLeft(8, '0');
System.Diagnostics.Debug.Assert(result.Length == 8);
ix = 99999999;
result = ix.ToString("X");
result = result.PadLeft(8, '0');
System.Diagnostics.Debug.Assert(result.Length == 8);
ix = int.MaxValue;
result = ix.ToString("X");
result = result.PadLeft(8, '0');
System.Diagnostics.Debug.Assert(result.Length == 8);
ix = int.MinValue;
result = ix.ToString("X");
result = result.PadLeft(8, '0');
System.Diagnostics.Debug.Assert(result.Length == 8);
}
September 23, 2007
Test String Padding Functions
Using The IComparer and IComparable Interfaces
Sorting and the IComparer and IComparable Interfaces
private class XXXComparer : IComparer{ public XXXComparer() { } #region IComparer Members public int Compare(IXXX x, IXXX y) { return x.SomeProperty.CompareTo(y.SomeProperty); } #endregion } Say you have a list of IXXX, you can sort it with this m_XXX_List.Sort(new XXXComparer()); private class SomeObjectComparer : IComparer { #region IComparer Members public int Compare(object x, object y) { IXXX xi = x as IXXX; IXXX yi = y as IXXX; return xi.SomeProperty.CompareTo(yi.SomeProperty); } #endregion } private class XXX : IComparable { #region IComparable Members public int CompareTo(XXX other) { return this.SomeProperty.CompareTo(other.SomeProperty); } #endregion } Say you have a list of XXX, you can sort it with this m_XXX_List.Sort(); // Simplest Enumerator. Can enumerate the objects without // exposing the underlying list public IEnumerable Processes() { foreach (IProcess process in m_Processes) { yield return process; } }
Enable/Disable Form Close
This disables the system menu file close
private const int SC_CLOSE = 0xF060;
private const int MF_ENABLED = 0x0;
private const int MF_DISABLED = 0x1;
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);
private void DisableFormClose()
{
EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_DISABLED);
}
private void EnableFormClose()
{
EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_ENABLED);
}
Need to add something that disables the File Close menu
Iterating Over Dictionary Entries
Dictionary<string, XXX> m_MyDictionary = new Dictionary<string, XXX>() foreach (KeyValuePairkvp in m_MyDictionary) { DoSomething(kvp.Key); DoSomethingElse(kvp.Value); }
Using PInvoke
Check out the PInvoke website. Greate Site for finding PInvoke syntax for c# Calls to the WIN 32 API.
A Winforms CheckList
- Check the StartPosition property is CenterParent
- Check the Text property (the Title) is set to something sensible
- IF it is a child window set the 'ShowInTaskbar' property to 'False'
- Check FormBorderStyle property is set to 'FixedDialog' on the Form if it is non-resizable.
- Check MaximiseBox property is set to 'True' on the Form if you want it
- Check MinimiseBox property is set to 'True' on the Form if you want it
- Check AcceptButton property is set on the Form to the 'default' accept/OK button
- Check CancelButton property is set on the Form to the 'default' cancel changes button
- IF you want the OK and cancel buttons to work automatically then set the DialogResult property of the button to Accept or Cancel etc.
- Check the TAB order is set.
this.AcceptButton = this.NAME_OF_ACCEPT_OK_BUTTON;
this.CancelButton = this.NAME_OF_CANCEL_BUTTON;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "FORM TITLE";
Modal/Modeless Dialogs
Modal Dialog
SomeForm sf = new SomeForm();
DialogResult res = sf.ShowDialog(this);
To close a form
... // within the form class itself
DialogResult = DialogResult.OK;
Close();
...
Set the AcceptButton and CancelButton to the correct buttons if required
Modeless Dialog
// Declare the modeless dialog
private MagnifyingGlassForm m_MagFrm = null;
// Creating and showing the modeless dialog
if (m_MagFrm == null)
{
m_MagFrm = new MagnifyingGlassForm();
m_MagFrm.Owner = this; // IMPORTANT Set the owner of the modeless dialog
m_MagFrm.Show(); // Show the modeless dialog
}
...
// To just hide the modeless dialog use Hide() method
m_MagFrm.Hide()
...
// Terminating the Modeless Dialog
if (m_MagFrm != null)
{
m_MagFrm.Close();
m_MagFrm.Dispose();
m_MagFrm = null;
}
Subscribe to:
Comments (Atom)