473,588 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5477
"BD" <bw******@chart er.netwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.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.ShowTheF orm();

May 8 '07 #2
On May 8, 10:16 am, BD <bwald...@chart er.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.I nstance;
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...@cana da.comwrote:
On May 8, 10:16 am, BD <bwald...@chart er.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.I nstance;
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
'datasetWorkOrd ers.Workorder_L abor' table. You can move, or remove it,
as needed.

this.workorder_ LaborTableAdapt er.Fill(this.da tasetWorkOrders .Workorder_Labo r);
// TODO: This line of code loads data into the
'datasetWorkOrd ers.WorkOrderMa terial' table. You can move, or remove
it, as needed.

this.workOrderM aterialTableAda pter.Fill(this. datasetWorkOrde rs.WorkOrderMat erial);
// TODO: This line of code loads data into the
'datasetWorkOrd ers.Locations' table. You can move, or remove it, as
needed.

this.locationsT ableAdapter.Fil l(this.datasetW orkOrders.Locat ions);
// TODO: This line of code loads data into the
'datasetWorkOrd ers.Customers' table. You can move, or remove it, as
needed.

this.customersT ableAdapter.Fil l(this.datasetW orkOrders.Custo mers);
// TODO: This line of code loads data into the
'datasetWorkOrd ers.Work_Order' table. You can move, or remove it, as
needed.

this.work_Order TableAdapter.Fi ll(this.dataset WorkOrders.Work _Order);
}

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

work_OrderTable Adapter.FillByW orkOrderID(data setWorkOrders.W ork_Order,
WorkOrderID);

this.workorder_ LaborTableAdapt er.Fill(this.da tasetWorkOrders .Workorder_Labo r);

this.workOrderM aterialTableAda pter.Fill(this. datasetWorkOrde rs.WorkOrderMat erial);

this.locationsT ableAdapter.Fil l(this.datasetW orkOrders.Locat ions);

this.customersT ableAdapter.Fil l(this.datasetW orkOrders.Custo mers);
}

May 8 '07 #4
Bruce Wood wrote:
On May 8, 10:16 am, BD <bwald...@chart er.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
datagridvie w 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.I nstance;
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
<alwayskeepitlo aded@delete_thi s.googlemail.co mwrote:
Bruce Wood wrote:
On May 8, 10:16 am, BD <bwald...@chart er.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.I nstance;
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
1936
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 concepts I have not encountered elsewhere, or explain ones I have encountered in ways that add to my understanding. There are a few points of style with which I take issue, such as not consistently using braces around the body of an if or loop,...
4
2802
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 myself with the contents of the fields and do a simple window.open but I must use POST, not GET. And another question: Is there a better way to submit large fields to the server without the POST method and without including the
55
4640
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 not obvious how the code works if you don't know the intricacies of the Property Let/Get syntax. Likewise, I dislike (and code to minimize the use of) the VB/VBA syntax of returning a value by referring to the function name as if it were a...
115
14043
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 prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
8
10756
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 can't do this in a report as it will prompt me for the parameters, even though they seem to be defined ahead of the recordsource. I have worked around this by opening reports to a bogus recordsource
2
4403
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 that extension and use that as my filter criteria for the File Open dialog. Once the user selects a file with that extension (from the File Open dialog), I simply want to open that file (whether it is an .xls file, .txt file, etc.). I am...
6
2389
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 completed finished, the second browser jusk pick up some session variables from the first browser and process right after that. It messed up everything. I was thinking about use remote_addr, but it seems not working since we are behind the...
3
4675
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 form that has the System.Windows.Forms.LinkLabel controls on it is in a different project and under a different namespace from the file where the LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog under the LinkClicked method. ...
6
2260
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 a standard way to gain control within the sub-forms to do individual clean-up prior to their removal?
0
7862
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.