I'm having MdiParent form which has Menu and Submenu
Parent Form is Say Form A if I open Form B using submenu option using following code
B addbill = new B();
B.Show();
B.MdiParent = this;
It opens Form B as child of Form A. Now I want to open Form C from Form B after click the Button on Form B and Form B will be closed and Form C will be opened as Child of Form A
Again after click button on Form C , Form C Will be closed and Form B will be opened as Form A
So what can I do to do same ?
On FormB button click event write this code:
FormC fc=new FormC();
fc.MdiParent=this.MdiParent;
fc.Show();
And in FormC load event write this code:
FormB fb=new FormB();
fb.Hide();
fb.Close();
before B is closed:
C.MdiParent = B.MdiParent; // which is pointing to A
In Form B button click where you are calling Form C, you have to assign the MdiParent of FormB to MdiParent of FormC which is FormA. After that you can close FormB.
//FormB Button Click
private void button1_Click(object sender, EventArgs e)
{
FormC frm = new FormC();
frm.MdiParent = this.MdiParent; // assign MdiParent of FormB to FormC
frm.Show();
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
Analysis an = new Analysis();//on login click open anothe form on same perrent
an.MdiParent = this.MdiParent;
an.Show();
}
It's Work..Try This Code
private void btCountSale_Click(object sender, EventArgs e)
{
bool exist = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Counter Sale")
{
exist = true;
f.BringToFront();
break;
}
}
if (exist == false)
{
frmCounterSale fm = new frmCounterSale();
fm.MdiParent = this.MdiParent;
fm.Show();
}
}
It's Work..Try This Code
private void btCountSale_Click(object sender, EventArgs e)
{
bool exist = false;
foreach (Form f in Application.OpenForms)
{
if (f.Name== "frmCounterSale")
{
exist = true;
f.BringToFront();
break;
}
}
if (exist == false)
{
frmCounterSale fm = new frmCounterSale();
fm.MdiParent = this.MdiParent;
fm.Show();
}
}
Related
I have 2 win forms..Form A and B, form B is let's say child and A is parent...
Form B is sending some data to A, but i have duplicate process with form A when i close form B...a second form A opens up...
This is the code that I put in form B...but the problem is that the value from form B is not "going" to form A...with this code only form A opens up, there is no duplicate process and B closes up, but no data comes from B to A...
This is the code:
private void button1_Click(object sender, EventArgs e)
{
//Index of the row which is currently selected.
int myIndex = dataGridView1.CurrentRow.Index;
DataGridViewRow row = this.dataGridView1.Rows[myIndex];
brdok = row.Cells["Broj_dokumenta"].Value.ToString();
FormCollection fc = Application.OpenForms;
bool FormFound = false;
foreach (Form frm in fc)
{
if (frm.Name == "Main")
{
//Open form A
Main f = new Main();
f.textRadniNalog.AppendText(brdok);
f.Focus();
FormFound = true;
}
}
if (FormFound == false)
{
Main f = new Main();
f.Show();
}
//close form B
this.Close();
}
The other answer explains how you can keep your current basic architecture, except without the specific bug. That said, Marko's comment is a better answer. Here's what that would look like:
partial class Main : Form
{
private void button1_Click(object sender, EventArgs e)
{
// Add whatever other initialization is needed here, of course
new FormB(this).Show();
}
}
partial class FormB : Form
{
private Main _main;
public FormB(Main main)
{
_main = main;
}
private void button1_Click(object sender, EventArgs e)
{
//Index of the row which is currently selected.
int myIndex = dataGridView1.CurrentRow.Index;
DataGridViewRow row = this.dataGridView1.Rows[myIndex];
brdok = row.Cells["Broj_dokumenta"].Value.ToString();
_main.textRadniNalog.AppendText(brdok);
_main.Focus();
//close form B
this.Close();
}
}
If it's actually possible for the Main form to be closed while FormB is open (something you can prevent by showing FormB using the ShowDialog() method instead of the Show() method), then you can add logic to account for that:
partial class FormB : Form
{
private Main _main;
public FormB(Main main)
{
_main = main;
if (_main != null)
{
_main.FormClosed += (sender, e) => _main = null;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (_main != null)
{
//Index of the row which is currently selected.
int myIndex = dataGridView1.CurrentRow.Index;
DataGridViewRow row = this.dataGridView1.Rows[myIndex];
brdok = row.Cells["Broj_dokumenta"].Value.ToString();
_main.textRadniNalog.AppendText(brdok);
_main.Focus();
}
else
{
new Main().Show();
}
//close form B
this.Close();
}
}
If needed, you can similarly handle the scenario where the Main form could be opened after FormB is shown; the code that does that would of course need to know about the FormB instance, and would pass the new Main form instance reference to it when it shows the form. In that case — where forms rely on each other but come and go on some arbitrary mechanism — it would be best, rather than having each individual form have to know what other form(s) have to react to them, for you to have some top-level controller object that deals with all of the interactions, including being the go-between for events and state changes in different windows.
looks like you are creating a new instance even when the form is found
if (frm.Name == "Main")
{
//Open form A
Main f = new Main();
please try changing it to
if (frm is Main)
{
Main f = frm as Main;
f.textRadniNalog.AppendText(brdok);
f.Focus();
or
var mainFrm=fc.OfType<Main>().FirstOrDefault();
if (mainFrm != null)
{
mainFrm.textRadniNalog.AppendText(brdok);
mainFrm.Focus();
}
else
{
Main f = new Main();
f.Show();
}
I a form than I can open it also by pressing F1 and clicking so in this code first I check if the form has been opened or not.if was opened I just want to bring it to front. my problem is it wont bring in front by pressing F1 or clicking ToolStripMenuItem if i open it befor.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Form2 form2;
form2 = new Form2();
if (e.KeyCode.ToString() == "F1" && Application.OpenForms.OfType<Form2>().Count() <= 0)
// the user pressed the F1 key
form2.Show();
else
{
form2.TopMost = true;
form2.BringToFront();
form2.Focus();
form2.TopMost = false;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2;
form2 = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() <= 0)
form2.Show();
else
{
form2.TopMost = true;
form2.BringToFront();
form2.Focus();
form2.TopMost = false;
}
}
It doesn't work because you forgot to call form2.Show() in the else clause. The code is wrong, you don't want to create a new instance of Form2 if one already exists. Also, there's a bug in Winforms that makes Application.OpenForms lose track of form instances.
Best thing to do is to explicitly keep track of the lifetime of the form with its FormClosed event handler so you don't have to find it back later:
Form2 form2;
private void showForm2() {
if (form2 == null) {
form2 = new Form2();
form2.FormClosed += delegate { form2 = null; };
}
form2.Show();
form2.Focus();
}
Call showForm2 from your event handlers.
Just call form2.Show() and BringToFront. You should declare the `new Form2()' once though.
private Form2 form2;
private void ShowForm2()
{
if (form2 == null)
{
form2 = new Form2();
form2.FormClosed += delegate { form2 = null; };
}
form2.Show();
form2.BringToFront();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
ShowForm2();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowForm2();
}
From Hans' code, I added the delegate to handle null problem on closing the form and opening it again.
i have created an application in which a menustrip is present in which 2 buttons are there, one for ADD, and another for UPDATE & both controls are in a single form, means a button of add & update is there in a single form, whenever i press add button in menustrip update button will be disabled, and when i press update on menustrip the add button will disable. how to do this? i m doing this by show method but that form is opening multiple times using show().
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button1.Enabled = true;
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
you simply have to use a single form in this case. try using the singleton approach -
http://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
try using .ShowDialog() instead .Show() and no other form will be able to be clicked on until that one closes.
To do that you'll need to have an instance of that Form outside of those methods that you dismply show if the Form has already been created, or create and show it if it has not (this is the singleton pattern). Here's an example:
Form1 f2 = null;
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form1();
f2.MdiParent = this;
f2.button1.Enabled = true;
}
f2.Show();
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2.MdiParent = this;
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
f2.Show();
}
One question on your disabling of the menu items though, how do you plan on re-enabling them after they have been disabled?
just try to check that form is already opened or not by using its Text Property.. if it is opened just focus on that form other wise show that form as normally
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
}
}
if (IsOpen == false)
{
Form f1 = new Form1();
f1.Show();
}
}
Try This Guys Its Simple
Actually Its my first project. While I want to convert my VB.Net2008 to C#2010 I have few clarification pls.
In Form2 Properties I set - IsMDIContainer = True. Then the below code to open my MdiChild and now what is my problem when I click the close button, it's also closing the MDIParent. But I need to close only mdichild... for that I tried as like Vb.Net2008 style by the following codes placed in MDIParent Form2, Its not working. Any right directions ...
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form3 NwMdiChild2 = new Form3;
NwMdiChild2.MdiParent = this;
NwMdiChild2.Dock = System.Windows.Forms.DockStyle.Fill;
NwMdiChild2.Show();
}
private void Form2_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
Form[] MdiChildForms = this.MdiChildren;
int kkk1 = MdiChildForms.Length;
int x = 0;
for (x = 0; x <= MdiChildForms.Length - 1; x += 1)
{
if (MdiChildForms[x].Name == "Form1")
{
kkk1 = kkk1 - 1;
}
MdiChildForms[x].Close();
}
if (kkk1 > 0)
{
// For Not Closing
e.Cancel = true;
}
else
{
// For Closing
e.Cancel = false;
Application.Exit();
}
}
Any Right Directions for Me?
I'm not sure if I understand well what you want to achieve: do you want, when click Parent Form close button, insteed of closing parent form, to close all the child's form? Form2 is your main form (parent MDI container), Form3 is the MDI children, isn't it?
Please try following code and tell if it's what you are asking for:
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form3 NwMdiChild2 = new Form3(); //don't forget ()
NwMdiChild2.MdiParent = this;
NwMdiChild2.Dock = System.Windows.Forms.DockStyle.Fill;
NwMdiChild2.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//if no MDI child - this is going to be skipped and norlmal Form2 close takes place
if (this.MdiChildren.Length > 0) //close childrens only when there are some
{
foreach (Form childForm in this.MdiChildren)
childForm.Close();
e.Cancel = true; //cancel Form2 closing
}
}
Is there any code like, frmBallloon is shown in btnShow click event of the frmBase and again I want to show frmBase if user clicks on the btnShow of the frmBalloon then there is two copy of the frmBase.I want only one copy.Then how can I alter the form view by clicking on the button.
Try this
frmBase button click
Form2 frm2 = new Form2();
this.Visible = false;
frm2.Show(this);
And here frmBalloon Button Click
if (this.Owner != null)
{
this.Visible = false;
this.Owner.Show();
}
You can try something like this
Code for Form1
public Form2 f2;
private void button1_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form2 {f1 = this};
f2.Show();
}
else
f2.Focus();
}
Code for Form2
public Form1 f1;
private void button1_Click(object sender, EventArgs e)
{
if (f1 == null)
{
f1 = new Form1 {f = this};
f1.Show();
}
else
f1.Focus();
}