|
As a MFC programmer I got used to be able to specify an UpdateCommandUI command handler that would take care of enabling/disabling my toolbar buttons and menu items, setting checkmarks in front of menuitems, select correct push buttons in toolbars etc. Is there any mechanism similar to this in .NET? I haven't been able to figure it out, or do I have to hard code this behaviour into every single event handler that's relevant?
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!
|
|
|
|
|
dabs wrote:
do I have to hard code this behaviour into every single event handler that's relevant?
Write seprate method which update controls and simply call it whenever you need updating.
Mazy
No sig. available now.
|
|
|
|
|
|
Uwe Keim wrote:
You missed the "call" word
Corrected now.
Uwe Keim wrote:
but surely it is quite inefficient if you have large number of controls to update.
Yes, but I don't think any other way exists in C#.
Uwe Keim wrote:
I think MFC's version only call the apropriate handlers that are really needed (e.g. only the contents of ONE dropdown menu).
Its a long time that I haven't use MFC , but as muuch as I remeber it was not act like this too.
Mazy
"A useless but amusing fact is that if all the DNA in all the cells in a single human being were stretched out it would reach the moon and back eight thousand times." - Steve Jones
|
|
|
|
|
Has anyone ran into this before? When I try to load a xfdf into the ocx the control stops responding. The reader.exe has full support for xfdf, so I have not idea what to think.
|
|
|
|
|
I have a small library witch contains a few controls and one simple class. The class holds a vew variables and functions that the controls need and also manipulates with the controls itself (changes their text and color for example).
MyOwnPropertiesClass <---- Ctrl1/2/3 connect to this class.
| | |
Ctrl1 Ctrl2 Ctrl3
Because all controls in library exchange information through that one class, there must be only one instance of that class. It should be created just when the library is initialized or something like that. How is it possible ?
Best regards, Desmond
|
|
|
|
|
Create a singleton. Here's how:
Create a private static member variable of your class of the same type as your class and call it singleInstance or similar.
Create a static initialiser which is like a normal constructor but with the keyword static in front of it. In this method create a new instance of the class and assign it to the singleInstance member variable.
Modify your constructor so that it is private . This ensures that no one else can create your class.
Create a new static public get property called Instance or similar and return the singleInstance member variable.
The remainder of the class is as you would normally have it.
You have now created a singleton. Every time you want to use the class you access it through the Instance property. This version of a singleton will be created at application start up.
If you want to delay the initialisation until first use then you can remove the static initialiser and have the code for the Instance property check singleInstance == null in which case a new instance of the class is created and assigned to singleInstance first. Then it returns singleInstance as it did before.
Does this help?
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Thanks, this seems to be exacly what I need. However, could You please show me a little (code) example of a static initializer ?
And -- Where could I find more info on static initializers ? I did a quick search, but didn't result much...
Regards, Desmond
|
|
|
|
|
desmond5 wrote:
Where could I find more info on static initializers ?
Sorry, my fault - I was using Java terminology. In C# they are actually called static constructors. See this MSDN document[^]
[Edit]Ooops! I fixed some bugs[/Edit]
public MySingletonClass
{
private static MySingletonClass onlyInstance;
static MySingletonClass()
{
onlyInstance = new MySingletonClass();
}
public MySingletonClass()
{
}
public MySingletonClass Instance
{
get
{
return onlyInstance;
}
}
}
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Thanks. I also learned that these singletons has something to do with thread-safety (has something to do with multithreading) ... is this code compatible with that ?
|
|
|
|
|
desmond5 wrote:
singletons has something to do with thread-safety
I don't see why it cannot be used in a thead safety mechanism. However I've never used a singleton for that purpose.
For instance if you have a singleton that controls access to a shared resource then in each of you instance methods and properties (not the static ones) you can lock(this) to ensure that not other thread has access to your singleton while some sensitive operation is being carried out. See MSDN[^] for more information.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Or a shorter version:
public MySingletonClass{
public static readonly MySingletonClass Instance = new MySingletonClass();
private MySingletonClass()
{
}
}
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
public (static) fields EVIL!!!!!!
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
|
Arjan Einbu wrote:
The readonly keyword makes it work exactly the same way as the property getter.
If all you do in the property getter is return the member variable (field) then there isn't much difference.
As an absolute rule I never ever under any circumstances reveal the member variables of a class as public. Readonly or not. It breaks the OO rule on encapsulation.
What if you change the design of the class later on? You've publicly exposed the inner workings of the class! Using properties you can hide the inner workings, you can implement lazy lookup (my second suggestion employed a form of lazy lookup - the singleton is not created until needed), the innards of the class can change and the code for the property can coerce the expected value out so that existing code doesn't notice the difference.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Colin Angus Mackay wrote:
If all you do in the property getter is return the member variable (field) then there isn't much difference.
You did in your example... ;)
I know the rules of OO, but i believe rules can be broken sometimes if it serves a greater purpose. (Less code, fewer sources of error, greater maintainability.)
You're absolutely right that you might want to change your code later on, and if other assemblies access my field, they will need to be recompiled if that field later on is changed to a property. So if some other assemblies are going to use that singleton, one should consider wrapping it up in a property, as you say, Colin.
(Also, public fields cannot be made part of an interface definition, so there can be several reasons to make it a property.)
However, if the field is accessed only from within the same assembly (or assemblies that are compiled together with the containing assembly) it will make no difference if you change the field to a property. This is very often the case. (Though one should perhaps mark the property as internal instead of public in that case.)
Encapsulation is preserved i this construct. By making the instance constructor private, we make sure there will be made no other instances of the singleton.
Revealing the inner workings on this construct wont do any harm. The knowledge gained from it can't be abused to access inner data in other ways than the way we planned for.
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
Arjan Einbu wrote:
I know the rules of OO, but i believe rules can be broken sometimes if it serves a greater purpose. (Less code, fewer sources of error, greater maintainability.)
Many years ago I worked with a programmer that would take this statement and abuse it to the extent that everything would be public! He'd interpret it as: Less code == fewer sources of error == greater maintainability
You make a very good argument in this case for allowing the public readonly field. However by the time I thought through all the possible senarios for each member variable I create and decide whether to fully encapsulate it or decide that it is safe to expose I could have written a default compliment of getters/setters for them.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Colin Angus Mackay wrote:
Many years ago I worked with a programmer that would take this statement and abuse it to the extent that everything would be public!
Well, he didn't get it... neither about OO or maintainability.
I hope you understood it the way i meant it...
And yes; Creating the getter for this one probably is better, and isn't that much work. (Virtually none with the right tools[^] )
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
Desmond,
If you elected to receive response notifications via email the email you got will contain slightly incorrect code. I have corrected the code, but you need to view it on the forum.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
By the way, is there a way to "shorten the patch" to the singleton ? I mean, I have to write right now MainMameSpace.MyClasses.PropertiesClass.Instance.SomeFunction, but it would be much easier if it could be just MainNameSpace.PropertiesInstancel.SomeFunction.
Isn't it possible to access the ProperitesClass.Instance through a pointer or something ?
|
|
|
|
|
At the top of the file you can add using statements so that you can short circuit the path to the classes. If there is any ambiguity, for example two classes with the same name but in different namespaces, you will still have to use the full path.
For example:
using MainNameSpace;
using MainNameSpace.MyClasses;
then later in your code you can just put:
PropertiesClass.Instance.SomeFunction()
C# only permits pointers in unsafe methods. However if you are going to use the instance a lot then you can just assign it to a variable as normal. e.g.
PropertiesClass pci = PropertiesClass.Instance;
...
pci.SomeFunction();
I hope this points you in the right direction.
Regards,
Colin.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
hi, im using a datagrid generated from a XML file using the VS XSD builder. now i want to add a "virtual" colum that add's some extra information, i only need this information on the runtime so i don't wanna add it to the XML file.
i have read some examples with "BindingContext" and "Expression" but this examples are mostly DataGridBoolColumn and no TextColums.
dose anybody knows a tuturial or have some example code?
|
|
|
|
|
|
hi
i am try to do like this ....
i have a win form textbox and i am trying to write something like "jacal99" using threading with five seconds interval .... the value of thread was thread.sleep(5000);
but when the next charactor try to print in the textbox the first value gone.
i want to pring "jacal99" with five seconds interval .... every charactor...
thx.;)
Mazhar Hussain
|
|
|
|
|
If you post your code it might be easier to help.
Perhaps your assigning the value of Text property everytime and not adding to it:
yourTextBox.Text = "whatever"; //no
yourTextBox.Text += "whatever"; //yes
|
|
|
|