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

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.

Related

Devexpress Winforms - Forms are fractured while transition one form to another

I am using Devexpress winform for my project. There are three forms simply. The first is MainForm that used MdiParent, the second is FormArticles that used listing articles about law into GridControl. And the last is FormArticleView that used viewing selected article into pdfViewer control. I managed to use documentManager and SplashScreenManager while loading Mdi Child forms and articles into one of Mdi Child form FormArticles. Here is my code:
public prjLibrary()
{
InitializeComponent();
var frm = new FormArticles{ MdiParent = this, Dock = DockStyle.Fill };
frm.Show();
}
While transition one form to another, the forms is fractured and after load it is fixed. Here is my screenshot:
And here is fixed view:
How can I fix fractured view while transition of forms?
This is because when the form from the first screenshot is getting focused, the controls have to be rendered in their Paint-event. This seems to take some time but you can see the fractured text is shown in rectangles where I think the underlying controls (radio buttons, text boxes, labels) are placed. So they are not rendered yet and not ready to go while any other call is blocking the thread. I think the problem is that you create a new form in your mainForm's constructor.
Anyway, it is a good practice to perform heavy tasks (which seem to block the painting of your controls) in a background thread having the UI waiting for the response. If this is too hard to do, try to do it after the UI is shown to the user. This could be the OnLoad- or even OnShown-event.
Note that I don't want to encourage you to write any business code into the UI layer but that seems not to be the question here.

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.

Best way to go about replacing forms?

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.

Dock Windows Forms (tabbed chat interface)

Edit for those who say to use tab control
I would love to use a tab control; yet i have no idea how to go about linking the tab control up from the main form. I would assume that I would have to do something like this:
Create Form with a blank TabControl on it, no pages created.
Create a CustomuserControl (Add -> user Control), with my controls on it.
When a new chat comes in, create a tab control Item, Tab Control Page, add the Custom Control to the Tab Control Page. Add the tab control handle to the hash table, so that when new messages come in, they can be referenced in the proper control.
But, i am so not sure how to do this. For example, I know that I can create custom events inside of the User Control, so that, for example, if each control has a 'bold' button, i can each page that has that control on it, to actually USE the button.
Yet i also need to register message callbacks, so that I can use a MessageGrabber to send data to it, and tha'ts not assigned inside of the UserControl, that's assigned programatically when a new window comes in; but since I have no controls to reference, i can't assign.
KISS Philosophy
Wouldn't it be easier to just create the form, like i do now, and then just dock that form within a window or something? So that, in essence, it's still creating the form, but it's also a separate window?
Original Question
Okay, so i'm stumped (which isn't that big of a surprise when it comes to complex C# logic lol)! What i'm trying to do is the following:
Goal: Setup tabbed chatting for new chat application.
Completed: Open new window whenever a chat message is received, or a user requests a new chat from the roster. This is working perfectly, and opens only a window when the user doesn't already have the chat open. Nice and happy there.
Problem: I dont want windows. Well, i do want A window, but, i do not want tons of separate windows. For example, our Customer Service team may have about 10 active IM windows going at one time, i do not want them to have to have 10 windows tiled there lol. I'd rather they have a single Private IM window, and all 10 tabs docked within the window.
Logic: This is my logic here, which may be flawed, i do apologize:
OnMessage: Open new chat window if one doesn't already exist; if one exists, open it as a tab within the current chat window.
SendMessage: ^^ ditto ^^
Code Examples:
if (!Util.ChatForms.ContainsKey(msg.From.Bare))
{
RosterNode rn = rosterControl1.GetRosterItem(msg.From);
string nick = msg.From.Bare;
if (rn != null)
nick = rn.Text;
frmChat f = new frmChat(msg.From, xmpp, nick);
f.Show();
f.IncomingMessage(msg);
return;
}
Note on above: The Util. function just keeps tracks of what windows are opened inside of a hashtable, that way, when messages come in, they route to the proper window. That is added with the:
Util.ChatForms.Add(m_Jid.Bare.ToLower(), this);
Command in the frmChat() form.
Library in Use: agsxmpp from: http://www.ag-software.de/agsxmpp-sdk/download/
Problem:
How can i convert this code to open inside of tabs, instead of windows? Can someone please give me some ideas, and help with that. I just can't seem to wrap my head around that concept.
Use TabControl

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