 |
|
 |
This can be done with FirstOrDefault method:
Please find the code snippet below.
private void toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button == toolBarButton1)
{
Patients myPatients = new Patients();
Form frmExist = this.MdiChildren.FirstOrDefault(f => f.Name == myPatients .Name);
if (frmExist == null || frmExist.Name != myPatients .Name)
{
myPatients.MdiParent = this;
myPatients.StartPosition = FormStartPosition.Manual;
myPatients.Location = new Point(0,0);
myPatients.Show();
}
else
{
frmExist.Activate();
}
}
Note : This will applicable only for .Net 3.5 and later
Thanks
Kalai
|
|
|
|
 |
|
 |
What about just searching the MdiChildren collection for an instance with the same type as the form in order to decide, whether or not, you should create a new instance? Something like:
var f = this.MdiChildren.OfType<YourForm>().FirstOrDefault();
if (f == null)
{
f = new YourForm();
f.MdiParent = this;
f.Show();
return;
}
f.Focus();
Cheers,
|
|
|
|
 |
|
 |
mdiTest2::Material^ MaterialWnd;
for(int x = 0; x < this->MdiChildren->Length; x++)
{
//found the window?
if(String::Compare(this->MdiChildren[x]->Name, "Material") == 0)
{
this->MdiChildren[x]->Activate(); //yes -> activate
return; //and return..
}
}
//if we end up here, the window doesnt exist and we can create it...
MaterialWnd = gcnew mdiTest2::Material;
MaterialWnd->MdiParent = this;
MaterialWnd->Name = "Material";
MaterialWnd->Show();
|
|
|
|
 |
|
 |
great, it works well
|
|
|
|
 |
|
 |
I am still learning C#. I am searching for other solutions to this concern and found my way to this thread.
I made a C# version of the IsLoaded function to tell me if a child form is already opened.
private bool IsLoaded(string frmName)
{
for (int x =0; x < this.MdiChildren.Length;x++)
{
if (this.MdiChildren[x].Text == frmName)
return true;
}
return false;
}
so in your toolBar1_ButtonClick, you can do this:
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (IsLoaded("Patient"))
{
// activate child form because its already loaded
}
else
{
// instantiate and show child form
}
}
|
|
|
|
 |
