70-526 MCTS


Behaviour

Windows forms can behave in many ways.


AllowDrop

The default value is set to False. This property is inherited from Control.AllowDrop and allows items to be dropped onto the form. To handle the dropped item, the events DragDrop and DragEnter need to be handled.

To change the AllowDrop property at run time, simply set the property to the desired value.

this.AllowDrop = true;

AutoValidate

The default value is set to EnablePreventFocusChange. This property overrides ContainerControl.AutoValidate. The choice of values for this property are:

  • Disable - The controls on a form will not raise their validating event when they lose focus.
  • EnableAllowFocusChange - The control allows the focus to be lost even if the validation fails.
  • EnablePreventFocusChange - If the Validating method of the control sets the event argument's Cancel property to True then the focus will remain in the control.
  • Inherit - The control inherits its AutoValidate behavior from its container. As the form is the container, then this is never used.

    The Validating method of a control needs to validate the control in some way. For example, a textbox may only want to accept letters and not numbers. Ths code may look something like the following:

    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
      // Validate to not accept numbers
      if (textBox1.Text.IndexOfAny(new char[] { '0', '1', '2', '3', '4', 
          '5', '6', '7', '8', '9' }) != -1)
      {
        // String contains a numeric character
        e.Cancel = true;
      }
      else
      {
        // String is ok
        e.Cancel = false;
      }
    }

    If the validation fails, then the event is canceled. To demonstrate this further, this sample application (written in C# Express) shows the first three of the options in action.

    EnablePreventFocusChange (Default)

    Form1 uses the default action. If you enter only text in the box, you may click on the checkbox to change its state. If you enter any numbers in the textbox, then you may not click on the checkbox to alter its state. The Validating event is called when the control loses focus.

    Disable

    Form2 has the property set to Disable.

    The two textbox validation routines are only run when you click on the button.

    private void button1_Click(object sender, EventArgs e)
    {
      if (this.ValidateChildren())
      {
        MessageBox.Show("Validation succeeded!");
      }
      else
      {
        MessageBox.Show("Validation failed!");
      }
    }

    The ValidateChildren method returns a false if any control on a form has set CancelEventArgs to true, otherwise it returns true. This method may be overloaded, allowing you to specify what kind of controls should be validated, by passing a parameter containing bit fields of the ValidationConstraints enumeration. The members of the enumeration are:

  • Enabled - Validates child controls whose Enabled property is set to true.
  • ImmediateChildren - Validates child controls that are directly hosted within the container. Does not validate any of the children of these children. For example, if you have a Form that contains a custom UserControl, and the UserControl contains a Button, using ImmediateChildren will cause the Validating event of the UserControl to occur, but not the Validating event of the Button.
  • None - Validates all child controls, and all children of these child controls, regardless of their property settings.
  • Selectable - Validates child controls that can be selected.
  • TabStop - Validates child controls that have a TabStop value set, which means that the user can navigate to the control using the TAB key.
  • Visible - Validates child controls whose Visible property is set to true.

    EnableAllowFocusChange

    Form3 has the property set to EnableAllowFocusChange. It does not matter whether the validation has set CancelEventArgs to true, you may still click on the checkbox to change its state.

    References

  • AutoValidate Enumeration (MSDN)
  • ValidateChildren Method (MSDN)
  • User Input Validation in Windows Forms (MSDN)


    ContextMenuStrip

    The ContextMenuStrip property allows either a ContextMenuStrip or a ContextMenu to be associated with the form.

    The following image shows a form with a ContextMenuStrip having two menu items.

    To demonstrate this further, this sample application (written in C# Express) shows the context menu above.


    DoubleBuffered

    The default value is set to False.

    Every time you invalidate a form, Windows repaints it. First it clears the Form, and then runs your paint method. This can cause substantial on-screen flicker. Using buffered graphics can reduce or eliminate flicker that is caused by progressive redrawing of parts of a displayed surface. The DoubleBuffered property determines whether the Form should redraw its surface using a secondary buffer to reduce or prevent flicker.

    To demonstrate this further, this sample application (written in C# Express) shows the difference between using normal buffering and using double buffering.

    To change the DoubleBuffered property at run time, simply set the property to the desired value.

    this.DoubleBuffered = true;

    Enabled

    The default value is set to True. This property allows you to enable or disable controls at run time. Setting the Enabled property to false does not disable the application's control box or prevent the application window from receiving the focus. When a Form has its Enabled property set to false, all of its contained controls are disabled, as well. This is shown in the screen shot below.

    To change the Enabled property at run time, simply set the property to the desired value.

    this.Enabled = true;

    ImeMode

    The default value is set to NoControl. This property determines the Input Method Editor (IME) mode of the control. An Input Method Editor (IME) allows users to enter and edit Chinese, Japanese, and Korean characters.

    The choice of values for this property are:

  • Alpha - Alphanumeric single-byte characters(SBC). This setting is valid for Korean and Japanese IME only.
  • AlphaFull - Alphanumeric double-byte characters. This setting is valid for Korean and Japanese IME only.
  • Close
  • Disable - The IME is disabled. With this setting, the users cannot turn the IME on from the keyboard, and the IME floating window is hidden.
  • Hangul - Hangul SBC. This setting is valid for the Korean IME only.
  • HangulFull - Hangul DBC. This setting is valid for the Korean IME only.
  • Hiragana - Hiragana DBC. This setting is valid for the Japanese IME only.
  • Inherit - Inherits the IME mode of the parent control.
  • Katakana - Katakana DBC. This setting is valid for the Japanese IME only.
  • KatakanaHalf - Katakana SBC. This setting is valid for the Japanese IME only.
  • NoControl - None.
  • Off - The IME is off. This mode indicates that the IME is off, meaning that the object behaves the same as English entry mode. This setting is valid for Japanese, Simplified Chinese, and Traditional Chinese IME only.
  • On - The IME is on. This value indicates that the IME is on and characters specific to Chinese or Japanese can be entered. This setting is valid for Japanese, Simplified Chinese, and Traditional Chinese IME only.

    To change the Enabled property at run time, simply set the property to the desired value.

    this.ImeMode = ImeMode.AlphaFull;

    MSDN references

  • Control.AllowDrop Property
  • Form.AutoValidate Property
  • Control.ContextMenuStrip Property
  • Control.DoubleBuffered Property
  • Control.Enabled Property
  • Control.ImeMode Property


    << Previous Contents Next >>

    © Publicjoe, 2007