70-526 MCTSBehaviourWindows forms can behave in many ways. AllowDropThe 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; AutoValidateThe default value is set to EnablePreventFocusChange. This property overrides ContainerControl.AutoValidate. The choice of values for this property are: 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: 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 ContextMenuStripThe 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. DoubleBufferedThe 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; EnabledThe 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; ImeModeThe 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: To change the Enabled property at run time, simply set the property to the desired value. this.ImeMode = ImeMode.AlphaFull; MSDN references
|