Well, when the last line of code above executes, it ends up resulting
in the New method of frmMain being called again. What am I doing wrong?
I'm not even sure where to start. First, I think that what you understood me
to be saying was not what I was saying at all. It can be difficult to
communicate by writing. What I was doing was explaining how .Net does
Windows Forms in an object-oriented way, and answering your questions. I
began by explaining that *understanding object-oriented programming* is the
single most important thing for you to do, and then went on to address each
question individually.
Your first question was
1) How do I declare these in the code for the Main form?
I explained how the Form is a class which contains other classes as members,
and that members (fields or properties, which I gave a simple example of)
are "global" within the scope of the Form, which is the main thread of the
application. What you wrote back was that you tried doing what you
understood me to be saying to do. Well, I didn't say to *do* anything, other
than to make a concerted effort to *understand* what you're doing before you
start to do it. I did explain and illustrate how fields and properties are
created. Your private field was a correct implementation of a class field.
But your public function was not a property at all. It did not have a get
method or a set method. It was a function that returns the private field.
Okay, in your first paragraph you mentioned that at design time the parent
doesn't exist because you assign it at run-time. Well, actually, nothing
exists until you assign it at run-time. What the designer does is write code
for you, which executes when the application runs. What you see at
design-time is an approximate visual representation of how these class
instances will appear after they are created at run-time. My point here is
that there is no difference whether Visual Studio writes the code or you do.
The rules for the code are still the same.
As an aside, you might benefit from taking a look at the code in the
InitializeComponent method called by your Form class at Design-time (as well
as at run-time). This should give you an idea of how to write good Form
control initialization code, which you will need for creating your Controls
and Forms later at run-time.
Another point here is that you should never say "I have the following code"
unless you can explain what it does. Programming is the process of writing a
set of instructions. The instructions are derived from a set of human
requirements, originally expressed as human ideas in human language.
Programming is the process of translating these humand ideas into
instructions, in a language that a computer can understand. The first
prerequisite for writing such instructions is, of course, planning what the
instructions should be in human ideas. So, when you show me some lines of
code you're having problems with, and do not tell me what they are supposed
to do, it is like showing me some Spanish that is not correctly translated,
but expecting me to know what it is supposed to mean. If I am very good at
what I do, I may be able to make an educated guess. But programming is not
guesswork, and it facilitates the communication process between us if you
can give me the ideas with the code. If you do not fully comprehend what
exactly you want to do, you shouldn't start writing code (until you have
settled that).
Now, in your first paragraph, again, there is something I do not understand:
what I'm doing is SHOWing user controls with
their parent being a panel on the main form. At design time, of course,
their parent/owner does not exist because I don't assign it until
runtime. Anyway...
There are several communication difficulties here. I understand that you do
not want to display the User Controls (which are Controls, like any other
System.Windows.Forms.Control) unless there is certain user action. What I do
not understand is your use of the term "parent/owner". A Control does not
have an Owner property. It has a Parent property, which is the Control which
contains it. This is achieved by adding the Control to the Controls
Collection of the Parent Control. Again, a look at the InitializeComponent
method (and all of the Designer code) generated by Visual Studio should
help. Let me just give you the basics:
1. A Control is declared as a field in a Form class, or as a variable in a
method.
2. The Control's properties are initialized, and the Control is
instantiated.
3. Another Control is declared in much the same way as number 1.
4. That other Control is initialized and instantiated.
5. That other Control is added to the first Control's Controls Collection.
In this way, the Form class represents a hierarchy of Controls contained
within Controls contained within Controls, and so on, with the Form itself
at the "root" of the "tree."
Controls can also be removed from the Controls Collection of other Controls.
This makes the Control disappear, which is not the same as either making it
invisible, or destroying/disposing it.
Does that help any?
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
A lifetime is made up of
Lots of short moments.