So in the project I'm planning, the user will be switching between different screens frequently. These different screens will have different controls with different functions, and really nothing in common.
Should I create a form for each screen and just form.close and form.show(?) to switch screen? Or is it better to keep hiding and showing certain controls, so if a user wants to go to form B from form A, all of form A's controls disappear, and form Bs controls appear? I don't need to know how to do it, I just need to know which is the recommended or proper way, since I haven't really seen it anywhere.
You shouldn't use
form.close();
as this will close the current form (and if its your main form, exit the application) use
form.hide();
instead. An example is given below
from Form1:
private void btnModify_Click(object sender, EventArgs e)
{
Form mod = new modifyForm();
mod.Owner = this;
mod.Show();
this.Hide();
}//end btnModify_Click
from modifyForm:
private void btnCancel_Click(object sender, EventArgs e)
{
this.Owner.Show();
this.Close();
}
You can also try using the TabControl. You can group your different screens into the different tab items. All you have to do is drag it to the form and edit its content in the Properties window.
Related
I've had a look around but none of the answers make any sense to me. I have a menu form which has buttons on; when users come to use the menu form, you can open other forms from the menu. Currently, I can get the form to open, but the menu form stays open too.
private void BtnAddNewCar_Click(object sender, EventArgs e)
{
AddCompanyCar carForm = new AddCompanyCar();
carForm.ShowDialog();
}
The code above opens the form AddCompanyCar from the menu. How do I add to this code so that the form 'Menu' closes when AddCompanyCar opens?
Are you sure want to do this as it impacts usability. If you're using WinForms, then just create a container window, and replace the panels instead. Might be easy and best way
If not and you wanna go-ahead, can take a look on this example
Why not just hide it, then show it again when ShowDialog() returns?
private void BtnAddNewCar_Click(object sender, EventArgs e)
{
this.Visible = false;
AddCompanyCar carForm = new AddCompanyCar();
carForm.ShowDialog(); // execution stops here until "carForm" is dismissed
this.Visible = true;
}
by closing the main window, you destroy the context in which you were previously working. As others suggest, simply hide the main window so you can return to it.
I'm trying to simply get a form to load correctly. It is a modeless child form of a modal parent (which itself is a modeless child form of the main UI). I need to be able to interact with all forms somewhat simultaneously.
The form I want to reload has very little access by way of fixed controls. Mostly dynamic controls loaded from reading a text file (users) and placing checkboxes (1 per user in the text file in columns) on the form.
The first thing I need to do is simply to write the form accurately every time. I open the form with a button, but retain ownership to the parent. If the parent closes, all of the children should close (but not the program. The parent of this child, is a child). Ex:
private void bPermissions_Click(object sender, EventArgs e)
{
Permissions af3 = new Permissions();
af3.Owner = this;
af3.Show();
}
So I click the button Permissions and the form opens accurately. If I select it again, the form opens without the checkboxes. If I close Permissions and then try to reopen it, it does not load accurately. Only the fixed items load. I've tried to .Refresh() the parent form and the child form in various events (FormClosed, FormClosing, Load, etc.) on both the Start form and the Permissions form.
How can I refresh this form accurately every time I try to open it?
EDIT:
I'm a newb and very much enjoy doing this. But I am learning. Please be kind and point me in a direction. :-D Thank You.
EDIT2: Not modal. Modeless.
Ok. #D.. definitely led me down the path. As a Newb I promise I had to do a little head scratching.
I was making the call from onLoad
private void Permissions_Load(object sender, EventArgs e)
{
WidgetLogic.getPermText(this);
WidgetLogic.getDetailerPermText(this);
WidgetLogic.getAdminPermText(this);
}
I should have been making the call from the button prior to the page loading and passing it back from the logic to the targetForm. The form where I wanted the information to go (Permissions af3)
private void bPermissions_Click(object sender, EventArgs e)
{
Permissions af3 = new Permissions();
af3.Owner = this;
af3.Show();
WidgetLogic.getPermText(af3);
WidgetLogic.getDetailerPermText(af3);
WidgetLogic.getAdminPermText(af3);
}
Awesome Sauce!! :-D Thank You!!
I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}
there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?
please see this example:
Here's a solution using a standard WinForms TabControl, where the Tabs are hidden at run-time, but of course they are available at design-time.
Assumptions :
You don't want to get into creating OwnerDrawn Tabs, which is possible.
A standard WinForms TabControl will meet all your design-time needs.
Code :
In the Form Load event of the Form that hosts your TabControl use code like this :
tabControl1.Region = new Region(tabControl1.DisplayRectangle);
To hide the Tabs.
Then, "wire" up your buttons to handle selecting the different TabPages in the TabControl. Obviously you could do this in a more elegant way than this :
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[0];
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[1];
}
Note : if you want to insert secondary Forms or UserControls into the TabPages of a TabControl : that's not a problem : of course simpler to use UserControls. Insert them into the Controls collection of each TabPage and set their 'Dock Property to 'DockStyle.Fill.
Note : there are fancier ways you can hide the Tabs, like using a derived TabControl as shown here on CodeProject : TabControl on a WinForm without showing the Tab header? There are other solutions out there that use a modified WndProc. They're not hard to find.
I don't know exactly what you're trying to do, but if you've got a Panel on your form named contentArea and a bunch of user controls created (but not on the form), then you could use this as an event handler for a button:
public void myButton_Click(object sender, EventArgs e) {
contentArea.Controls.RemoveAt(0);
contentArea.Controls.Add(new MyUserControl());
}
...though as other people have said, a tab control would be better in this case.
What you can do is have them each in a separate Panel. Set the Visible property to false to each. When the Click event on the button, set the Visible property of all of them to false and set the one you want shown's Visible to true.
For example if you have two form Form1 and Form2 and you want to load form2 inside from1. when you press a button to load form2 the code is like this
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Controls.Clear();
foreach(Control c in this.Controls)
{
this.Controls.Add(c);
}
}
this code will load all the controls in the form2 into form1.
I (currently) have two forms, one that needs to call the other. In other words, by clicking a certain button in Form1, Form2 needs to "pop up", like a dialog box. I know I have to get a reference in both forms, but I'm not entirely sure how to do this. What is a decent tutorial/beginner site to learn about GUIs in C#?
You can try this:
protected void Button1_Click(object sender, EventArgs e)
{
Form2 myForm = new Form2();
myForm.ShowDialog();
}
ShowDialog forces the user to close that window before s/he can access the rest of the application.