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.
Related
Hello Everyone
In the following, I am going to give you a routh idea of what I am trying to accomplish with the program I am trying to code at the moment bevor describing the problem I am struggeling to solve. Thanks for your help!
The Program
Purpose
Timers. Multiple of them. Each timer should be displayed on an individual form, being controlled by individual buttons and having a unique design, once again set up by user settings.
Layout
The program itself should be made up by one main form, containing all "start" and "stop" buttons for each timer. However, the timer forms should be open at all time. Meaning, once the user has changed the properties, all timer forms should be closed again and depending on the settings new forms should open.
Question
In generell, I know how to open a form in such a way to change properties and componants from another form. Example:
namespace example
{
public partial class Form1 : Form
{
Form2 timerForm1;
private void Form1_Load(object sender, EventArgs e)
{
timerForm1 = new Form();
timerForm1.Show();
}
//now you are able to access Form2 in different functions, like:
{
timerForm1.label1.Text = "00:02:56";
}
}
}
However, I need to open as many forms as the user declares, meaning I can not set up variables in fore hand. However, I need to access those opend forms just like in the given example.
I would like to store the different, user declared timers inside an array or list if possible, if there is a smarter, easier or faster way i can adapt and change it to whatever is required to get it working.
I didn't really understand what or if you have an issue with the timers. But controlling various amount of forms from each individual is not that hard.
As I understand that you want to control various amount of Form2 instances from one Form1. In that case you can make a List of Form2 and add each Form2 you creat to that list.
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.
I want to make an application which has another screen in the extended screen in another monitor (or in my case projector), similar to the presenter view in power point.
I want to use C#, and I guess in windows form application or wpf template there is no dual screen application support.
My first guess is to make 2 separate project and connect them, but I'm not sure about it either.
Any idea how to do it, or at least someone has a tutorial related to it?
I've looked at google, but still not sure what the right question to the issue, since google always showed "how to set up dual screen" for users.
Please clarify anything if my question is unclear. Thanks.
The easiest method I can think of is to simply create a second form (assuming you use Window Forms for your project), so you'd have Form1 as the default, and Form2 as your second form.
Run Form1 by default, and then while loading Form1, do something like this:
if(System.Windows.Forms.Screen.AllScreens.Length > 1)
{
Form2 form2 = new Form2();
form2.Show();
}
Then within form2's shown event, set it's position to the second screen (and perhaps maximize it, depending on what you need).
private void Form2_Shown(object sender, EventArgs e)
{
this.Location = Screen.AllScreens[1].Bounds.Location;
this.WindowState = FormWindowState.Maximized;
}
From that point on (and provided you keep a reference to Form2 within Form1), you can easily exchange information between the two forms as needed. You could even expand it to support 3-4 monitors, simply by making multiple Form2's, one for each screen
I have 3 forms. How can I make it so that one form is shown with .Show() and the other is hidden with .Hide() from a separate form?
This is part of my code
private void buttonYes_Click(object sender, EventArgs e)
{
LoggedIn loggedinform = new LoggedIn();
loggedinform.Hide(); // Hides another form that is in the background
MainForm mainform = new MainForm();
mainform.Show(); // Show first form
this.Hide(); // Hides current form
}
One problem, the LoggedIn form does not hide itself. From the looks of it it skips it and just goes for the mainform.Show();
Is this a bug or do I need to do something else?
The line LoggedIn loggedinform = new LoggedIn() is going to create a new instance of that login window. That might be useful if, say, you intended to show 5 "Login" windows onscreen all at once. I think what you want to do is retrieve a reference to the login window that is already showing, and hide that; so, avoid creating a new one.
Properly passing references to existing objects around the program is kind of a structural problem, and one that I ran into quite a bit in my early programming days. The quick, unclean, and generally not-recommended way is to declare instances of those singular objects (like, maybe, your login window) as static, so they can be retrieved anywhere. However, to fully answer your question in the best way, maybe you could describe the structure of your program a bit more (full code isn't necessary, just generally-speaking, what the flow is between classes)
Ok I figured it out. I can use
Application.OpenForms[1].Hide();
[1] is the form I'm trying to hide. And it worked.
I also realized thanks to Katana that it makes sense why it wasn't working because it was basically making a new instance of the form instead of finding the current one. Sorry that my code is a mess.
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