473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

C# APP - Program design in single Windows Form

maxx233
32
Hi, I'm new to OO and GUI programming and would like to know: what's a good standard way of creating an application that loads different function-content in a standard space on a single form? In other words, I have a single form with a toolbar across the top. When someone clicks the "create new employee" icon I want the controls below the toolbar to change to whatever's needed to create a new employee record and add that to the DB. When they click the "View Report" icon I want that same space below the toolbar to change from the 'create employee' content to a report of current employees (or whatever.)

The only project I've done so far I had the icons set up to create and show a new Form whenever clicked. That worked for that app because the area below the toolbar had content loaded that was used very often and it was useful to have child windows. In this current app I'm building however, there's no one function I can foresee getting any more use than any other function, therefore I don't want a window with just a toolstrip that launches multiple other windows - it makes more sense to me to launch the function's content in the same area instead of in multiple windows, as there's also not really any specific use for child windows.

All I can think of to do this is putting my content into panels that are all positioned in the same space, and which I then programatically show and hide at runtime depending on which icons are clicked. Is that proper? Let me know how I *should* be doing this ;)

Maxx
Dec 6 '07 #1
10 4128
Plater
7,872 Expert 4TB
Some people would suggest that use MDI Children for this.
I have never liked that though.
As for your panels idea, you can use a TabControl and then have it NOT show it's tabbar at the top (thus mimicing what a whole series of Panels would do). Or you could have it show the TabBar and use that as how they switch.

What I've recently decided makes designing a bit easier is to create everything in seperate forms. And keeping in mind that you can add a Form object in the same way you would a Button or any other object, you can create/show and close any number of other forms.
Dec 6 '07 #2
maxx233
32
How do you add a whole form in the same way as a button? If I'm understanding right that sounds pretty ideal. All I can figure is instantiating a new instance of that form at runtime and showing it - but this pops up a *new* window, no? Is there a way to do it visually in VS05?

As far as MDI, I don't know much about it. I keep hearing it from google in reference to this sort of thing, but when I start looking into it I can't seem to make sense of the connection for how it applies to what I'm looking to do. I've seen it pop up enough that I'm certain I must be missing something important ;)

The tab idea also sounds great, if I'm not understanding your adding Form idea correctly then I think I'll end up going with that. Thanks!

Maxx

Some people would suggest that use MDI Children for this.
I have never liked that though.
As for your panels idea, you can use a TabControl and then have it NOT show it's tabbar at the top (thus mimicing what a whole series of Panels would do). Or you could have it show the TabBar and use that as how they switch.

What I've recently decided makes designing a bit easier is to create everything in seperate forms. And keeping in mind that you can add a Form object in the same way you would a Button or any other object, you can create/show and close any number of other forms.
Dec 6 '07 #3
Plater
7,872 Expert 4TB
Consider this:
Expand|Select|Wrap|Line Numbers
  1. SomeForm myform = new SomeForm();
  2. //set the formborder style = none
  3. //set myform.Location to whereever you want the topleft corner to be on the main form
  4. this.Controls.Add(myform);
  5.  
or in vb
Expand|Select|Wrap|Line Numbers
  1. Dim myform As New SomeForm() 
  2. 'set the formborder style = none 
  3. 'set myform.Location to whereever you want the topleft corner to be on the main form 
  4. Me.Controls.Add(myform) 
  5.  
Dec 6 '07 #4
maxx233
32
I tried the C# example on the button_click event (when someone clicks the icon I have in the toolbar) and it throws an error when clicked:

ArgumentException was unhandled
Top-level control cannot be added to a control.

I then thought maybe it's because I shouldn't have it instantiating the form on the click event, but on form initialization instead. That makes more sense anyway I think, right - then just do a show/hide like you said, whereas if I had it instantiate on my button_click then it's not scoped so it can be hidden when the next icon's clicked, so instead each time a new form would be created. But ANYWHO, it doesn't work on initialization because of the same error anyway ;)

Any ideas? I'd really like to use this idea, it sounds great. Just don't know what I'm doing wrong with it. Thanks a lot!

Maxx



Consider this:
Expand|Select|Wrap|Line Numbers
  1. SomeForm myform = new SomeForm();
  2. //set the formborder style = none
  3. //set myform.Location to whereever you want the topleft corner to be on the main form
  4. this.Controls.Add(myform);
  5.  
or in vb
Expand|Select|Wrap|Line Numbers
  1. Dim myform As New SomeForm() 
  2. 'set the formborder style = none 
  3. 'set myform.Location to whereever you want the topleft corner to be on the main form 
  4. Me.Controls.Add(myform) 
  5.  
Dec 6 '07 #5
Plater
7,872 Expert 4TB
You aren't trying to add an instance of the SAME form as the one you're running in are you?
Dec 6 '07 #6
maxx233
32
Nope. I just made a sample app... Form1 and Form2. Form1 has a Toolstrip across the top with one button:

