|
|
I thought to use the event TextChanged, detaching and attaching again(durring color changes)
Any other better idea ?
void rTBOnContentChanged(.....) <br />
{ <br />
.................... <br />
.................... <br />
rTB.TextChanged -= new EventHandler(rTBOnContentChanged) <br />
.................... <br />
rTB.TextChanged += new EventHandler(rTBOnContentChanged) <br />
}
|
|
|
|
|
That code would load the processor tremendously. The TextChanged event is fired very often and attaching and detaching event handlers is expensive.
Instead of doing that, just use a state variable. In your handler (and why are you using the same handler which would make changes to the text, thus calling the handler again in a loop), just check if a state variable (herhaps a bool ) is set or unset (true and false , respectively). When you change the color, set the state variable, when you're done making changes, reset it. The handler would check the state of this variable and conditionally execute.
State variables are used everywhere in some form or fashion, and would be best to use here.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you very much Heath Stewart.
|
|
|
|
|
Hello,
I'm trying to solve the following. I've got a couple of say Books, Videos, Magazines. The classes are contained in various collections (derived from CollectionBase, but this is actually not importand). Now I'd like to go and write a generic Loader, which will populate a Listview Control with the items in a Collection. E.g. we'd like to load the BooksCollection. Since I'm having not only three Element-Classes, but say 50, I don't want to write a Loader for each class on its own. I'd rather like to check what kind of public fields the class has got and add the appropriate columns and values to the Listview. Question is, how can I retrieve the public fields of a class at runtime. I guess it has got something to do with reflection, but this is like a big wide desert to me. Any starting point?
Thanks in advance,
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
Start by reading Discoverying Type Information at Runtime[^] in the .NET Framework SDK.
If I were you, however, write a little more elegant solution (still using either reflection or the ComponentModel, which is a little easier to use - see the class documentation for the TypeDescriptor for a starting point). Derive all your collections from a single base class (perhaps CollectionBase or some other base class that exposes a common base class for all your objects, which might define common properties - don't use fields - like ID and Name , for example).
Also define an Attribute -derived class for fields you want to be mapped to columns (perhaps with an optional column heading for better control) like so:
[AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute : Attribute
{
string heading;
public ColumnAttribute()
{
}
public ColumnAttribute(string heading)
{
this.heading = heading;
}
public string Heading
{
get { return this.heading; }
}
} As you're enumerating properties, only show properties attributed with that attribute in the ListView , optionally using the Heading for the actual heading text (in case the property name to heading name mapping wasn't the same). You could even define some sort of order with the attribute for additional layout capabilities.
This would be attributed on properties of the objects being collected, however. The collection is rather moot (you could have one that just accepts the base class of all the other types, for example).
As far as loading this information, that really depends on what you're loading it from. The collection could be written as a smart class that knows which types to create based on the information presented to it (the navigational tree in our flag ship product works this way). You could go a step further and use a provider pattern to modularize how that information is obtained (having ADO.NET clients to get it from a database, XML Serialization providers, etc.).
XML Serialization may also be a good idea to use. Read XML and SOAP Serialization[^] in the .NET Framework SDK for more information (you can ignore the SOAP information, as that commonly applies to Web Services and .NET Remoting and is just a particular grammar of XML).
There's a lot of things you could do, but it's important you understand the concept of polymorphism: types deriving from other types that can override some information, basically. Implementing a good object model can save you a lot of time and create a more elegant solution that is easier to change, extend, and understand.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
thats more then I've dreamed to expect. Thanks a lot for your explanation. In the meantime, I have been writing some test-code which uses Reflection (no Attributes, I will think about this later on) to determine the type of items in the collection and the properties of them. It all works fine. My problem now is, that i'd like to access the value of a property of a given instance. Say I know that Car has a property MaxSpeed (because I've been using the Type.GetProperties() method). How can I access an instance' values if I only have the PropertyInfo?
Thanks again, be sure to get my 5!
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
Type.GetProperty returns a PropertyInfo , which defines a GetValue method. You pass this the instance of the object you're reflecting (or null for a static property) and either null for the second param (depending on which overload you call) or an object[] array for an indexer (the only property type that can accept arguments):
Button b = new Button();
b.Text = "Click me";
PropertyInfo prop = b.GetType().GetProperty("Text");
string text = prop.GetValue(b, null);
Console.WriteLine(text); I recommend looking at the System.Reflection namespace member documentation. I also recommend you take a look at System.ComponentModel . Using the ComponentModel encapsulates reflection in a way that's easier to use and design-time friendly (if applicable to what you're doing).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hy everyone!
I know it is possible to access any folder in Outlook via MAPI but I do want to check if the opend folder is lets say the Inbox folder.
Or is it possible to check if an item (e.g. a contact item) was selected?
Because I do want my button to be only selectable/clickable when one of those match. Meaning when any other folder is open then the button should appear grayed out.
Thanks!
Stephan.
|
|
|
|
|
I'm not sure what method of MAPI you are using but
Stephan Wright wrote:
I know it is possible to access any folder in Outlook via MAPI but I do want to check if the opend folder is lets say the Inbox folder.
This is stored in the PR_CONTAINER_CLASS property.
Stephan Wright wrote:
Or is it possible to check if an item (e.g. a contact item) was selected?
PR_MESSAGE_CLASS property identifies the type. A contact is IPM.Contact or occasionally IPM.Contact.SBE
Michael
CP Blog [^]
|
|
|
|
|
thanks for the hint but I do still have two questions:
1) Where is this PR_CONTAINER_CLASS located in? Because I can't find it
2) Together with this class: Is it possible to send the data to an application via argument that way or am I just able to check if it is this type. Or is it possible to assign it somewhere?
meaning is it something like
if(item.type==IPM.contact.SBE)
.....
or do I get access (a pointer?) to the object to use it for something else?
Thanks!
Stephan.
|
|
|
|
|
What method are you using to access the MAPI data?
I'm only familiar with using Extended MAPI via C++. PR_CONTAINER_CLASS is a property of the MapiFolder data. It isn't a class, its a defintion for the mapi property id.
Michael
CP Blog [^]
|
|
|
|
|
the way I get there is
new application of Outlook then namespace of MAPI
By the way: My programminglanguage is C# in Visual Studio, but well it should be all the same in Visual Studio!
Stephan.
|
|
|
|
|
Ok. You are using the Outlook object model. Sorry, for leading you down the wrong path.
If you have a MapiFolder object then you can use GetDefaultItemType() method to get the type of items in the folder.
I can't remember all the values but a google for Outlook GetDefaultItemType should return some more details.
Michael
CP Blog [^]
|
|
|
|
|
hi,
I'm developing a mediaplayer, and I need to disable the screensaver and the poweroff functions while playing a video file.
on msdn I found that I have to use the SystemParametersInfo[^] function.
This code works only partially: the screensaver is disabled, but the monitor is still switched off by the system.
[DllImport("user32.dll")]<br />
private static extern bool SystemParametersInfo<br />
(<br />
uint action,<br />
uint param,<br />
object data,<br />
uint winini<br />
);<br />
<br />
private const int SPI_SETSCREENSAVEACTIVE = 0x0011;
private const int SPI_SETPOWEROFFACTIVE = 0x0056;
<br />
private void someMethod()<br />
{<br />
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0); <br />
<br />
SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 0, null, 0);<br />
}
what's wrong?
thank you
________
http://www.jockersoft.altervista.org
|
|
|
|
|
|
Nope - if you read the documentation for SystemParametersInfo , it refers to the power phase of the screensaver.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
The SPI_SETPOWEROFFACTIVE may have more to do with the system suspension state than the monitor. The documentation is a little vague on this matter.
I did try a few things. Handling the WM_POWERBROADCAST message with the PBT_APMQUERYSUSPEND only seems to work with the power state, so the next logical choice is something like this:
P/Invoke both GetActivePwrScheme and SetActivePwrScheme , as well as declare the POWER_SCHEME structure (you can flatten it if you like, since unmanaged structures are just blocks of contiguous memory). Information about these functions and this struct can be found in the Platform SDK at http://msdn.microsoft.com/library[^]. You can pass null (or IntPtr.Zero ) to the second parameter (declare it as an IntPtr in order to pass IntPtr.Zero ). If you do so, the signature would look like this:
[DllImport("powrprof.dll")]
static extern bool SetActivePwrSchema(uint id, IntPtr globalPolicy,
ref POWER_POLICY powerPolicy); First call GetActivePwrScheme to get an ID for the current policy. Change the POWER_POLICY.user.VideoTimeoutAc (typed as it's named in the Platform SDK) to either 0 or UInt32.MaxValue (not sure which) to set the power state of the monitor to off or a relatively indefinite time (far more than the life of a computer), whichever works (such values aren't documented).
Don't forget to clean-up your code, however, like storing the previous VideoTimeoutAc value and doing this over with the previous value. This will merge the previous state with the new state, but there's nothing I see in the documentation that dictates whether that merged state is persisted in the power scheme or if its just for the duration of the computer's up-time (still always a good idea to undo what you've done, though).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You might want to check out SetThreadExecutionState[^]. Calling this function with all three flags set will let the system know that your application needs the system to be completely powered up at all times.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
thank you Heath and Dave for your hints.
I followed Dave's one (much simpler) and it works.
here is the code that does the job:
[DllImport("Kernel32.dll")]
private static extern uint SetThreadExecutionState
(
uint esFlags
);
private const uint ES_SYSTEM_REQUIRED = 0x00000001;
private const uint ES_DISPLAY_REQUIRED = 0x00000002;
private const uint ES_CONTINUOUS = 0x80000000;
private void disablePoweroff()
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS);
}
private void enablePoweroff()
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
}
thank you again,
bye
________
http://www.jockersoft.altervista.org
|
|
|
|
|
Hy everyone!
I implemented an addin in Outlook which calls an executable when the button in Outlook is clicked.
Well this isn't the problem. My problem is how to check, if there is already an instance of the programm, meaning avoiding the user to start it twice or more often e.g. displaying "application already running" or bringing the application to the foreground.
I tried to add a kind of counter in the method but it did not work.
My application is called via the Process.Start() command.
Thanks.
Stephan.
|
|
|
|
|
Stephan Wright wrote:
I tried to add a kind of counter in the method but it did not work.
By design it wont. No data is shared between application, unless u use remoting to connect to them.
Stephan Wright wrote:
My application is called via the Process.Start() command.
Give the process a name or some ID , and enumerate thru all the processes before starting yours.
top secret xacc-ide 0.0.1
|
|
|
|
|
Hi stephan,
Firstly, i'm sure there is a better way of doing it than this but one of the programmes we use at work does it this way and it works for us, it creates a "Lock" file.
When the instance loads it first checks for this lock file, if it doesn't exist the program loads and creates the lock file, if the file already exists then an instance is already running so the program can just display an error and shut down.
When the instance finishes running just delete the lock file.
Like i said, not the best way of doing it and i'm sure someone will post the correct way to check but this is simple and as long as it's implimented correctly it will be effective.
Kev Pearman MCP
|
|
|
|
|
|
Using a mutex is more common to solve such a problem, as the next reply after yours recommended. Lock files can lead to problems, especially when they're not cleaned-up appropriately (i.e., the CLR crashes because of problems in the application).
Microsoft MVP, Visual C#
My Articles
|
|
|
|