Need to check a property of a Windows.Forms.Control

Hello!

Great forum btw!

I need to check some properties of a text box on a form, let say theForeColor.

so far, I got the name and the type of the member, but I don't know where to look to get the value of theForeColor property.

Can anyone help me?

Thanks

[337 byte] By [Sypher723] at [2007-12-20]
# 1

You can certainly do something like this using Reflection, if you have a valid object instance. In our static checker, however, you don't have an object that can be queried for a specific value. If you want to accomplish something statically, you'd need to follow the initialize component logic to see if the ForeColor is set to any particular value.

I don't know your scenario, but it sounds as though you might be looking for more of a dynamic analysis, where you could point a tool to a live window and do some checking. Is that true? We use tools to do this sort of thing in our test effort, but it's not an FxCop scenario.

Michael

MichaelFanning-MS at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2
Michael Fanning - MS wrote:

I don't know your scenario, but it sounds as though you might be looking for more of a dynamic analysis, where you could point a tool to a live window and do some checking. Is that true? We use tools to do this sort of thing in our test effort, but it's not an FxCop scenario.

Yes,that's true, I need a dynamic analysis, and I though FxCop could helped me. Because I only needed the instance of the object, and I was sure it was buried somewhere deep down the Reflection librairie.

What are the tools you are using? Can you give me some names? because I am not aware of any tools that can check what I need to check. (I was about to build one...)

Thanks for you answer

Sypher723 at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 3

The tools that we use for dynamic (runtime) analysis are all internal tools that are not shipped externally.

Most of the ones that target the .NET Framework assemblies are Reflection-based (ie System.Reflection) and it shouldn't be too hard to write up a quick framework that did something similar.

DavidM.Kean-MSFT at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 4

thank you, you were quite useful.

keep up the good forum!

P.s Sorry about the font last post, I guess I wrote it without my glasse

Sypher723 at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 5

Actually its pretty easy to do this kind of checking if you don't mind being evil. Personally I like being evil! All you have to do is create an instance of your form. This was easy before .Net 2.0 because you would just do a:

(Form)Activator.CreateInstance(type.GetRuntimeType());

"Unfortunately" in .Net 2.0 an exception may be thrown if you create the form on a thread that is not ApartmentState.STA. The thread that runs the FxCop rules is not STA so you have to create a new thread and then run your rule's "check" method. Here's my helper class for this:

public static class StaThreadHelper

{

public static void DoWork(ParameterizedThreadStart workMethod, object value)

{

Thread thread = new Thread(workMethod);

thread.SetApartmentState(ApartmentState.STA);

thread.Start(value);

thread.Join();

}

}

In custom rule code should look something like this:

public override ProblemCollection Check(TypeNode type)

{

if (type.IsSubClassOf(typeof(Form)))

{

StaThreadHelper.DoWork(this.CheckForm, type);

return this.Problems;

}

else

{

return null;

}

}

The CheckForm method can then create an instance of the form (as in the first code chunck) and iterate through controls, etc.

This is EVIL, but makes a bunch of stuff easier. For example if you set Localizable = true in the designer for your form the InitializeComponent method is completely different. If you were going to do static code analysis you would have to process this as well as the resource files the property values are stored in.

I hope this helps in some way!

JLeBert at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified