C# Multiple GUIs With One Window in Window Form [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My friend and I are trying to work on a text game using Visual Studios Community. As of now, we have started our project in windows form. One thing we are stuck on is being able to design multiple screens but using only one window. As of right now, the way we have it designed is after you click "Start Game" on the first window, it pops open a second window to the character select screen. Once you select a character, it opens a third window.
What we would rather do is be able to design the GUI to display a basic opening splash screen and clicking on "Start Game" would bring up a new "screen" but in the same window. The new screen should have it's own unique GUI from the initial splash screen. Also part of the game, we are going to want to put a pause menu with options. When the user clicks on the pause button, that should bring up a new "screen", again with it's own unique GUI from the main screen you would see during the game.
Is it possible to create multiple GUIs but only using one window in window form? If not, how could we make something like that happen?
Thanks in advance!

You have to use UserControl in this case. A UserControl can be set up as a whole form, then you simply swap the UserControls that you have created.
In visual studio create a UserControl item, put your user interface in them, basically very similar to designing a normal Form you just put buttons, labels and other stuff on it and wire up events and logics and you are ready to go.
You propably need to implement a global logic or business model to handle or pass the events of each usercontrol you are creating to have a unified model accross your application.
Here is a good tutorial on using UserControl
You can also apply transition animations while swapping between different controls, anyway if you google these stuff up you will find plenty of useful data.

You can create a wizard like control which can have a custom TabControl. Each tab page can then have different controls you would like to place. You can also create user controls and add them to tab pages which would make it a bit easy to maintain.
Customizing a tab page:
public class CustomWizard : TabControl
{
protected override void WndProc(ref Message m)
{
// Second condition is to keep tab pages visible in design mode
if (m.Msg == 0x1328 && !DesignMode)
{
m.Result = (IntPtr)1;
}
else
{
base.WndProc(ref m);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Tab)
return;
base.OnKeyDown(e);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
You can then use this tab in your form and have next and back buttons. Handle the click event of these buttons to move back and forth.

Related

C#/WPF: Closing the current window without closing the whole project [duplicate]

This question already has answers here:
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application
(3 answers)
Closed 6 years ago.
I have a project which acts as a boardgame. In this board game, I have multiple mini-games that the user can play (memory game, tic-tac-toe, minesweeper.) My problem is how to not close down the whole project when asking the user if he wants to start a new game so that he can play the other games.
I have the board game window as the base window, inside that window I have the grids for the games.
For example, when I win the tic-tac-toe a message box pops up asking if I want to start a new game with options yes and no. However, If I choose no, it will close the whole project instead of just stopping to play the tic-tac-toe.
in the tic-tac-toe.cs I have this method for closing the tic-tac-toe game:
internal void newGame(string message)
{
if(MessageBox.Show("Play Again?", message, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Clear(); // calls the method to clear buttons etc.
}
else
{
//Application.Current.MainWindow.Close();
//Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}
}
As you can see both lines in Else are commented as they do close down the whole project instead of just the tic-tac-toe game.
You can override the main window's window closing event:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//You can ask users to play new games, if they click close window (etc.)
e.Cancel = true; //This will cancel the closing of main window.
//You can now write logic to close your local windows (dispose objects of games) and show the main window.
}

Display data on same page or separate page? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on my first mobile app. I'm getting input from the User and having them click a button before retrieving data into a dataset. All that is working well but getting the data displayed has stopped me in my tracks. Can/Should I display it in the same page or create a 2nd one? How do I call a second page from my c# code? I don't know best practices for Xamarin and mobile so any assistance or direction would be helpful.
There are multiple ways of doing this and reasons for doing it, it all depends on your app. I assume you're not using xamarin forms, so I'll give default android/iOS answers.
Can/Should I display it in the same page?
If you're making the usual search control it helps displaying the search results in the same page as the one you are typing in.
If you have a list of items, where selecting one shows more details, then the usual way of doing it is to navigate to a new page that displays the information. So you'll have to create another page.
There are multiple ways of doing this it all depends on your app.
How do I call a second page from my c# code?
You don't call the second page from your first page, you rather launch the page and pass the information you want.
Android: You'll create a new Activity with it's layout file and use an intent to launch the activity from your Action (clicking a button in your case).
You can use this tutorial to get up to speed with miltiscreen Xamarin applications for android. Your code should look similar to the following.
Button yourButton = FindViewById<Button> (Resource.Id.YourButtonId);
yourButton.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(SecondActivity));
StartActivity(intent);
};
To pass information to the second page you can use one of the intent.PutExtra() methods. Similar to
intent.PutStringArrayListExtra("YourKey", myList);
Which passes a list to the launching activity through the intent, storing the value under YourKey. In your SecondActivity you can get the data from your intent using
intent.GetExtra("YourKey")
iOS: Not much code involved so I'll list the steps to do this briefly. You can use this tutorial to get started with multiscreen iOS applications.
You'll create a new controller in your Main storyboard, using Interface Builder or Xamarin Studio.
On your first view, place your button.
press Ctrl and drag it to the new ViewController. On releasing your click select Show (push) segue.
In your FirstViewController override prepareForSegue and set the data you want to use in your SecondViewController.
Something like below.
public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue (segue, sender);
// set the View Controller that’s powering the screen we’re
// transitioning to
var secondViewController = segue.DestinationViewController as SecondViewController;
secondViewController.YourData = "Your data you want to pass over";
}
Let me know if this answers your question. Goodluck.

