473,756 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application wide component

Joe
Is it possible to have a component which is global to the entire
application? I need to have a single component act sort of like a server
which components in any of the forms can access.

For example if I drop a component on Form1 & Form2 and that component has a
property called Server, at design time I would like to be able to assign the
global component to the Server property.

I'm sure I'm asking for way too much...

Thanks for any help,
Joe
Dec 9 '06 #1
7 2226
Hi Joe,

You can use the singleton pattern to ensure that only a single instance of
your server component may be used:

sealed class GlobalServer
{
public static readonly GlobalServer Instance = new GlobalServer();

public object GlobalState
{
get { return globalState; }
set { globalState = value; }
}

private static object globalState;

// remove the private constructor (or add a public one)
// if you want to allow creation of instances that aren't
// shared (see comments below)
private GlobalServer() { }
}

In your "client" components, you don't have to assign them any reference.
Just use GlobalServer.In stance directly or wrap it in a property:

class Client : Component
{
public GlobalServer Server
{
get
{
return GlobalServer.In stance;
}
}
}

If you need to be able to assign different instances of GlobalServer to your
Client component then simply add a "set" accessor with a local field in
which to store the reference. To assign an instance in the Form designer
using the Properties window, your GlobalInstance component would have to
derive from Component as well and must be added to the Form, but then it
will no longer be a shared (global) instance. You'll also have to indicate
to the PropertyGrid which Types are allowable for your Server property. You
can use a base editor that I made for one of my projects:

// 2.0 framework code

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Componen tModel.Design;
using System.Componen tModel;
using System.Windows. Forms;

