Open Same Form 2 Different Ways

B

BD

I am coding with C# in Visual Studio 2005 for a database application
residing on remote MS SQL Server 2005. What I want to do is open the
same form but from 2 different places and only one instance. I
currently have the form where it will open from a double click on
datagridview from another form to the proper record and only one
instance. However, doing this bypassed by databinding source for
opening the form to all records. I still want to be able to open the
form to all records again only one instance. I think I need to set
up
custom load methods and run a form load check with if/else statements
pointing the correct load method. I have been unable to do this
however and any help would be greatly appreciated.
 
A

Alberto Poblacion

BD said:
I am coding with C# in Visual Studio 2005 for a database application
residing on remote MS SQL Server 2005. What I want to do is open the
same form but from 2 different places and only one instance. I
currently have the form where it will open from a double click on
datagridview from another form to the proper record and only one
instance. However, doing this bypassed by databinding source for
opening the form to all records. I still want to be able to open the
form to all records again only one instance. I think I need to set
up
custom load methods and run a form load check with if/else statements
pointing the correct load method. I have been unable to do this
however and any help would be greatly appreciated.

You can use a version of the "Object Factory" pattern. The class contains
a static method for creating instances, which keeps track of wether an
instance has already been created, and in that case returns the existing
instance. If what you want to do is show the form, it can also do a Show()
on that instance. Something like the following:

class MyForm : Form
{
private MyForm():base{} //Prevents "new" form being used from
outside

private static MyForm instance = null;

public static MyForm ShowTheForm()
{
if (instance==null) instance=new MyForm();
instance.Show();
return instance;
}
}

Every time you want to open it, invoke it as MyForm.ShowTheForm();
 
B

Bruce Wood

I am coding with C# in Visual Studio 2005 for a database application
residing on remote MS SQL Server 2005. What I want to do is open the
same form but from 2 different places and only one instance. I
currently have the form where it will open from a double click on
datagridview from another form to the proper record and only one
instance. However, doing this bypassed by databinding source for
opening the form to all records. I still want to be able to open the
form to all records again only one instance. I think I need to set
up
custom load methods and run a form load check with if/else statements
pointing the correct load method. I have been unable to do this
however and any help would be greatly appreciated.

Check out the Singleton design pattern. You can apply it to a Form so
that there is only ever one instance of the Form at any given time. It
goes something like this:

public class SingletonForm : System.Windows.Forms.Form
{
private static SingletonForm _instance = null;

private SingletonForm()
{
... usual constructor stuff here ...
}

public SingletonForm Instance
{
get
{
if (SingletonForm._instance == null)
{
SingletonForm._instance = new SingletonForm();
}
return SingletonForm._instance;
}
}
}

Then, wherever in your code you need a new SingletonForm, you say

SingletonForm f = SingeltonForm.Instance;
f.Show();

If there already is one, then it will give it back to you. If there
isn't one, then it will create one.

Of course, you should always use .Show() with this form,
never .ShowDialog(). If the form is already showing and you do
a .ShowDialog(), I have no idea what will happen. (Anyone care to let
us know?)
 
B

BD

Check out the Singleton design pattern. You can apply it to a Form so
that there is only ever one instance of the Form at any given time. It
goes something like this:

public class SingletonForm : System.Windows.Forms.Form
{
private static SingletonForm _instance = null;

private SingletonForm()
{
... usual constructor stuff here ...
}

public SingletonForm Instance
{
get
{
if (SingletonForm._instance == null)
{
SingletonForm._instance = new SingletonForm();
}
return SingletonForm._instance;
}
}

}

Then, wherever in your code you need a new SingletonForm, you say

SingletonForm f = SingeltonForm.Instance;
f.Show();

If there already is one, then it will give it back to you. If there
isn't one, then it will create one.

Of course, you should always use .Show() with this form,
never .ShowDialog(). If the form is already showing and you do
a .ShowDialog(), I have no idea what will happen. (Anyone care to let
us know?)

I need to supply some more information. All of my forms are contained
within MdiParent. On this MdiParent, I have a docked panel with
linklabel to open this form to expose all records. Also have another
linklabel to open another form with datagridview listing showing all
records pertaining to one record. When you doubleclick a row in
datagridview, opens the above form to a specific record. That latter
works fine, but the coding for showing all records is bypassed. See
below:

//The Following is for the LinkLabel Control
private void WorkOrder_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
'datasetWorkOrders.Workorder_Labor' table. You can move, or remove it,
as needed.

this.workorder_LaborTableAdapter.Fill(this.datasetWorkOrders.Workorder_Labor);
// TODO: This line of code loads data into the
'datasetWorkOrders.WorkOrderMaterial' table. You can move, or remove
it, as needed.

this.workOrderMaterialTableAdapter.Fill(this.datasetWorkOrders.WorkOrderMaterial);
// TODO: This line of code loads data into the
'datasetWorkOrders.Locations' table. You can move, or remove it, as
needed.

