win32exception error creating window handle.

Hello Experts:
Today  seems to be a bad day for me... my time and effort are running out.

Having this scenario:
There is three Forms, First one appears as DialogBox and hides the second form (a child form).. asks the user to enter a password and if its correct then the first form (the DialogBox one) will close and the second form will appeare as a child in the thired form which is MDI form.

the problem is when a valid passowrd is enterd an win32Exception thrown. and if an invalid password entered in the first form (or cancel was clicked) an exception will says "Value Dispose() cannot be called while doing CreateHandle().".

See the attached picture. which demonstrates the state.

Thanks in advance for any help persists here.
Saed Salman
101.jpg
LVL 6
SaedSalmanAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
I face this problem as well. The trouble is, when you try to call the close method of the form in the load method of the form, it contradicts with each other. Try showing the login form in the Shown method of the main form instead of the loading method.
0
 
CodeCruiserConnect With a Mentor Commented:
Another way is to load the login form only and if the login is correct then create the main form, instead just close the login form.
0
 
SaedSalmanAuthor Commented:
You are amazing CodeCruiser, this will solve the problem but I got ride of the problem already.
What I did is not to re-place login form to the main method instead of load method BUT I removed the call of loginForm.Hide() and loginForm.Show() in the child form.
here is a code snippet:
Initialize() method is called in the load method of the child form. Please take a look and give me your opinon, I already you will not like my silly programming style, this is the way I performe login phase :)

Note that the twice call of Form.Show() is reason of the problem:
I called Show() method in the child form which will display the login form then I called Show() method in the MDI Parent form which will display the child form. (Am I right? Why ?)
private void Initialize()
        {
            bool flag=false;
            try
            {
                //this.Hide();
                ConnectDB x = new ConnectDB();// create the login form
                DialogResult result = x.ShowDialog();
                if (result == DialogResult.OK)
                {
                    flag = true;
                    connstr = x.ToString();
                    con = new OracleConnection(connstr);
                    con.Open();
                    x.Close();
                    x.Dispose();
                    //this.Show();
                    FillFields();
                }
                else
                {
                    flag = false;
                    x.Close();
 
                    if (this.IsMdiChild)
                        this.MaximizeBox = false;
                    else
                        this.Dispose();
                    //Application.Exit();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "Loging in...",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if ((flag) && (con.State!=ConnectionState.Open))
                    Initialize();// recursion call
            }
        }

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
CodeCruiserCommented:
Does it work ok now?
0
 
SaedSalmanAuthor Commented:
Yes it is :)
0
 
CodeCruiserCommented:
The code seems ok.
0
 
SaedSalmanAuthor Commented:
Thank you for your time, Your help is much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.