Click to See Complete Forum and Search --> : Problems with winforms in VS 2005 C#
green_rob
February 13th, 2009, 01:16 PM
I am having problems with adding forms to my application. I select add windows form in the project menu, and then add a form using the windows form template. After doing this, I get an object not set to an instance error on the form designer tab, and if I build the solution, then quit and restart VS a random number of times, I have a place to build my form design.
Now, I am also trying to create some inherited forms from this initial form, and when I add windows form, select inherited form, select the correct form to inherit from, when I go to the design tabs there is a tiny little circle in the upper left hand corner that I can grab and move, but if I let it go, it just goes right back to the corner. None of the things that I wanted to inherit are present.
Can anyone tell me what I am doing wrong?
thanks,
rob
BigEd781
February 13th, 2009, 01:31 PM
Sometimes the designer gets a bit muddled. If you look in that file, you should have:
1. A declaration of each object.
2. The instantiation of each object in the InitializeComponent method.
3. Property setting areas for each object, also in InitializeComponent.
A good thing to do as a beginner is to study this file. You could do this yourself, the IDE just takes care of it for you. You are really just creating objects and setting their properties. Did you erase the InitializeComponent() call from your form's constructor?
As for the inheritance issue, if you have controls on you base class form and then inherit from it, you cannot drag those controls around in the designer. You can move them around at runtime, but if you need to you probably should not be inheriting from that form at all. You could create an abstract base class that defines the common logic and then inherit each form class from that one. If this does not solve your problem, it would help if you could post the actual line of code throwing the error.
green_rob
February 17th, 2009, 09:08 AM
OK, I looked at the source code. The form that I am inheriting from is just an empty form with some of the properties already set (like size and color), there are no controls on the forms. I added 8 of these inherited forms, and only one of them is visible. I compared the .Designer.cs files and they all look the same. The thing I did notice is that for the one that shows up, there is a .resx file available in the solution explorer, and there is not one for any of the other forms.
One last detail, I have an evaluation copy of Code Rush and Refactor! installed. Are these likely to have interfered with the prolper creation of the form?
thanks for any help,
rob
green_rob
February 17th, 2009, 09:35 AM
Well, I noticed one other thing: some of the inherited forms are not missing the .resx files even though they are not behaving correctly.
this is very confusing.
rob
green_rob
February 17th, 2009, 11:04 AM
yet another detail. It seems that the size property is not being inherited correctly. The size of all new inherited forms is 0,0. I cannot seem to figure out how to get it to inherit the size properly, and resetting the line in the InitializeCopmponent method to:
this.ClientSize = new System.Drawing.Size(1726, 932);
does not have the desired effect.
Not to mention I would not want to have to do this to every form I create, isn't that why we have inheritance?
BigEd781
February 17th, 2009, 03:54 PM
Not to mention I would not want to have to do this to every form I create, isn't that why we have inheritance?
It does, but you probably have the size set in two places. Your InitializeComponent method runs in your base class first, and then it runs in your inherited form, so the initial value gets wiped out. Also, inheritance is meant more for extension than modification. If you need to modify a lot of things in the base class then was it really the correct choice to use inheritance? You would be better of using composition in that case. I should note that changing the size of a form does not fall into this category.
green_rob
February 18th, 2009, 08:38 AM
I don't know how I could have the size set in 2 places.
This is how I am using the form: I set a base form to inherit from that sets the default initial size for design purposes, with the appropriate colorings and background images, and other things that are reused across forms. I then use that form to inherit these initial settings to create functional specific forms where I add buttons, gridviews, etc.
This is the part I am confused about. I have repeated the problem agian yesterday. Using the same project where I am having problems, I create a new base form to inherit from, and set all of its properties. This form shows up correctly in the designer. I then chose to create a new inherited form, inheriting from this base form, and it shows up as size 0,0, and I cannot seem to get this value to change. I am not altering any code on the designer.cs page, or anywhere else that should be causing this.
I have a total of 8 forms that will be inheriting my base form. The first one I set up last week works correctly, and if I change the form it inherits from to the new form, it changes correctly. All newly created forms from last week (the other 7) and any new form I create, do not work correctly. Since these forms are child forms to a main form, and the main form involved a lot of setup and fine tweaking, I would hate to scrap it all and restart. Especially since there is no guarantee this won't keep happening. So I really need to figure out how to make this work.
thanks for your help thus far.
green_rob
February 18th, 2009, 12:52 PM
OK, I stripped down the project to just some code for the forms. You can download it at www.robkeller.com
It is the only link at that website.
If anyone can open that project in VS2005 and inherit from either Form_base or Form_base_inherit, and then tell me what they are doing to make it happen, I would be very grateful. If you can at least replicate my problem, I would also be very grateful.
green_rob
February 19th, 2009, 03:34 PM
OK, problem solved. Apparently the property to allow gorw and shrink was set, and this caused it to shrink to 0,0 in the designer. I re-set it to grow only, and now it is fine.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.