[.net] C# - Panels and Forms

Started by
7 comments, last by Headkaze 15 years, 5 months ago
Okay, well I've got a homework assignment and I'm spending more time fighting with C# than actually working on the homework. A small bit of background... I've barely used C#. The first time I used it was for my first assignment, and I'm now on the second. (I do have a history with C++, however.) So I decided to stuff all of my assignments into one application, rather than writing seperate apps for everything. The best way I saw to do with was to move everything from my first assignment into a seperate form, and then set up a framework form with a panel that I could load the other forms into. (All these forms were created through the GUI, btw.) The idea is that I just set up seperate forms for each assignment and swap them in and out of the panel in the framework. The problem is that I get a compile error when I try to load the form into the panel. It considers my "Assignment1" form a class, so I get a "'ImageApp.Assignment1' is a 'type' but is used like a 'variable'" error. Conversely, I can create a new form through code, and that works, but I'm trying to use the existing form I created through the GUI. What am I missing? Code is nothing fancy, just something I read online. And now that I look at it, it doesn't even really make any sense. Is a form a type of control?

panel.Controls.Add(Assignment1);
Also, if there's a better way to do this, let me know. I'm not familiar with all of C#'s controls.
Advertisement
If Assignment1 is the name of the class that's your problem. You have to create an instance of the class:

Assignment1 frm = new Assignment1();

Besides that, you can't add an instance of a form to a Panel. You could make the forms you're trying to add to the panels user controls, copy the controls on the forms to the user controls and add instances of the user controls to the panels.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Everything you see on a form inherets from the superclass Control. A panel is a Control, a button is a Control etc.

Panel.Controls is a collection of controls that are contained inside the Panel. You should not place a Form inside a Panel.

If Assignment1 is the name of a Form, then yes you can't use that directly because it is a class. You have to create an object (instanciate) the Assignment1 class.

So here is an example of instanciating a form

Assignment1 myAssignment1 = new Assignment1();
myAssignment1.ShowDialog();

So lets look at this Assignment1 is the class, myAssignment1 is the object of type Assignment1 and the "new Assignment1()" instanciates the object.

Now we have an object we then call it's ShowDialog() method and the form shows.
Oh... duh. That seems so obvious now. Clumsy on my part. :P

Anyway, I got it compiling but it's still not showing the form. I'm not trying to do what Headkaze mentioned with the modal dialog, I'm actually trying to embed it inside the panel. And I'm getting nothing. Is there some kind of visibility flag I'm missing?

Assignment1 form = new Assignment1();form.TopLevel = false;panel.Controls.Add(form);
As Machaira mentioned I don't think you can add a Form as a child of another Control. Try changing Assignment1 to a UserControl and see if that works; UserControl objects can be children of other Controls.

Yeah, that did it. I had never heard of a UserControl before, but it's exactly what I needed. It's working now.

Thanks everyone!
Quote:Original post by mutex
As Machaira mentioned I don't think you can add a Form as a child of another Control. Try changing Assignment1 to a UserControl and see if that works; UserControl objects can be children of other Controls.


Actually you can. You have to manually set up the parent of the form with the panel you want to add the form into. As far as I can tell, it works. We need to use this in our project at work because we have to show forms that must be loaded from an exe. We didn't already do that (scheduled in a month or so), but I've done some simple tests and it seems to work.

That aside, I wonder why the OP didn't get an error when it tried to add the form to Controls...

Anyway, using UserControls is the correct way to do the job.
i was stuck wanting to layout elements in my panel using visual studio. which sucks because the layout is messed up and the elements do not show correctly. they're just icons. luckily, you mentioned usercontrol and it was just what i needed. the maker probably tried hard to be unique not wanting panel to be just an ordinary container. geez.. i wasted 2 hrs for this
http://www.chitgoks.comhttp://tech.chitgoks.com
If you really wanted to you probably could have a form in panel by using the SetParent() Win32 API function. So if you p/invoke'd it you could probably do it.

This might work but I haven't tried it.

Win32.SetParent(form1.Handle, panel1.Handle);

The way I see it the OP could have just used a tab control and had each assignment with it's own tab. Or just have a main menu that you select the assignment and it will open the form for that assignment.

This topic is closed to new replies.

Advertisement