Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2005, 12:20 PM
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem of Validating and CausesValidation


With a great hope I write you this letter, for a problem, that I have with the control validating.
The problem concern the example in the in the Chapter 13, the Title "The Textbox Control", and subtitle "Textbox Events", where we have the explications of the events of the textbox and how use them. After this subtitle "Textbox Events", in the book I have an example with copy of screens in design mode and the examples of code for textbox event "Validating". Just in this example (I write the code of the book here) I have the proposition of using "e.Cancel = true".


The code :

//////////////////////////////////////////////////////

private void txtBoxEmpty_Validating(object sender,
                                   System.ComponentModel.CancelEventArgs e)
{
  // We know the sender is a TextBox, so we cast the sender object to that
  TextBox tb = (TextBox)sender;

  // If the text is empty we set the background color of the
  // Textbox to red to indicate a problem. We use the tag value
  // of the control to indicate if the control contains valid
  // information.
  if (tb.Text.Length == 0)
  {
     tb.BackColor = Color.Red;
     tb.Tag = false;

     // In this case we do not want to cancel further processing,
     // but if we had wanted to do this, we would have added this line:
     // e.Cancel = true;
  }
  else
  {
     tb.BackColor = System.Drawing.SystemColors.Window;
     tb.Tag = true;
  }

  // Finally, we call ValidateAll which will set the value of
  // the OK button.
  ValidateAll();
}


//////////////////////////////////////////////////////


In my version of the book it is on the page 348 (“Beginning C#” – 2001 Wrox Press ISBN 1861004982), in the other version it may be on the page 358.

Finally when I do this modification proposed by You, I can’t close the form with the Top Right X button, it fires the event Validating of the Textbox. Even when I change the property CausesValidation of the form to false, it’s no effect.

I hope that this problem is not a bug of the software, and that you can find the solution.

Thanks.


 
Old June 2nd, 2005, 09:43 AM
sk sk is offline
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Problem:
  "...Validating Event and Sticky Forms the Validating event also fires when you close
  a form. If you set the Cancel property of the CancelEventArgs object to true inside
  this event, it will cancel the close operation as well. "..
  (Quoted from: http://www.examcram2.com/articles/ar...30936&seqNum=6)

  The closing operation can be conducted in one of three ways:
  Either 1) Clicking a cancel button, 2) Window close button or 3) Escape key.


Solution:
 We now show how to stop the validation from occuring in case of canceling the operation. We
 refer to each of the possible operations specified above.


1. Cancel Button:
  Simply set the CausesValidation of the cancel button to false . Thus, every control that
  is assigned to a validating event will not be called.

2. Window close button:
  Need to override the wndProc method, check the window msg and if the last equals WN_CLOSE, and define it somewhere (it is not automatically defined) ,
  set the CausesValidation of the active control to false.
  e.g.
  protected override void WndProc(ref Message m)
  {
 if (m.Msg == WM_CLOSE)
 {
  ActiveControl.CausesValidation = false;
        }
        base.wndProc (m);
  }

3. Escape key:
 Derive a class from System.Windows.Forms.Button where you override the IButtonControl.PerformClick
  method.
  e.g.

  public class CancelButton : Button, IButtonControl
  {
  void IButtonControl.PerformClick()
 {
  this.Parent.ActiveControl.CausesValidation = false;
  base.PerformClick();

 }
  }
and use it as your form's cancel button

This about it. Do hope was of help.









Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with validating vanila XML with XSD joshuaa XML 6 August 21st, 2008 01:42 PM
problem with validating sax parser vijay3000 XML 1 April 18th, 2006 10:15 AM
No CausesValidation for CheckBox component koekie17 C# 0 March 9th, 2006 09:49 AM
Validating XML sunilyenpure XML 3 July 13th, 2004 12:11 AM
Validating users rboyle General .NET 1 March 16th, 2004 01:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.