C# Screen Changing [duplicate]

This question already has answers here:
change active form to show another form
(4 answers)
Closed 9 years ago.
Is there a way to make it so that, when I click a button, instead of needing to bring up a new dialog for my next screen, it just 'replaces' what's on the screen? (For instance, the default screen is a main menu with some buttons, which lead to more in depth screens, like Parts Inventory, and all of it's options now being on the screen).
In the realm of stacking panels, how efficient is this for a larger system? I need at least 30-40 screens going on.
Edit: I'm using Visual Studio 2010 to do this project for school, and using Windows 7 with the default packages.
Instead of creating a new forms for each screen you can create a new user control for each screen. Then you could have one form with a panel and loading the control into the form instead of creating a new form showing it and hiding the old one. Youcould do something like this:
...
MyControlWasForm1 form1 = new MycontrolWasForm1(); // this is a user control
panel1.Controls.Clear();
panel1.Controls.Add(form1);
...
You could also keep references to the old control if you want to be able to go back then just reload it into the panel.Controls when needed. Keep in mind if you don't have a variable referencing the control outside of the method, then when you remove it from the controls it would get garbage collected.
...
//property on main Form
public MyControlWasForm1 Form1 { get; set; }
...
//in method
if(Form1 == null) Form1 = new MycontrolWasForm1(); // this is a user control
panel1.Controls.Clear();
panel1.Controls.Add(Form1);
...
You can use multiple panels stacked and show/hide them depending on what you want to display

Desktop applications dynamic GUI problem

I have asked the question "Is there something like master page in desktop applications?" Now I am in position that I have to extend the question. Thanks for understanding.
I have add one MDI master form into my project and several inherited forms that inherit MDI master one. I was using this code.
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form child in this.MdiChildren)
{
child.Close();
}
Search childSearchForm = new Search();
childSearchForm.MdiParent = this;
childSearchForm.Text = "Search ";
childSearchForm.Show();
}
This code is triggered when I press some button on master form and the new in this case Search form is opened inside master.
Now my question is the right way to build desktop applications or there is some other more elegant way where content of user interface can be dynamic and switch from view to view by clicking on the buttons inside. For instance clicking on "Search" button on some search form will take you to search results grid, all that happening in one master form.
And if this is right way (which I doubt) how can I achieve to open other inside forms by clicking on buttons inside them. Also if I put some controls on masterpage they will appear two times in master form and in inherited form.
Thanks.
PS
I am using Visual Studio 2008 and MS SQL 2005.
If you are only wanting to show one view at a time you could create a new user control for each view you require. e.g One for your search results.
Then you could add a panel and clear the controls contained within the panel and add the new one to display the view you are after.
Or as described, the other option is to use a tab control, hide the tabs and set the visible index programatically.
Not sure how you would easily do this in winforms, but in WPF you could create a navigation app - a link driven navigation similar to a web browser experience but still a stand alone application. See http://msdn.microsoft.com/en-us/rampup/cc514215.aspx for details of how to get started.

Resetting a Windows Forms form's elements to an initialized state (C#/.NET) [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 14 years ago.
Improve this question
I'm building a Windows Forms form in C# with various elements in a panel that starts out either invisible, disabled, or set to null (labels, combo boxes, grids, etc.). As the user goes through and makes choices, these elements are populated, selected, etc.
The idea is to upload files, read them, and process entries to a database. Once the processing for this directory has completed, I'd like to be able to have the user select another directory without exiting and restarting the Windows Forms application, by pressing a button that becomes visible when the process has completed.
Is there an easy call to reset the application (or the panel that contains the elements), similar to when a webform is refreshed, or do I have to write a function that "resets" all of those elements one at a time?
As the result of a development meeting, my project has changed direction. I thank the two of you who helped with answers, and am going to close the question.
Simply remove the panel from the form and create the new one.
Sample:
Panel CreatePanelWithDynamicControls() {
Panel ret = new Panel();
ret.Dock = DockStyle.Fill;
// Some logic, which initializes content of panel
return ret;
}
void InitializeDynamicControls() {
this.Controls.Clear();
Panel pnl = this.CreatePanelWithDynamiControls();
this.Controls.Add(pnl);
}
void Form1_Load(object sender, EventArgs e) {
if (!this.DesignMode) {
this.InitializeDynamicControls();
}
}
// I don't know exactly, on which situation
// do you want reset controls
void SomeEvent(object sender, EventArgs e) {
this.InitializeDynamicControls();
}
You could try calling this.InitializeComponent(), which may do the trick. Alternately, if your application has a 'Directory Select' form and a 'Process Files' form, you could have the Directory Select form do a "new" on the Process Files form, which should return it to its original state (not while its open, though).

Categories

Resources