namespace YourLibrary
{
public abstract class ComponentSelect orEditor<T: ObjectSelectorE ditor
where T : class, IComponent
{
public ComponentSelect orEditor()
{
}

protected override void FillTreeWithDat a(Selector selector,
System.Componen tModel.ITypeDes criptorContext context, IServiceProvide r
provider)
{
selector.Clear( );
selector.AddNod e("(none)", null, null);

Type baseType = typeof(T);

IReferenceServi ce referenceServic e =
provider.GetSer vice(typeof(IRe ferenceService) ) as IReferenceServi ce;

foreach (IComponent component in context.Contain er.Components)
{
if (baseType.IsAss ignableFrom(com ponent.GetType( )))
{
string name = null;

if (component != null && component.Site != null)
name = component.Site. Name;
else if (referenceServi ce != null)
name = referenceServic e.GetName(compo nent);
else
throw new InvalidOperatio nException("The re is no service available to
retrieve the name of one or more components.");

selector.AddNod e(name, component, null);
}
}
}
}
}

With this base editor you can derive an editor that will allow properties to
bind to "GlobalServ er" components:

public sealed class ServerSelectorE ditor
: ComponentSelect orEditor<Global Server // base component Type
{
// no implementation required :)
}

Note: GlobalServer should be renamed to Server since it's not global anymore

The editor above can be assigned to your Server property like this:

class Client : Component
{
private GlobalServer server;

[Editor(typeof(S erverSelectorEd itor),
typeof(System.D rawing.Design.U ITypeEditor))]
[DefaultValue(nu ll)] // not req. but recommended
public GlobalServer Server
{
get { return server; }
set { server = value; }
}
}
NOTE: You may need to build the solution and then close and restart VS for
the changes to take affect, or else you might not see any GlobalServer
components in the list even though some have been added to the Form
designer.

You could code instances of GlobalServer to access only static resources, so
in a sense you could actually do what you wanted using the editor above,
however I'd recommend using the first scenario for that and foregoing the
designer support if you don't actually need instances of GlobalServer.

--
Dave Sexton

"Joe" <jb*******@noem ail.noemailwrot e in message
news:et******** ******@TK2MSFTN GP04.phx.gbl...
Is it possible to have a component which is global to the entire
application? I need to have a single component act sort of like a server
which components in any of the forms can access.

For example if I drop a component on Form1 & Form2 and that component has
a property called Server, at design time I would like to be able to assign
the global component to the Server property.

I'm sure I'm asking for way too much...

Thanks for any help,
Joe

Dec 9 '06 #2
Joe
Hi Dave,

Thanks for the info. It seems like it should give me exactly what I'm
looking for but the client control doesn't see the GlobalServer at design
time if they're on different forms.

I may just go with hard coding the static reference into the clients. I had
wanted to avoid this so other developers won't go crazy trying to figure out
how the client controls get a connection to the server.

Thanks again,
Joe

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Hi Joe,

You can use the singleton pattern to ensure that only a single instance of
your server component may be used:

sealed class GlobalServer
{
public static readonly GlobalServer Instance = new GlobalServer();

public object GlobalState
{
get { return globalState; }
set { globalState = value; }
}

private static object globalState;

// remove the private constructor (or add a public one)
// if you want to allow creation of instances that aren't
// shared (see comments below)
private GlobalServer() { }
}

In your "client" components, you don't have to assign them any reference.
Just use GlobalServer.In stance directly or wrap it in a property:

class Client : Component
{
public GlobalServer Server
{
get
{
return GlobalServer.In stance;
}
}
}

If you need to be able to assign different instances of GlobalServer to
your Client component then simply add a "set" accessor with a local field
in which to store the reference. To assign an instance in the Form
designer using the Properties window, your GlobalInstance component would
have to derive from Component as well and must be added to the Form, but
then it will no longer be a shared (global) instance. You'll also have to
indicate to the PropertyGrid which Types are allowable for your Server
property. You can use a base editor that I made for one of my projects:

// 2.0 framework code

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Componen tModel.Design;
using System.Componen tModel;
using System.Windows. Forms;

namespace YourLibrary
{
public abstract class ComponentSelect orEditor<T: ObjectSelectorE ditor
where T : class, IComponent
{
public ComponentSelect orEditor()
{
}

protected override void FillTreeWithDat a(Selector selector,
System.Componen tModel.ITypeDes criptorContext context, IServiceProvide r
provider)
{
selector.Clear( );
selector.AddNod e("(none)", null, null);

Type baseType = typeof(T);

IReferenceServi ce referenceServic e =
provider.GetSer vice(typeof(IRe ferenceService) ) as IReferenceServi ce;

foreach (IComponent component in context.Contain er.Components)
{
if (baseType.IsAss ignableFrom(com ponent.GetType( )))
{
string name = null;

if (component != null && component.Site != null)
name = component.Site. Name;
else if (referenceServi ce != null)
name = referenceServic e.GetName(compo nent);
else
throw new InvalidOperatio nException("The re is no service available to
retrieve the name of one or more components.");

selector.AddNod e(name, component, null);
}
}
}
}
}

With this base editor you can derive an editor that will allow properties
to bind to "GlobalServ er" components:

public sealed class ServerSelectorE ditor
: ComponentSelect orEditor<Global Server // base component Type
{
// no implementation required :)
}

Note: GlobalServer should be renamed to Server since it's not global
anymore

The editor above can be assigned to your Server property like this:

class Client : Component
{
private GlobalServer server;

[Editor(typeof(S erverSelectorEd itor),
typeof(System.D rawing.Design.U ITypeEditor))]
[DefaultValue(nu ll)] // not req. but recommended
public GlobalServer Server
{
get { return server; }
set { server = value; }
}
}
NOTE: You may need to build the solution and then close and restart VS for
the changes to take affect, or else you might not see any GlobalServer
components in the list even though some have been added to the Form
designer.

You could code instances of GlobalServer to access only static resources,
so in a sense you could actually do what you wanted using the editor
above, however I'd recommend using the first scenario for that and
foregoing the designer support if you don't actually need instances of
GlobalServer.

--
Dave Sexton

"Joe" <jb*******@noem ail.noemailwrot e in message
news:et******** ******@TK2MSFTN GP04.phx.gbl...
>Is it possible to have a component which is global to the entire
application? I need to have a single component act sort of like a server
which components in any of the forms can access.

For example if I drop a component on Form1 & Form2 and that component has
a property called Server, at design time I would like to be able to
assign the global component to the Server property.

I'm sure I'm asking for way too much...

Thanks for any help,
Joe


Dec 9 '06 #3
Hi Joe,
Thanks for the info. It seems like it should give me exactly what I'm
looking for but the client control doesn't see the GlobalServer at design
time if they're on different forms.
Yes, the editor only looks for server components on the same Form as the
client component, which is why you must add a new server instance to each
form. Since each client will have their own server instance on each Form,
the server class is no longer "global", as I mentioned in my original
response.

Another approach would be to derive all of your components from a custom
base component class that has the Server property from my first example (the
one with no set accessor). That way, all of your components will use the
static server and won't have to be assigned each time they are added to a
form.

<snip>

--
Dave Sexton
Dec 9 '06 #4
Joe
Hi Dave,

I thought that would be the only way to handle this.
Thanks for your time,
Joe

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uy******** ******@TK2MSFTN GP06.phx.gbl...
Hi Joe,
>Thanks for the info. It seems like it should give me exactly what I'm
looking for but the client control doesn't see the GlobalServer at design
time if they're on different forms.

Yes, the editor only looks for server components on the same Form as the
client component, which is why you must add a new server instance to each
form. Since each client will have their own server instance on each Form,
the server class is no longer "global", as I mentioned in my original
response.

Another approach would be to derive all of your components from a custom
base component class that has the Server property from my first example
(the one with no set accessor). That way, all of your components will use
the static server and won't have to be assigned each time they are added
to a form.

<snip>

--
Dave Sexton


Dec 9 '06 #5
Hi Joe,

You can me the modifier to public to make the GlobalServer seen from other
forms. Also, a better approach, is to create another class library project
e.g. named Common. You can add reference to this classs, so that
GlobalServer is form independent and can be seen from all forms.

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Dec 11 '06 #6
Hi Joe,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
=============== =============== =============== =====

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Dec 13 '06 #7
Joe
Yes this has been resolved. I ended up creating a static class which my
components all reference.

"Kevin Yu [MSFT]" <v-****@online.mic rosoft.comwrote in message
news:4Q******** ******@TK2MSFTN GHUB02.phx.gbl. ..
Hi Joe,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
=============== =============== =============== =====

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Dec 13 '06 #8

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

Similar topics

3
1936
by: DraguVaso | last post by:
Hi, I have an VB.NET application that uses an OCX for some control. I integrated the application with the .NET Application Updater Component (http://windowsforms.net/articles/appupdater.aspx) to copy automaticly new versions of the application to the clients. The problem is: it copies the OCX, but the OCX isn't registered. Everything works fine until the the directory of the old version is deleted: The application doesn't find anymore...
3
5884
by: xmlguy | last post by:
XmlTextReader myXmlReader = new XmlTextReader(args); string en = myXmlReader.Encoding.EncodingName; //Console.WriteLine(x); Error: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
1
2939
by: ~dr-sci-fi | last post by:
hi all, problem: i have several controls on my form which can get focus, like buttons/combos. i have to catch keyboard events for special keys like Alt-F1 on my form, for that, i simply implemented keydown event handler on my form control, but when some other control has focus, all keypress events are generated on that control. Q: how can i implement an application wide keypress event handler, so that if Alt-F1 is press on any control...
0
945
by: Stefano | last post by:
Hi all, I need to define an application wide custom event, and handle it into an HttpModule. I created a class, with a statis "Current" property. This property returns the instance of the class, retrieved from HttpContext.Current.Application (if it's the first time, a new instance is created). Now... I created a custom HttpModule, and I registered it into web.config
2
1075
by: Steve | last post by:
I am developing a web solution. I would like to create a dynamically sized array of strings at application start (presumably in the "Application_Start" event), which can then be referenced through subsequent "page level" objects. I only want to dynamically create the array once, so it seems that it cannot be defined in either the "Global.asax" module or the "HttpSessionState" object.
2
1467
by: Brett Romero | last post by:
I'd like to create an application wide variable in a winform app. I can do this on my start up form by reading in some values from a config file (since these values may change in the future) and assigning them to a public var. From there, I'd always have to reference the start up form. Is there a better way? Thanks, Brett
2
1461
by: Mark Olbert | last post by:
I want to bind a series of ObjectDataSource instances to an application-wide business object which caches the results of a database query. I'm confused about where/how to cache the results (it's a simple query, BTW, so I'm using a DataReader, not a DataTable). The business object has no knowledge of the application, and hence of the Cache, which is where I would normally put this kind of thing (actually, I used to put this stuff in...
1
1113
by: cnickl | last post by:
I have a multi form (not MDI) application and I would like my program to intercept keyboard input regardless witch form is active. I tried the usual KeyUp event hander on the MainForm on the App first, but it didn’t do anything because one of the controls of the form always seems to be selected. Next, I tried to override the WndProc with the following code: const int WM_KEYUP = 0x101; protected override void WndProc(ref Message m) {...
11
3045
by: Sanjay | last post by:
Probably a newcomer question, but I could not find a solution. I am trying to have some singleton global objects like "database connection" or "session" shared application wide. Trying hard, I am not even being able to figure out how to create an object in one module and refer the same in another one. "import" created a new object, as I tried. Badly need guidences.
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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
10014
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
9819
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
9689
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
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
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
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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

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.