So i have that aplication:
Menu Picture
and when i press the button "Terminar sessão" it's supposed to log out (and its works)
but when i went back to login the menu does not close!
Login Picture after log out
I am using nested form, the form ends session is closing but not the menu.
This is my menu.cs:
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelChildForm.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
openChildForm(new Transferências());
Slidepanel.Height = (button2.Height - 15);
Slidepanel.Top = (button2.Top + 10);
}
and on "Transferências" form i have it:
private void Sessao_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Tem a certeza que deseja terminar sessão?", "Terminar Sessão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Hide();
Login login = new Login();
login.ShowDialog();
}
}
i already tried Menu.Close(); but it does not work
With the amount of information you provide, it is quite hard to tell what the problem might be. Did you try to debug it to verify that your program actually tries to execute Menu.Close()?
If you show your code people might be able to help you more.
Related
I have a label with a text "X" and I use this to close my forms. I have this code in my login:
private void btnLogIn_Click(object sender, EventArgs e)
{
if (userLevel.IndexOf("ADMIN") + 1 > 0)
{
Save_Data();
using (AdminGateOpt adminOptions = new AdminGateOpt())
{
adminOptions.gUsers = User;
adminOptions.windowNumber = windowNumber;
adminOptions.userDetails = userLevel.Split('|');
adminOptions.ShowDialog();
Close();
}
}
}
..and if the userlevel is an admin, then it will go to another form:
private void btnInGate_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Do you want to go in In-Gate Form?", String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
ReadBarCodeInGate gateForm = new ReadBarCodeInGate();
gateForm.gUsers = gUsers;
gateForm.windowNumber = windowNumber;
gateForm.userDetails = userDetails;
gateForm.ShowDialog();
Hide();
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
login Login = new login();
Login.ShowDialog();
Hide();
}
And it's also the same to other forms that suddenly it became like this, that once I hit the X label button it will go to another form but it wont close nor hide the previous form/s.
I have a Save button on my WinForm which saves the form information and then closes the form using the this.Close() statement.
There is however another way of closing the form and that is the X button.
I am using the FormClosing event to ask a question before the form is closed.
private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Newsletter = null;
}
else
{
e.Cancel = true;
}
}
But the way of handling the close form depends on how the form is closed. If the user clicks on the Save button, the form should be closed without a question. It is only when user clicks on the X button that the question should be asked.
How can I let my form know what the Close command comes from?
A simple boolean flag should do the trick:
private bool saveClicked = false;
private void btnSave_click(object sender, EventArgs e)
{
saveClicked = true;
}
private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e)
{
if(saveClicked)
return;
DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Newsletter = null;
}
else
{
e.Cancel = true;
}
}
I have a one issue in my window application, there are two form :login and main.
I have created Exit Button on both form and also coded of Exit Confirmation.
It works fine when we Press Exit Button message No on form 1. But We login to on form to
another form and second form Exit Button message button press No , and then back to return
form one and then click exit btton No ::::::it will display two times message PopUp of confirmation...............
Code
private void Btn_Exit_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Do you want to exit.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
Application.ExitThread();
} else
{ }
}
Plz there is any solution in c#............
Just code this.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to close this application?", "Exit", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
}
}
In your case consider LoginForm and MainForm , Once you logged in you try to hide the LoginForm and show MainForm. Problem is when you try to close MainForm it will invoke the respective FormCloseEvent and once you chose to close, it automatically invoke parent form that is hiding in background, so it invokes LoginForm's FormCloseEvent. This is the reason for two time Popups.
To resolve this you need to trigger an event i.e whenever Child form is closed you need to raise a flag, so in your parent's FormCloseEvent you need to check for flag, if flag is true you no need to show the pop up.
private bool isMainFormClosed = false;
private void showMainForm()
{
// Hide the loginform UI.
this.Hide();
var mainForm = new MainForm();
// Creating close event for mainform, whenever close icon is clicked it will close the login form which is hiding in background.
mainForm.FormClosed += new FormClosedEventHandler(mainFormClosed);
// Show the mainform UI
mainForm.Show();
}
private void mainFormClosed(object sender, FormClosedEventArgs e)
{
this.isMainFormClosed = true;
this.Close();
}
private void loginFormClosing(object sender, FormClosingEventArgs e)
{
if(!this.isMainFormClosed)
{
DialogResult dialogResult = MessageBox.Show("Do you want to close the application",AppConstants.APPLICATION_NAME,
MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(dialogResult == DialogResult.No)
e.Cancel = true;
}
}
DialogResult is returned by dialogs after dismissal. It indicates which button was clicked on the dialog by the user.
Code
private void btnExit_Click(object sender, EventArgs e)
{
const string message = "Do you want to exit?";
const string caption = "EXIT";
var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else if (result == DialogResult.Yes)
{
this.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to close this application?", "Exit", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
}
}
i doing window app using c#.net.
i have a form name form_1 with menu-strip.
from the menu-strip of form_1, i am opening same form form_1 and closing the same form_1 after using it but if i click that for the second time it is not showing,if i click that for third time it is showing.
edit:
mainform
form fm;
bool frm= false;
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (frm== false)
{
fm= new form();
fm.MdiParent = this;
fm.Show();
frm= true;
}
else
{
if (fm.IsDisposed)
{
frm= false;
}
}
}
form
form fm = new form();
fm.MdiParent = this;
fm.Show();
this.Close();
If you expect your function addToolStripMenuItem_Click to always open fm (assuming it is disposed), then you'll need fm.show() in the else condition as well. You could try something like this instead...
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!frm || fm.IsDisposed)
{
if (fm != null && fm.IsDisposed) { frm = false; }
fm = new form();
fm.MdiParent = this;
fm.Show();
frm = true;
}
}
This probably makes your bool frm obsolete, but I left it in in case you're using it for something else.
I am building a kids learning application, where clicking on a button on panel, I want to show different forms in the same place of the panel. Can you please help with any walk-through or tutorial links?
This question should have been posted on Stackoverflow website rather than here.
But you can use this approach to handle the case.
subForm = new SubFormYouWantToLoad();
subForm.TopLevel = false;
subForm.FormBorderStyle = FormBorderStyle.None;
ContainerPanel.Controls.Add(subForm , 0, 1);
subForm .Visible = true;
You can add this code when you click on the specific button.
Here each subform is added to the Panel as a Control. You should remove the subform from the panel's control list before adding another subform. For this ,it is better to remove,close and dispose the first one.
ContainerPanel.Controls.Remove(activeform);
activeform.Close();
activeform.Dispose();
Instead of Forms use user controls and load them in to panels
Sample if you want to show usercontrol1
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr1 = new UserControl1();
usr1.Show();
panel1.Controls.Add(usr1);
If usercontrol2
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr2 = new UserControl2();
usr2.Show();
panel1.Controls.Add(usr2);
You could create a number of forms as user controls or a control that inheriets from a panel. Then have a parent form with a panel to hold the user controls. You can then change the active user control in the container when the panel needs to be changed.
There is a tutorial on msdn for creating user controls.
http://msdn.microsoft.com/en-us/library/a6h7e207(v=vs.71).aspx
I used this code to close the form on the panel but not worked..
private void button12_Click(object sender, EventArgs e)
{
dontShowPANEL();
//ActiveForm.Close();
MainImaginCp kj = new MainImaginCp();
//kj.Visible = false;
kj.panel2.Controls.Clear();
panel1.Visible = true;
EngABCLearning usr1 = new EngABCLearning();
usr1.Show();
kj.panel2.Controls.Add(usr1);
//kj.Focus();
}
And I used the following code to show the form in the panel.
private void toolStripMenuItem1_LR_ENG_Click(object sender, EventArgs e)
{
//kids.Form2 hj = new kids.Form2();
//hj.Show();
EngABCLearning gh = new EngABCLearning();
//gh.Show();
gh.TopLevel = false;
gh.FormBorderStyle = FormBorderStyle.None;
//Panel2.Controls.Add(subForm, 0, 1);
panel2.Controls.Add(gh);
gh.Visible = true;
}
This is closing my main form and exiting the application.
try this out i have loaded two forms inside a single panel
private void Form1_Load(object sender, EventArgs e)
{
Form2 f1 = new Form2();
f1.TopLevel = false;
f1.AutoScroll = true;
panel1.Controls.Add(f1);
f1.Dock = DockStyle.Left;
f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f1.Show();
//form2
Form3 f2 = new Form3();
f2.TopLevel = false;
f2.AutoScroll = true;
panel1.Controls.Add(f2);
f2.Dock = DockStyle.Left;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Show();
}
try this i used this method to load multiple forms at one panel
private Form activeForm = null;
public void FormLoad(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
panelName.Controls.Add(childForm);
childForm.Visible = true;
}
private void YourBtn1_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm1Name());
}
private void YourBtn2_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm2Name());
}