Hi!,

I get InvalidCastException.
It says "Unable to cast object of type 'HD.MainMenu' to type 'HD.NewBookEntry'" and points to this statment "mpg=(NewBookEntry)fm"
Plz help me to solve this.
My code is:

FormCollection fms=Application.OpenForms;
            foreach(Form fm in fms)
            {
                mpg=(NewBookEntry)fm;
            }
            mpg.Dispose();

Form has a public Dispose() method. Why are you trying to cast between the two types? Assuming that both classes derive from Form, HD.MainMenu and HD.NewBookEntry are both ancestors of Form, and have common variables/methods from Form. However, they define their own variables/methods, therefore they lack a commonality between them.

I figured it out.

Form2 nbe = (Form2)Application.OpenForms["Form2"];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.