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.
Related
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.
I have a mdi parent form and I open my other forms in run time as mdi child form by this code:
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
{
Upload objWA = new Upload();
objWA.MdiParent = this;
objWA.Show();
//objWA.WindowState = FormWindowState.Maximized;
}
private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
Reports objUI = new Reports();
objUI.MdiParent = this;
objUI.Show();
//objUI.WindowState = FormWindowState.Maximized;
}
but the problem is: When current form is open, user can open another form and it can be repeated several times so that each form is opened what's code for closing the previous child form before user open a new child form??
Screen shot for reference
if we see the image my upload and reports forms both are getting opened on one another but it should only show the currently opened form
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
{
Upload objWA = new Upload();
objWA.MdiParent = this;
objWA.Show();
//objWA.WindowState = FormWindowState.Maximized;
}
private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
Reports objUI = new Reports();
objUI.MdiParent = this;
objUI.Show();
DisposeAllButThis(Form objUI);
//objUI.WindowState = FormWindowState.Maximized;
}
public void DisposeAllButThis(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType()
&& frm != form)
{
frm.Dispose();
frm.Close();
}
}
}
let me know if it worked because i just made it up
public void DisposeAllButThis(Form form){
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType()
&& frm != form)
{
frm.Dispose();
frm.Close();
}
}}
got this from:
C# loop through all MDI childer and close all other forms except the current
I open an additional form through a Toolstrip to enter a Username that will be needed in the Mainform (and is declared as String in the Mainform)
Code of Mainform:
private void toolStripButton6_Click(object sender, EventArgs e)
{
using (Form frm = new Form3())
{
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
frm.StartPosition = FormStartPosition.CenterParent;
if (frm.ShowDialog() == DialogResult.OK)
{
Username = frm.ReturnValue1;
}
}
}
Code of Form3:
public string ReturnValue1 {
get
{
return textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
C# tells me that there is no frm.ReturnValue1 :(
You have declared your form as type Form not Form3:
using (Form frm = new Form3())
and as the class Form doesn't have a property ReturnValue1 you are getting the error. This compiles because Form3 is a subclass of Form so you can assign it to a variable of type Form without any casting being required. If you had it the other way round the compiler would have told you you needed a cast.
Your code should be:
using (Form3 frm = new Form3())
or perhaps even (my preference):
using (var frm = new Form3())
Then it will always be of the right type and you don't have to remember to change the class name in two places should you decide to use a different form in future.
I create a new MdiChild from MainForm using this method:
AdminLogInForm adminForm;
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null)
{
adminForm = new AdminLogInForm();
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
Why when i close my child, using in chld form "this.close()" using that method i cant open it anymore?
there i call close();
private void cancelLogInButton_Click(object sender, EventArgs e)
{
this.MdiParent.Activate();
if(this.MdiParent!=null)
((MainForm)this.MdiParent).LogInAsAdminMenuItem.Enabled = true;
this.Close();
}
by the way to make work that I asked before I hed to plase this.Close(); after all statements .
By closing the form you are not making adminForm instance to null (Which is what your if condition will check when you try to open it next time.)
On diposal of your form make adminForm = null and then your if condition will work next time.
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null)
{
adminForm = new AdminLogInForm(this);
adminForm.Disposed += new EventHandler(adminForm_Disposed); //Add Disposed EventHandler
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
void adminForm_Disposed(object sender, EventArgs e)
{
adminForm = null;
}
As Described by Marshal that the closing of a form makes it disposed you should add a condition for disposing as well like this
AdminLogInForm adminForm;
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null || adminForm.IsDisposed)
{
adminForm = new AdminLogInForm();
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
Or you can also create a function to use a form as mdi
like this
private void button5_Click(object sender, EventArgs e)
{
if (domainUpDown2.Text == "Battlefield: Bad Company 2")
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
All that does is open a new blank form, but i need it to open a new form with a webbrowser in it so i can set it url dependent on the if statement..
Assuming your form2 has a WebBrowser control, and that it has a property you can set like this:
public Uri WebLocation
{
set { webBrowser1.Url = value; }
}
Then modify your code as follows:
private void button5_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
if (domainUpDown2.Text == "Battlefield: Bad Company 2")
form2.WebLocation = new Uri("http://badcompany2.yoursite.com");
if (domainUpDown2.Text == "Some Other Item")
form2.WebLocation = new Uri("http://someotheritem.yoursite.com");
form2.ShowDialog();
}