473,388 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Open Same Form 2 Different Ways

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.

May 8 '07 #1
5 5464
"BD" <bw******@charter.netwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
>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();

May 8 '07 #2
On May 8, 10:16 am, BD <bwald...@charter.netwrote:
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?)

May 8 '07 #3
BD
On May 8, 1:15 pm, Bruce Wood <brucew...@canada.comwrote:
On May 8, 10:16 am, BD <bwald...@charter.netwrote:
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?)
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.dataset WorkOrders.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.datas etWorkOrders.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.datasetWorkOr ders.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.datasetWorkOr ders.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.datasetWorkO rders.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(datasetWo rkOrders.Work_Order,
WorkOrderID);

this.workorder_LaborTableAdapter.Fill(this.dataset WorkOrders.Workorder_Labor);

this.workOrderMaterialTableAdapter.Fill(this.datas etWorkOrders.WorkOrderMaterial);

this.locationsTableAdapter.Fill(this.datasetWorkOr ders.Locations);

this.customersTableAdapter.Fill(this.datasetWorkOr ders.Customers);
}

May 8 '07 #4
Bruce Wood wrote:
On May 8, 10:16 am, BD <bwald...@charter.netwrote:
>>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?)
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
May 8 '07 #5
On May 8, 11:40 am, Dave Shooter
<alwayskeepitloaded@delete_this.googlemail.comwrot e:
Bruce Wood wrote:
On May 8, 10:16 am, BD <bwald...@charter.netwrote:
>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?)

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....

May 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Steven T. Hatton | last post by:
I've finally gotten around to reading Accelerated C++ by Andrew Koenig and Barbara Moo. There's a lot of good stuff in what I've read so far. Even though it is _very_ basic, they present some...
4
by: Timothy Madden | last post by:
Hello all I have a little problem and I hope it is simple. I have a form on my page and I have to open a pop-up window with the page serverd after the form submit. I know I can compose the URL...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
115
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i...
8
by: lauren quantrell | last post by:
When I open an Access form I can have no recordset specified, then in the form's OnOpen event I can do something like: Me.paramaters = "@SomeColumn = 22)" Me.recordsource = "dbo.sproc123" But I...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
6
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I am thinking about doing this since I got several cases that some of our internal users open more than one browser at the same time from our server. When one of the transactions was not...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
6
by: Thom Little | last post by:
Using C# 3.5 I have a form that calls many other sub-forms. Typically there will be five forms open at the same time. If the main form is closed all the sub forms are also closed. Is there...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.