Best way to go about replacing forms? - c#

I'm trying to make an application where the users can click on the buttons to navigate through different forms. I.E: If I click on network status, it replaces the Main Form with a network status form, and then you can go back to the main form by clicking on "return".
What would be the best approach to do this? I'm thinking panels but people told me to use usercontrol and I'm not quite familiar with it. I'd appreciate any guidance.

Looking on the post don't see any reason for non having just another Form.
Why do not just create another, fully functional Form, and hide "source" Form.
Can create some, sort of forms linked list structure, where you have (say)
public class MyAppForm : Form
{
MyAppForm _prev...
MyAppForm _next...
}
In this way where (say) Go to next button clicked, we hide original, pick and show _next.
Just an idea. You may change it in a way to make it simplier or fit better your app needs.

Hi,
Assuming your requirement as depicted in the storyboard. Have a UserControl of these many controls which are DOCKED as per the picture on the UserControl. Now in the blank area you will have to show / hide your page / form / screen.
I would suggest this piece of code can do the trick for you to display / hide form dynamically on navigation. Have this in your navigation bit i.e. Next / Prev click on the Usercontrol -
form.Location = new Point(leftPaneControl.Width , BannerControl.Height);
form.Size = new Size(this.Width - leftPaneControl.Width, this.Height - BannerControl.Height);
form.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left |
AnchorStyles.Right;
this.Controls.Add(form);
On Next and Prev clicks , you just do Controls.RemoveByKey(...) to hide the existing form & the above code will add the next respective form after you have instantiated the same. This is a pretty cool function to remove control[s] from a collection if you have defined unique names for all your forms / screen / page [whatever you say].
This approach / design will allow you to just focus on designing your
pages / screens because creating this Usercontrol is a one-time
activity. After that you will only design your individual navigation pages / screens one by
one.

You could use several forms where ever needed and Show and Hide them as required.
You might also consider using something like MDI forms, where you can have one parent form that could host your menus, status and other bars.

Related

Showing a modeless form's 'title bar' inside the parent form when it's minimized

I'm trying to implement some complement views inside my application and I would like to have a better layout control over them. I don't know how to explain in words what my desired functionality is, so I made it through with some photoshop help, hoping you could give me a hand to implement it.
My application now looks like this:
(i need reputation to post images so.. sorry for the links)
http://i59.tinypic.com/2ikv8m1.jpg
When I minimize the modeless form which is focused in the previous image, I would like to be able to see it (and handle it to maximize or close) inside my main form as I show in the image below (made it in photoshop)
http://i58.tinypic.com/1e28go.jpg
Hope someone can lead my into a solution and thanks for the support.
EDIT: I need to be able to move that form outside my main form, even to a different monitor.
If you don't want to use the MDI approach, then set TopLevel of the modeless Form to false and add it to the main Forms Controls collection before showing it:
Form frm = new Form();
frm.TopLevel = false;
this.Controls.Add(frm);
frm.Show();
*Obviously changing Form to the correct type of your modeless form.
If i understand what you are trying to do, you want to minimize a certain form but still see it within your app (im assuming like Excel or Word)
You can do something similar to what Idle_Mind said, but enclose both in a Form instead of the parent.
Form fParent = new Form();
fParent.Dock = DockMode.Fill;//i think this is the syntax. Use this if you want the form to fill to the screen
Form fChild = new Form();
fChild.TopLevel = false;
fParent.Controls.Add(fChild);
fChild.Show();
Here, it should minimize to the lower left part of the parent form. You can then size the parent to whatever you want it to be.

Multiple forms one windows C# possible or only panels/usercontrol?

