• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5341
  • Last Modified:

C# GUI Programming

I need to preface that I am a Java programmer that is new to C#, and I am having difficulty finding information on GUI design.

My methodology for Java GUI programming is to subclass the JPanel class for each main component on a GUI display.  I create custom layouts for each of these subclassed JPanels and then attach them to the main JFrame using another custom layout.

I have already figured out the custom layouts in C# and the Form class.  What I am having difficulty with is subclassing Panel.  From what I can see, it looks like this needs to be created using a new UserControl.  Once created, change the superclass from UserControl to Panel.  I am not sure if this is right, but it is easy.  Once created though, I have no way of adding the new control to my Form.  By the looks of what I am able to find, I need to save this subclassed Panel to a separate dll and then reload it back in as a reference.  This means that I would need to split my application into at least 2 pieces (main .exe and subclassed Panel .dll).

Can some one please let me know if this is really how I need to do this or if there is a better way.  I would like to have everything (main application and all GUI components) in the main .exe file.  Also if there is a better way to subclass Panel, please let me know.  And lastly, if there is overall a better way of doing what I am trying to accomplish, keeping in mind I would like to use custom layouts, I would appreciate the information.

Thank you.
0
bmscholl
Asked:
bmscholl
  • 5
  • 4
  • 3
  • +1
1 Solution
 
Bob LearnedCommented:
1) What version of .NET are you using?

2) With 2005/2.0, there are TableLayoutPanel and FlowLayoutPanel to inherit from.

3) Creating a custom control is pretty easy:

public class SpecialPanel : System.Windows.Forms.Panel
{
}

4) Adding this control to a form is easy.

5) With 2005, when you build the project, you will get a ToolBox entry for the user control so that you can drag and drop the control on a form.

6) You could also add the panel at run-time with this.Controls.Add(panel);

Bob
0
 
DotNetThinkerCommented:
Wow, that's a pretty extensive question. Are you using Visual Studio for your development? If so, layout is pretty much drag and drop.
0
 
NicoCommented:
Well, I usually use a UserControl when I want to compose a static control made out of labels and textboxes and such using the designer. You don't really need to use a panel for that, unless you need some specific functionality from the Panel control. In Java everything is built from panels, but in .NET you have more choices :)

When you programmatically want to add a control to a panel (for instance) you add it to the Controls collection. Don't forget to give your control at least a size and a location.. (WinForms framework does offter a few Layout controls; we have a FlowLayout and a GridLayout.. but that's it, bascially.. and those actually inherit from Panel I think

Panel panel = new Panel();
panel.Location = new Point(0, 0);
panel.Size = new Size(100, 100);
yourForm.Controls.Add(panel);

should do the trick..

To keep all your controls 'in the exe' you just keep them in the same project. No problem at all.
You can also create seperate dll's and merge the them after you build them.. but that's a little advanced.. ahh the beauty of the .NET framework hehe.

Ah well, it's a bit of a messy post, but hopefully I gave a few pointers into the right direction :) Feel free to ask anything specific..
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NicoCommented:
(actually yourForm.Controls is a bit odd.. use this.Controls if you're working from the form)

(and I was assuming you were using Visual Studio 2005 btw :))
0
 
bmschollAuthor Commented:
Here is a bit more information.

1) I am using Visual Studio 2005.

2) I am unaware of any project/solution specific Toolbox.  When I create subclassed Panels and/or UserControls, I do not see nor can I find a specific Toolbox.

3) The runtime line: this.Controls.Add(mysubpanal); produces errors.  The errors state that the referenced object is not contained in the assembly.  This is why it seemed that the control needed to be added to a dll and referenced back into the project.  There should be a better way.

4) As for Drag-n-Drop, I prefer to create custom layouts so the GUI reacts the way I want it to when it is resized.  This appears quite clumsy with the standard way of doing things.

5) The reason I was looking at subclassing Panel was more for organization.  Put one group of controls in one Panel, another set in another Panel, and output in another Panel.  If there is a better way of doing this, I am all ears.

6) When I create a UserControl and change the superclass to Panel, the designer is blanked out.  This is not a problem, as I can add all subcomponents via code.  If I start from scratch with a blank class and subclass Panel, I do not even get the designer.

7) Currently all of my components are in the same project and even in the same Namespace, but I still get the errors when trying to add my own subclassed Panel.

Thank you all for the input.
0
 
Bob LearnedCommented:
1) Add a custom control

2) Build the project

3) The control should be added to a tab like "WindowsApplication1 Components".

4) Check the Tools | Options | Windows Forms Designer | AutoToolboxPopulate  setting and make sure it is true.

5) The ToolBox is context sensitive, so you need to have a form open in the designer view.

Bob
0
 
bmschollAuthor Commented:
I have checked my Toolbox options, and everything looks the way it should.  Even in the Form designer view, I do not see any of my UserControls, Custom Controls, or subclassed Panels.  I also do not see a tab labeled with my Windows application name.
0
 
Bob LearnedCommented:
Did you build the project/solution?

Bob
0
 
NicoCommented:
as to point 3) .. I'm curious about your error, since I've never seen anything like it =]

4: How would you expect the GUI to react when you resize it? .NET will take a lot of work from your hands if you use anchoring and such.

5: in Java you use JPanel for control grouping, in .NET you ordinarily use UserControls for your control grouping. You CAN use FlowLayoutPanel / GridPanel / Panel, but a lot of the layout logic will be in your own hands.

6: the designer is meant to be used with UserControls, that's the whole point :) for the rest you are right about the subclassing of panels and adding your own controls *nods vigorously*

7: as I said before, those errors seem strange to me. Something fishy is going on there :) google for the error and see what comes up?


0
 
bmschollAuthor Commented:
Ok.  That was clumsy.  I Built.  Cleaned.  Built again.  Then  it showed up.
0
 
bmschollAuthor Commented:
I appreciate all of the comments.  It appears that for now, just getting the controls to show up was the heart of the problem.  For that reason, I am going to accept the solution provided by TheLearnedOne.

As an aside, the docking mechanism in the designer actually seems fairly silly.  Kind of like in beginner Java, adding JPanels to JPanels using BorderLayout to get things kind of where you wanted them to be.  I prefer to create custom layouts and set the location and size (SetBounds method) on each object.  This, oddly enough, is very easily done in C# by overriding the LayoutEngine property (see this link for details: http://msdn2.microsoft.com/en-us/library/ms171727(VS.80).aspx).  It truly is that easy, and you have complete control of each item added to a Form/Panel/etc.  For instance you can move components in relation to each other with just a few lines of code instead of trying to figure out the docking/anchoring scenario.  Please let me know if you would like example code for this as I am happy to provide it.

Thank you all again for the input.
0
 
NicoCommented:
Hey, I never realized how straightforward creating a custom LayoutEngine is. This should be .NET 101 ffs ;)
0
 
bmschollAuthor Commented:
It is amazing how much control it gives you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now