this.locationsTableAdapter.Fill(this.datasetWorkOrders.Locations);
// TODO: This line of code loads data into the
'datasetWorkOrders.Customers' table. You can move, or remove it, as
needed.

this.customersTableAdapter.Fill(this.datasetWorkOrders.Customers);
// TODO: This line of code loads data into the
'datasetWorkOrders.Work_Order' table. You can move, or remove it, as
needed.

this.work_OrderTableAdapter.Fill(this.datasetWorkOrders.Work_Order);
}

//The Following is for the Double-Click event on the
datagridrow view on other form
internal void WorkOrder_LoadView(int WorkOrderID)
{

work_OrderTableAdapter.FillByWorkOrderID(datasetWorkOrders.Work_Order,
WorkOrderID);

this.workorder_LaborTableAdapter.Fill(this.datasetWorkOrders.Workorder_Labor);

this.workOrderMaterialTableAdapter.Fill(this.datasetWorkOrders.WorkOrderMaterial);

this.locationsTableAdapter.Fill(this.datasetWorkOrders.Locations);

this.customersTableAdapter.Fill(this.datasetWorkOrders.Customers);
}
 
D

Dave Shooter

Bruce said:
Check out the Singleton design pattern. You can apply it to a Form so
that there is only ever one instance of the Form at any given time. It
goes something like this:

public class SingletonForm : System.Windows.Forms.Form
{
private static SingletonForm _instance = null;

private SingletonForm()
{
... usual constructor stuff here ...
}

public SingletonForm Instance
{
get
{
if (SingletonForm._instance == null)
{
SingletonForm._instance = new SingletonForm();
}
return SingletonForm._instance;
}
}
}

Then, wherever in your code you need a new SingletonForm, you say

SingletonForm f = SingeltonForm.Instance;
f.Show();

If there already is one, then it will give it back to you. If there
isn't one, then it will create one.

Of course, you should always use .Show() with this form,
never .ShowDialog(). If the form is already showing and you do
a .ShowDialog(), I have no idea what will happen. (Anyone care to let
us know?)

I remember reading that this idiom isn't entirely thread safe (I'll post
the URL if I can find it). If thread1 calls:

get
{
if (SingletonForm._instance == null)... // result true

and is then suspended before proceeding to the next line, a second
thread2 can also call:

get
{
if (SingletonForm._instance == null) // result still true
{
SingletonForm._instance = new SingletonForm();
}
return SingletonForm._instance;
}

If this thread isn't blocked it'll proceed to create the new form. If
thread1 then continues from where it left off, it'll also create a new
SingletonForm because SingletonForm._instance == null was true. For this
reason, I implement this pattern as :

public class SingletonForm : System.Windows.Forms.Form
{
private static SingletonForm _instance = null;
private static readonly object padlock = new object();

private SingletonForm()
{
... usual constructor stuff here ...
}

public SingletonForm Instance
{
get
{
if (SingletonForm._instance == null)
{
lock (padlock)
{
// Make a second check
if (SingletonForm._instance == null)
{
SingletonForm._instance = new SingletonForm();
}
}
}
return SingletonForm._instance;
}
}
}

The second check of "if (SingletonForm._instance == null)" ensures that
a suspended thread doesn't go on to create the object when it resumes.

Dave
 
B

Bruce Wood

I remember reading that this idiom isn't entirely thread safe (I'll post
the URL if I can find it). If thread1 calls:

get
{
if (SingletonForm._instance == null)... // result true

and is then suspended before proceeding to the next line, a second
thread2 can also call:

get
{
if (SingletonForm._instance == null) // result still true
{
SingletonForm._instance = new SingletonForm();
}
return SingletonForm._instance;

}

If this thread isn't blocked it'll proceed to create the new form. If
thread1 then continues from where it left off, it'll also create a new
SingletonForm because SingletonForm._instance == null was true. For this
reason, I implement this pattern as :

public class SingletonForm : System.Windows.Forms.Form
{
private static SingletonForm _instance = null;
private static readonly object padlock = new object();

private SingletonForm()
{
... usual constructor stuff here ...
}

public SingletonForm Instance
{
get
{
if (SingletonForm._instance == null)
{
lock (padlock)
{
// Make a second check
if (SingletonForm._instance == null)
{
SingletonForm._instance = new SingletonForm();
}
}
}
return SingletonForm._instance;
}
}

}

The second check of "if (SingletonForm._instance == null)" ensures that
a suspended thread doesn't go on to create the object when it resumes.

All very true for normal singletons, but remember that this is a Form.
You can't create forms and manipulate them from different threads, or
the WinForms UI goes BOOM!

Since you have to do all UI manipulation on the UI thread anyway,
there's not much point in making a singleton form threadsafe.

Unless I'm missing something....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top