Solved

i can't close the form when textbox validation is there , what can i do now ??

Posted on 2011-03-07
6
505 Views
Last Modified: 2012-05-11
currently i am working on C#.net windows application ...
in my application i can't close the form when textbox validation is there , what can i do now ??

please refer the below code ...

private void Admin_Load(object sender, EventArgs e)
        {
            //Get Form Properties
            Form objform = this.FindForm();
            objGeneral.GetFormProperties(objform);

            pctBoxLogin2.Visible = false;
            lblmsg.Visible = false;
        }

        private void btnFlashObject_Click(object sender, EventArgs e)
        {
            //add constructor logic here ...
            
        }

        private void txtPasskey_TextChanged(object sender, EventArgs e)
        {
            //add constructor logic here ...
            Form objform = this.FindForm();
            pctBoxLogin2.Visible = false;
            objform.CausesValidation = true;
        }

        private void txtPasskey_Validating(object sender, CancelEventArgs e)
        {
            //add constructor logic here ...
            Form objform = this.FindForm();
            
            if (!ValidText(txtPasskey.Text))
            {
                // Cancel the event and select the text to be corrected by the user.
                objform.CausesValidation = false;
                e.Cancel = true;
                txtPasskey.Select(0, txtPasskey.Text.Length);
                pctBoxLogin2.Visible = true;
            }
        }

        public bool ValidText(string stringvalue)
        {
            if (stringvalue == "pILy^caffs")
            {
                return true;
            }
            return false;
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            //add some logic here
            if (txtPasskey.Text == "pILy^caffs")
            {
                Results objResults = new Results();
                objResults.Show();
            }
            else
            {
                lblmsg.Visible = true;
                lblmsg.Text = "Please Enter Valid Pass Key";
            }
        }

Open in new window


anything wrong on the above code ,
how can i close form when textbox validation is there ??
0
Comment
Question by:Parth48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 35066693
My guess would be that you need to set e.Cancel = false to ensure that the close isn't canceled. I think I recall reading somewhere that e.Cancel = true is actually the default. You have to specifically tell it that the close shouldn't be canceled. eg.

            if (!ValidText(txtPasskey.Text))
            {
                // Cancel the event and select the text to be corrected by the user.
                objform.CausesValidation = false;
                e.Cancel = true;
                txtPasskey.Select(0, txtPasskey.Text.Length);
                pctBoxLogin2.Visible = true;
            }
            else
                e.Cancel = false;

Open in new window

Try that anyway, see if it works.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35066903
First off, this code is pointless:

    Form objform = this.FindForm();

You're asking for the Form that contains "this"...which already is the Form!
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.findform(VS.80).aspx

    "Retrieves the form that the control is on."    

So everywhere you obtain the form using that construct you could simply use "this" directly.

Try this out to allow the form to be closed:

public partial class Admin_Load : Form
    {

        private bool UserClosing = false;

        public Admin_Load()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_CLOSE = 0xF060;

            if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
            {
                UserClosing = true;
            }

            base.WndProc(ref m);
        }

        private void txtPasskey_Validating(object sender, CancelEventArgs e)
        {
            if (!UserClosing && !ValidText(txtPasskey.Text))
            {
                e.Cancel = true;
                txtPasskey.SelectAll(); 
                pctBoxLogin2.Visible = true;
            }
        }

        public bool ValidText(string stringvalue)
        {
            if (stringvalue == "pILy^caffs")
            {
                return true;
            }
            return false;
        }

    }

Open in new window

0
 
LVL 1

Expert Comment

by:lalitgada
ID: 35067241
how could it be possible if set

 objform.CausesValidation = true;

check this code.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Parth48
ID: 35068572
hi @Idle_Mind: u r right , but coding is some complex , can u tell me the easy way ??

the below code i can't understand ....

protected override void WndProc(ref Message m)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_CLOSE = 0xF060;

            if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
            {
                UserClosing = true;
            }

            base.WndProc(ref m);
        }

Open in new window


please tell me the easy way ...
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35069421
One of the root problems is that the Validating() event for the TextBox fires BEFORE the FormClosing() event of the Form.  Overriding the WndProc() method allows us to trap messages for the Form before they get routed to the proper control.  This gives us the opportunity to set the flag so we know the form is closing in the Validating event.

The SC_CLOSE message is sent when the user clicks on the 'X' in the top right of the window, or if they Right click on the window in the TaskBar and select 'Close window'.

If you want to programmatically close the window from code then you'd need to set the boolean flag manually like this:

   this.UserClosing = true;
   this.Close();

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc.aspx

    "All messages are sent to the WndProc method after getting filtered through the PreProcessMessage method."
0
 

Author Comment

by:Parth48
ID: 35079180
Thanks @Idle_Mind: for the help .....
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net mvc 2 33
ASP.NET - Why is accordion not working? 3 46
SSIS GUID Variable 2 31
Json and ajax 1 19
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question