|
 |
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { // a flag to store if the child form is opened or not if (e.Button == toolBarButton1) { // get all of the MDI children in an array Form[] charr = this.MdiChildren; if (charr.Length == 0) // no child form is opened { Patients myPatients = new Patients(); myPatients.MdiParent = this; // The StartPosition property is essential // for the location property to work myPatients.StartPosition = FormStartPosition.Manual; myPatients.Location = new Point(0,0); myPatients.Show(); } else // child forms are opened { foreach (Form chform in charr) { if (chform.Name == "Patients") // one instance of the form is already opened { chform.Activate(); return; } } Patients myPatients = new Patients(); myPatients.MdiParent = this; // The StartPosition property is essential // for the location property to work myPatients.StartPosition = FormStartPosition.Manual; myPatients.Location = new Point(0,0); myPatients.Show(); } } } Have a good day!
|
|
|
|
 |
|
 |
if you have a number of forms you can use this function: private bool FormIsOpen(string strFormName) { Form[] charr = this.MdiChildren; if (charr.Length == 0) // no child form is opened { return false; } else // child forms are opened { foreach (Form chform in charr) { if (chform.Name == strFormName) // one instance of the form is already opened { chform.Activate(); return true; } } } return false; }
then call it like this: if (! FormIsOpen("RelatedProducts")) { RelatedProducts frmRelatedProducts = new RelatedProducts(); frmRelatedProducts.MdiParent = this; frmRelatedProducts.StartPosition = FormStartPosition.Manual; frmRelatedProducts.Location = new Point(0, 0); frmRelatedProducts.Show(); } DotNetNuke modules, Databases, PocketPC interfaces, web sites and web marketing
|
|
|
|
 |
|
 |
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { // a flag to store if the child form is opened or not bool found = false; if (e.Button == toolBarButton1) { // get all of the MDI children in an array Form[] charr = this.MdiChildren; if(charr.Length > 0) { for( int i = 0;i<charr.Length;i++) { // one instance of the form is already opened if(charr[i].Name.Equals("frmSChild")) { charr[i].Activate(); found = true; break; // exit loop } } } else if(found == false) { frmSChild myPatients = new frmSChild(); myPatients.MdiParent = this; // The StartPosition property is essential // for the location property to work myPatients.StartPosition = FormStartPosition.Manual; myPatients.Location = new Point(0,0); myPatients.Show(); } } } Enjoy...
|
|
|
|
 |
|
 |
Hi ,
I implented this example in my application but its not working in my application don't know why actually m calling a first child form from parent and the secondchild form is called by the first child form which is already opened.first child form sends a msg to parent to open second child form now when i close this second child form i want to update a value in first child form it fires valueupdated event it executes also but it doesn't updates the value in first child form.can you help me on this ?
thanks
|
|
|
|
 |
|
 |
Form[] charr = this.MdiChildren;
if (charr.Length == 0) {
suspent sus=new suspent(transobj);
sus.MdiParent=this;
sus.StartPosition = FormStartPosition.Manual;
sus.Location = new Point(0,0);
sus.Show();
}
else {
int count=0;
foreach (Form chform in charr)
{
if (chform.Name == "suspent") {
chform.Activate();
count=0;
break; }
else
count++;
}
if(count>0)
{
suspent sus=new suspent(transobj);
sus.MdiParent=this;
sus.StartPosition = FormStartPosition.Manual;
sus.Location = new Point(0,0);
sus.Show();
}
}
i think it is more efficient cuase it was opening more instance for the 3rd and 4th and .. child
|
|
|
|
 |
|
 |
I a have a problem, I want to display information in a ststus bar(parent) from a child form, for example position mouse.
David, Mexico
|
|
|
|
 |
|
 |
Why don't you try to create an additional container class (with some properties) and some private members to pass the current mouse position from the child class. Then, retrieve this information from the parent class. (I think in order for it to work and therefore allow you communication between your two classes, you should make your necessary method/property static).
Hope this helps.
Regards,
Polis
Can you practice what you teach?
-- modified at 14:14 Monday 21st November, 2005
|
|
|
|
 |
|
 |
Activating the form is also more user friendly...
Programming in your interest
|
|
|
|
 |
|
 |
The problem addressed in the article comes up pretty frequently. The solution that is generally considered 'best practice' it to implement a Singleton pattern. If you have used older MS technologies (for example, VB) and are just switching to C#, a good patterns tutorial can help almost anyone bump his or her code up a notch.
There are a ton of articles on the web about design patterns, and several books that show how to implement the most popular design patterns in C#. I like Cooper, "C# Design Patterns" because it has a pretty good tutorial on OO programming in C#.
Dave Veeneman
www.veeneman.com
|
|
|
|
 |
|
 |
private void frmKreditLoadAll()
{
if (_frmChildSearch("Kredit list all") != 911)
{
frmKreditList _tmpfrm = new frmKreditList();
_tmpfrm.MdiParent = this;
foreach(Control c in this.Controls)
{
if(c is MdiClient)
_tmpfrm.Bounds = c.ClientRectangle;
}
_tmpfrm.Show();
}
}
private int _frmChildSearch(string _str)
{
int _i = 0;
if (MdiChildren.Length > 0)
{
for (; _i < MdiChildren.Length; _i ++)
{
if (MdiChildren[_i].Text.Trim().Equals(_str))
{
MdiChildren[_i].Focus();
return 911;
}
}
}
return _i;
}
test
|
|
|
|
 |
|
 |
How trying this, which is a little bit cleaner:
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button == toolBarButton1)
{
Form[] children = MdiChildren;
foreach (Form child in children)
{
if (child.GetType() == typeof(Patients))
{
child.Activate();
return;
}
}
Patients patients = new Patients();
patients.MdiParent = this;
patients.StartPosition = FormStartPosition.Manual;
patients.Location = new Point(0,0);
patients.Show();
}
Or even extract the foreach statement and the properties setting into seperate methods:
private bool ActivateMdiChild(Type formType)
{
Form[] children = MdiChildren;
foreach (Form child in children)
{
if (child.GetType() == formType)
{
child.Activate();
return true;
}
}
return false;
}
private void ShowMdiChild(Form form)
{
form.MdiParent = this;
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(0,0);
form.Show();
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button == toolBarButton1)
{
if (!ActivateMdiChild(typeof(Patients)))
{
ShowMdiChild(new Patients());
}
}
else if (e.Button == toolBarButton2)
{
if (!ActivateMdiChild(typeof(Staff)))
{
ShowMdiChild(new Staff());
}
}
}
Be aware that none of the above code has been tested.
Cheers
David
|
|
|
|
 |
|
 |
Why not use the singleton pattern? Surely it's slightly simpler and has the strong design background to it. What benifits does this design have?
|
|
|
|
 |
|
 |
The solution that I use in my MDI applications, when I only want one child instance per form, is to hook the created/closed events and disable the appropriate menu item and toolbar button. This also gives the person visual feedback that the form is already open.
If they can't find the form (say it's minimized), there's a list of open forms under the "Windows" menu item, in addition to the typical things--cascade, tile, close all, etc.
Marc
Microsoft MVP, Visual C#
MyXaml
MyXaml Blog
|
|
|
|
 |
|
 |
Before coming up with the described solution, I implemented it the way you describe it. However, disabling the toolBar button wasn't what I was after for because it made the button look very ugly. (There are Bitmaps on the buttons).
Polis
|
|
|
|
 |