I have two forms, Form 2 is inheriting from Form 1.
What I need to do is that when I close both Form 1 and Form 2 another form which asks if user is sure to quit appears. Then if user clicks Yes, another form which asks if the user wants to save the game appears if and only if the form which the user closes is Form 2 and not Form 1 since for Form 1 there is no saving necessary.
This is what I managed to do:
// These are the Form 1 closing and closed event handlers:
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
SureClose sc = new SureClose();
sc.StartPosition = FormStartPosition.CenterScreen;
sc.Show();
}
private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
}
Then in Sure Close: // Please note that Tournament is Form 2 inheriting from GameForm (Form 1)
private void yesButton_Click(object sender, EventArgs e)
{
this.Hide();
if (GameForm.ActiveForm is Tournament)
{
SaveGame sg = new SaveGame();
sg.StartPosition = FormStartPosition.CenterScreen;
sg.Show();
}
else
{
GameForm.ActiveForm.Close();
}
}
private void noButton_Click(object sender, EventArgs e)
{
this.Hide();
}
// This is the SaveGame Form:
private void saveButton_Click(object sender, EventArgs e)
{
// Still to do saving!
}
private void dontSaveButton_Click(object sender, EventArgs e)
{
this.Hide();
GameForm.ActiveForm.Close();
}
The problem is that when in the yesButton event handler in SureClose Form I have GameForm.ActiveForm.Close(), this is going back to the GameForm Closing event handler therefore the SureClose dialog is appearing again.
I tried doing: if (e.CloseReason() == CloseReason.UserClosing)
but obviously it doesn't work either because the reason of closing will always be the user :/
How can I solve this?
Thanks a lot for any help !
Form1 :
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(SureClose())
{
SaveChanges();
}
else
{
e.Cancel = true;
}
}
private bool SureClose()
{
using(SureClose sc = new SureClose())
{
sc.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sc.ShowDialog();
if(result == DialogResult.OK)
{
return true;
}
else
{
return false;
}
}
}
protected virtual void SaveChanges()
{
}
Form2:
protected override void SaveChanges()
{
using(SaveGame sg = new SaveGame())
{
sg.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sg.ShowDialog();
if(result == DialogResult.OK)
{
//saving code here
}
}
}
SureClose form and SaveGame form:
private void yesButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void noButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
Related
I designed a dashboard form that contains buttons. Some of these buttons have sub-menus. Since I'm planning to limit the access to some sub-menus, I want that every time a user clicks a restricted sub-menu, a form will appear that will prompt the user to enter the password before accessing the restricted page.
I already made a password form and programmed that when a sub-menu is clicked, the password form will appear. However, once the correct password is submitted, it will look like this:
I want that it will look like this when password is correct:
I want the overlapping form placed on the original position / panel. How to do that?
Here's the code for the dashboard:
public CFMenu()
{
InitializeComponent();
customizeDesign();
}
private void customizeDesign()
{
panelInventory.Visible = false;
panelSales.Visible = false;
}
private void hideSubMenu()
{
if(panelInventory.Visible == true)
panelInventory.Visible = false;
if(panelSales.Visible == true)
panelSales.Visible = false;
}
private void showSubMenu(Panel subMenu)
{
if(subMenu.Visible == false)
{
hideSubMenu();
subMenu.Visible = true;
}
else
subMenu.Visible = false;
}
private void CFMenu_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'casaFrancaDataSet.Sales' table. You can move, or remove it, as needed.
this.salesTableAdapter.Fill(this.casaFrancaDataSet.Sales);
timer1.Start();
label4.Text = DateTime.Now.ToLongTimeString();
label5.Text = DateTime.Now.ToLongDateString();
}
private void btnInventory_Click(object sender, EventArgs e)
{
showSubMenu(panelInventory);
}
private void btnSales_Click(object sender, EventArgs e)
{
showSubMenu(panelSales);
}
private void button1_Click(object sender, EventArgs e)
{
openPanelForm(new CFInventoryAdd());
hideSubMenu();
}
private void button2_Click(object sender, EventArgs e)
{
openPanelForm(new PasswordInventView());
hideSubMenu();
//string password = Interaction.InputBox("Enter Password: ", "Inventory View");
//if(password == "hello")
//{
// openPanelForm(new CFInventoryView());
// hideSubMenu();
//}
//else
//{
// MessageBox.Show("Incorrect Password!");
//}
}
private void button3_Click(object sender, EventArgs e)
{
openPanelForm(new CFInventoryUpdate());
hideSubMenu();
}
private void button7_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesAdd());
hideSubMenu();
}
private void button6_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesView());
hideSubMenu();
}
private void button8_Click(object sender, EventArgs e)
{
openPanelForm(new CFCalculate());
hideSubMenu();
}
private void button5_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesUpdate());
hideSubMenu();
}
private void button9_Click(object sender, EventArgs e)
{
openPanelForm(new CFReport());
hideSubMenu();
}
private void timer1_Tick(object sender, EventArgs e)
{
label4.Text = DateTime.Now.ToLongTimeString();
timer1.Start();
}
private Form activeForm = null;
private void openPanelForm(Form childForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelForm.Controls.Add(childForm);
panelForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
Here's a sub-menu that requires a password:
private void button2_Click(object sender, EventArgs e)
{
openPanelForm(new PasswordInventView());
hideSubMenu();
}
Here's the code for the Password Prompt form:
public partial class PasswordInventView : Form
{
public PasswordInventView()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var password = txtPassword.Text;
if (password == "1234")
{
CFInventoryView f2 = new CFInventoryView();
f2.Show();
this.Visible = false;
}
else
{
MessageBox.Show("Username or Password is incorrect!");
txtPassword.Clear();
}
}
Before making the password form, I tried using the InputDialog from VisualBasic by referencing it on my project. I liked it however I can't set the font of the input, and I wanted that my password will appear as ******** when typed. That's why I made my own password form.
public partial class PasswordInventView : Form
Change form to UserControl.
public partial class PasswordInventView : UserControl
And create user control in main form.
If this not works go create user control and copy PasswordInventView to UserControl.
Ctrl + Alt + L > Right click project > Add > New Item > Select User Control (Windows Forms) > Add. Copy chield form to user control. And create user control in main form.
Search about user control.
I have a C# project that has 2 forms. The first has 3 buttons. I need to be able from the second form to hide 2 (button1 and button2 )buttons with a checkbox, and I don't know how to call the buttons from the first form.
this is form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
and this is Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
?????????
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
Another option is to pass the form as the "owner" in the Show() command:
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this); // pass Form1 reference in to our instance of Form2
}
In Form2, cast the Owner property back to Form1 so you can access it (assuming you've changed the modifiers property of the buttons to public as already suggested):
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false; // or whatever your buttons are called
}
}
This is almost exactly what I had posted previously...you need to change the Modifiers property of the buttons so they are public and can be seen from Form2.
this is the final version that works in my case thanks to those who answered my question and helped me to get this answer
Form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm = new Form2();
frm.checkBox1.Checked = Properties.Settings.Default.checkB;
if (frm.checkBox1.CheckState == CheckState.Checked)
{
button1.Visible = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}
Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
The buttons and checkBox are set to Modifiers - Public
you need to make the buttons on the first form public and then you can access them once you create a instance of the first form you will need to pass that form to the second form.
Might want to look into building an event that fires from one form and gets handled by the other form to disable the button.
Can someone explain why my new form doesn't appear? The old form closes but it isn't replaced by my new one:
namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Is "Menu" the startup form for your application? If yes, then when it closes the entire application closes.
One solution would be to hide the current form, display "form" with ShowDialog(), then call Close():
private void PlayButton_Click(object sender, EventArgs e)
{
this.Visible = false; // hide the current form
Application.DoEvents();
PongForm form = new PongForm();
form.ShowDialog(); // code stops here until PongForm is dismissed
this.Close(); // now close the form (and presumable the whole app)
}
I want my application to hide when the user want to close it.
after that by clicking on notifyicon the form will be shown .
Here is my coded : [form name = login]
private void login_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.hide();
}
and for notifyicon :
private void NIcon_MouseClick(object sender, MouseEventArgs e)
{
this.Show();
}
but it doesn't work! What is wrong?
You can try also ShowInTaskbar property
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
e.Cancel = true;
ShowInTaskbar = false;
}
private void LoginForm_DoubleClick(object sender, EventArgs e)
{
ShowInTaskbar = true;
Show();
}
You have to add them to the event handlers also, try this on form_load
this.FormClosing += LoginForm_FormClosing;
notifyIcon1.DoubleClick += LoginForm_DoubleClick;
Then you can do like this
login form;
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
form.hide();
}
private void LoginForm_DoubleClick(object sender, EventArgs e)
{
form.Show();
}
Also read this it will be helpful.
How can I force the focus of an form? .Focus() is not working for me.
private void button1_Click(object sender, EventArgs e) {
var form = new loginForm();
if (Application.OpenForms[form.Name] == null) {
form.Show();
} else {
form.Focus();
}
}
What am I doing wrong?
You need to show the form first - use the Show() method:
var form = new loginForm();
form.Show();
Edit: (updated question)
For an existing form calling Activate() might be more appropriate, this also brings the form to the front:
private void button1_Click(object sender, EventArgs e)
{
var form = new loginForm();
if (Application.OpenForms[form.Name] == null)
{
form.Show();
}
else
{
Application.OpenForms[form.Name].Activate();
}
}
If the form is minimized you need to subscribe to the Activated event to change your window state to FormWindowState.Normal:
private void loginForm_Activated(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
Try this:
this.BringToFront();
this.Activate();
it should be
private void button1_Click(object sender, EventArgs e) {
var form = new loginForm();
if (Application.OpenForms[form.Name] == null) {
form.Show();
} else {
Application.OpenForms[form.Name].Focus();
}
}
None of the previous answers worked for me, but this does:
protected override void OnShown(EventArgs e)
{
Focus();
}
None of the focusing methods worked if called before this event. Hope it helps.
On start of the form we add
this.BringToFront();
this.Activate();