So here's my Question, I'm new to C#(teaching my self at that) Here's the thing, I'm working on a basic sim game, nothing to complex but I've got the design and basic functions done.
However In order to implement it, I'm currently using multiple Forms(Visual Studio 2013)
I have my "main" form which has the "action" buttons to it
So when i want to go to a user Profile page I have
Btn_profileview Click(object sender, EventArgs e){
Form profile = new Form();
profile.Show();
}
The User would then implement the changes(for instance change name) which is written to a text file, for use in other areas of the program.
However It opens a new windows, I've tried modal and nonmodal windows and while the benefit of Modal so they have to actual close the window solves the issue, i'd rather have it just overwrite the preexisting Form, and then on close go back to the "main" screen without actually using multiple windows.
Now I was told UserControl and/or Panel would solve the issue, but it would cause a complete redesign moving from the multiple forms to the multiple panel screens and figuring out how to get those to work(Visible and Invisible), i'm assuming it wouldn't be extremely difficult something along the lines of Panel"name".show(); and panel"name".close();
But would it be possible to actually add a line of code to the pre-existing code(so as not to cause a complete reesign) or are Panels and UserControl the only real way to implement within 1 continuous windows?
paqogomez is right: There are many ways to do it.
Here is one that combines a lot of the pros:
You create an invisible Tab on your window with as many pages as you need. Place a Panel on each tab and create all your controls on of them. This does not mean that you have to do it all over - you can move and drop the controls you already have without much hassle. Of course you need to turn off docking and maybe anchors, but other than that this is a simple process.
If you have controls on the 2nd form with the same name, these names should be changed to something unique though. I hope all Controls have proper names already, but especially Labels get neglected, at least here.. (With a little luck you can even use cut and paste to get Controls from another form to panel2!)
The big pro of this trick is that you can do all work in the designer of the same form. The Tab control serves only as a container where you keep your panels without adding to the UI and without giving the user control of what is shown.
Next you create one Panel variable in your main form:
Panel currentPanel;
On Load you assign the first 'real' Panel to it like this:
currentPanel = panel1;
this.Controls.Add(currentPanel);
Later, each time you want to switch, you re-assign the panels you need like this:
this.Controls.Remove(currentPanel);
currentPanel = panel2; // or whichever panel you want to show..
this.Controls.Add(currentPanel );
If your real panels are docked to fill the tabpage, as they should, the currentPanel will fill the form. You still have access to each panel and to each control by their names at any time but you see no overhead of tabs and your form never changes, except for the full content.

How to create navigation inside program Form

Warning! This is noob question probably! Sorry in advance.
I'm learning C# (using MS Studio 2013) and I'm having hard time creating some kind of decent navigation in simple desktop program.
Basically what I want is this: MenuStrip with options like "calculate something", "Calculate somethingelse"... and other (that I can easily add later - like dynamic menu on a webpage). If you click first option inside the Form connected with the StripMenu you will get some controls that allows you to do something(like inputs on a webpage). If you click the second all these options will disappear and you will get a fresh set of controls where you can do somethingelse (simply another webpage to play with).
What is the best way to do it (I find it amazing hard to find out :) ). Only way I figured out (more from experience in js then tutorials) is to use show/hide like in javascript/html.
ExamplePanel.Visible = false;
ExampleOtherPanel.Visible = true;
But this doesn't seem right - I think it would be impossible to manage in bigger program (not only in code, but visual designer too - you can only fit that much Panels inside Form).
Any advice? Or at least a link to material where I can find out?
EDIT:
Finaly I gave up and used multiple Forms as sugested in answer.
private void MenuStripExample_Click_1(object sender, EventArgs e)
{
SomeForm SomeForm = new SomeForm();
this.Hide(); //Hide the main form before showing the secondary
SomeForm.ShowDialog(); //Show secondary form, code execution stop until SomeForm is closed
//this.Show(); //You may uncomment this if you want to have the previous Form to get back after you close new one
}
You normaly don't hide and show panels with different layouts. This is not a good design.
If you have complete different navigations/control sets, then create a new Form which is responsible for the control set.
If you don't want to use new Forms take a look at the TabControl.
You may also want to take a look at MDI-Container. You can use a Form as a MDI-Container and display various other Forms as child-elements inside of this container.

c# Close a form opened from and instance class from the main form that created the class

I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?

Is their something like iframe in C#?

Is their something like iframe in c#?
I want to build an application with one form that its content changes according to the actions I do, like messenger live. The part when I log in and log out its too different "windows" but it happens in the same form.
There is no iframe equivalent in winforms or wpf, but there are ways to deal with it.
For either winforms or wpf, what you want to do is have a Panel which you change the content of.
A panel is a container which holds/encapsulates other controls.
If you have two different views you want to toggle between, create two panels at the same position with the content you need. Then you will show one and hide the other. When the user executes some action which requires you to change the view, then simply hide the displaying panel, and unhide/show the other one.
Think of it as layers, where you will only show one at a time.
You can also dynamically load user controls into a panel, much like an iframe, but I find it easier to have the content in the form, and hide/show as needed.
Assuming WinForms, how about using UserControls? Place as many as you want in a single/multiple forms, and interact as you do in forms. See, UserControl class.
You can load a form into a Panel or tabPage from a TabControl:
Form f = new Form();
f.TopLevel = false;
panel1.Controls.Add(f);
f.Show();
f.Dock = DockStyle.Fill;

Categories