public partial class Form1 : Form
{
public Form1() {...}

private void toolStripButton2_Click(object sender, EventArgs e)
{
Form2 test = new Form2();
test.FormBorderStyle = FormBorderStyle.None;
test.Top = 50;
test.Left = 0;
this.Controls.Add(test);
}
}

Maxx

You aren't trying to add an instance of the SAME form as the one you're running in are you?
Dec 6 '07 #7
Plater
7,872 Expert 4TB
Yeah I am getting the same thing.
I don't remember where I put the project where I was doing this.
I read it online and went "wow that is really neat" and tested it out, but now I cannot find anything about it.

EDIT:
Hurray for being able to search for words inside files!

Expand|Select|Wrap|Line Numbers
  1. // Add Form2 instance 
  2. SomeForm f2 = new SomeForm ();
  3. f2.FormBorderStyle = FormBorderStyle.None;
  4. f2.TopLevel = false;
  5. f2.Location = new Point(0, 0);
  6. f2.Visible = true;
  7. this.Controls.Add(f2);
  8.  
Dec 6 '07 #8
maxx233
32
YAY!! Thanks so much, it works great. So now I suppose my only question is, should I put the code to instantiate Form2 into the button_click event, or on form initialization? Form initialization makes a lot more sense to me, and then just show and hide Form2 when needed - but I don't know how I'd reference it? Is it proper to instantiate a whole form in the same way I'd instantiate a global variable, that way it's scoped for all my icon_click events to show or hide as needed? Or if it's better to keep the instantiation in the icon_click event, then does it still get disposed ever, or do new forms just keep getting instantiated anytime the button's clicked? Also to the user it would appear that clicking the "new employee" icon, filling in the fields, then accidentally hitting the "new employee" icon again would clear the form (because a new form would appear over the one they'd filled out), no? I'm curious how you've implemented in the past, I definitely like the idea. Thanks a lot for all your help, it's very much appreciated!

Maxx


Yeah I am getting the same thing.
I don't remember where I put the project where I was doing this.
I read it online and went "wow that is really neat" and tested it out, but now I cannot find anything about it.

EDIT:
Hurray for being able to search for words inside files!

Expand|Select|Wrap|Line Numbers
  1. // Add Form2 instance 
  2. SomeForm f2 = new SomeForm ();
  3. f2.FormBorderStyle = FormBorderStyle.None;
  4. f2.TopLevel = false;
  5. f2.Location = new Point(0, 0);
  6. f2.Visible = true;
  7. this.Controls.Add(f2);
  8.  
Dec 6 '07 #9
Plater
7,872 Expert 4TB
If you instanciate it in the button click, you will keep making more and more.
Better to insanciate and populate in the form load.
Actually:
Expand|Select|Wrap|Line Numbers
  1. private SomeForm myform = new SomeForm();
  2.  
  3. private void Form_Load(object sender, EventArgs e)
  4. {
  5. //do the stuff to set myform properties and add it to the controls
  6. }
  7.  
that way in your button clicks you can then go
Expand|Select|Wrap|Line Numbers
  1. myform.Visible=false;
  2. //myform.Visible=true;
  3.  
Dec 6 '07 #10
maxx233
32
Great! That's exactly how I was thinking to do it as well. Again, thanks so much, very helpful :)

Maxx

If you instanciate it in the button click, you will keep making more and more.
Better to insanciate and populate in the form load.
Actually:
Expand|Select|Wrap|Line Numbers
  1. private SomeForm myform = new SomeForm();
  2.  
  3. private void Form_Load(object sender, EventArgs e)
  4. {
  5. //do the stuff to set myform properties and add it to the controls
  6. }
  7.  
that way in your button clicks you can then go
Expand|Select|Wrap|Line Numbers
  1. myform.Visible=false;
  2. //myform.Visible=true;
  3.  
Dec 6 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
6
by: Alice | last post by:
Hi -- I am trying to code a "mad libs" style program, in which the user puts in nouns, adjectives, etc., into text boxes. Then the user clicks on a button which opens a message box. Inside the...
1
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following...
11
by: Peter M. | last post by:
Hi all, I'm currently designing an n-tier application and have some doubts about my design. I have created a Data Access layer which connects to the database (SQL Server) and performs Select,...
27
by: cj | last post by:
I run this program and to exit click the X in the upper right corner. But apparently it isn't really ending the program. If I return to VB and make changes then try to rebuild the app it says the...
23
by: JoeC | last post by:
I am a self taught programmer and I have figured out most syntax but desigining my programs is a challenge. I realize that there are many ways to design a program but what are some good rules to...
0
by: vve | last post by:
I'm discovering a strange behaviour in an C# project using ZedGraph (https://sourceforge.net/projects/zedgraph/). After adding a signal to it, it seems that the clr goes mad for some reason. I...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.