|
For any object that implements IDisposable , you should always dispose of the object when finished. This includes (and definitely is not limited to) Bitmap and Graphics . In C#, you can use the using statement to make sure that objects are disposed even in case an exception is thrown:
using (Bitmap bmp = new Bitmap(32, 32))
{
using (Graphics g = Graphics.FromImage(bmp))
{
}
bmp.Save(...);
} This ensures that unmanaged resource (resources that aren't managed by the CLR, like native handles) are freed and that managed objects are disposed of immediately instead of the next time the GC (garbage collector) is run by the CLR.
On a note, if you call Environment.Exit finalizers (like destructors in C++...sort of) are executed but IDisposable.Dispose is not. This really isn't too important since Environment.Exit will unload the CLR and exit the host process - the OS will free all handles in use at this time. If any exceptions are thrown or you return execution to the caller, using the using statement will still dispose the object. The following is equivalent:
using (Bitmap bmp = new Bitmap(32, 32))
{
}
Bitmap bmp = new Bitmap(32, 32);
try
{
}
finally
{
bmp.Dispose();
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi all ,
As part of my preparation for an upcoming presentation on .NET reflection API
at the next Cochin .NET user group meeting , i have created a small four function
calculator [ + , - , / , * , ( . ) , unary + , unary - , numbers ] which can evaluate
arbitary mathematical expressions.
Aim of the code is just to show how one can use Reflection.Emit code to
create an assembly on the fly.
u may download a zipped PDF file from the following url
http://www.praseedpai.com/Misc/misc.html
I hope this will give an overview to a novice developer on IL code generation
Here is how u can use the code
----------------------------------------
Copy the contents of the PDF document on to a text file ( Expr.cs ).
use C# command line compiler csc to compile the code as follows
csc Expr.cs
u may execute the program like this
Expr "2*3"
Expr "-2+3*4"
Praseed Pai
P.S :- All the numbers are treated as doubles . No decimal points are allowed in
numbers .
|
|
|
|
|
I would suggest that you write up an article covering the material in your presentation. There are too many questions that get asked in this forum, your thread will quickly disappear.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi,
I've designed 2 custom controls (ColorButton & ColorRadioButton) that subclass off the original windows controls, but are composed of 3 images each (left cap, right cap, center slice). I need to build a set of subclasses of these buttons (ie OrangeButton,
GreenButton,...) which are identical to the parent except the bitmaps (which I have set up as properties) would be distinct. This works fine, but I don't want my base classes (ie ColorButton) to be visible in the Design view (from the toolbox). I tried making the base class protected, but that won't compile. How can I restrict one class in a dll from the toolbox while allowing the derived classes to show up?
thanks!
Deanna
|
|
|
|
|
there is an attribute you can apply to your components that will prevent it from being able to show and select into the toolbox, what it is called i cannot remember look in system.componentmold.design perhaps.
top secret xacc-ide 0.0.1
|
|
|
|
|
Actually, there are a few and they are in the System.ComponentModel namespace.
Those are BrowsableAttribute , EditorBrowsableAttribute , DesignTimeVisibleAttribute , and DesignerSerializationAttribute (a little less like the others, but can hide the code from the code serializer).
I'm not sure if these will help, though. Customizing the toolbox in VS.NET reflects the .NET assembly you opened and looks for Control derivatives. Whether or not that code makes use of the attributes mentioned above is questionable (only giving it a try would determine such behavior).
Another attribute that may work (though I've never used it) is the ToolboxItemFilterAttribute . This would at least make the toolbox item hidden if the filter string was specified to never match anything. I doubt this would prevent the control from showing up in the toolbox customization dialog, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Hy everyone!
I do want to generalize some comparestatements or assignstatments in my C# proggy. I managed to do so when I had a sender object (casting it) but now there is none and I am stuck.
My datastructure looks like this:
ldata contains the data
this looks like this
ldata[]
||=> keys[]
|| ||=> keyID //id of the field to display the data in
|| ||=> telnumber //the data to being displayed
||=> telefonID //the telefonnumber for which to display data
the telefonID should not matter, you can switch the numbers but only one of those is displayed at once, meaning you select one and the data for this number is displayed. But this doesn't matter concering my problem because at this stage the number has already been selected and I do just have to display the data for the selected number or check for changes before either switching numbers or just to save them.
My code looks like this at the moment:
for(li=0;li<ldata[0].keys.Length;li++)<br />
{<br />
if(ldata[0].keys[li].keyId==1)<br />
{<br />
txtNameKey01.Text=ldata[0].keys[li].telephoneNo;<br />
}<br />
}<br />
<br />
for(li=0;li<lTelephone[0].keys.Length;li++)<br />
{<br />
if (lTelephone[0].keys[li].keyId==1)<br />
{<br />
if (lTelephone[0].keys[li].telephoneNo!=txtNameKey01.Text)<br />
}<br />
}
But well this isn't nice and even harder to change lets say to 24 TextBoxes, but well it works . What I do want to do now is change the thing above to something like
for(li=0;li<ldata[0].keys.Length;li++)<br />
{<br />
TextBox=ldata[0].keys[li].telnumber;<br />
}
The pseudocode above and beneeth should describe what I want to do. I do want to create a general object/variable/... whatever to just having to assign or compare in one statement. I do want the keyID to describe where the data should be sent to or which fielddata should be compared to.
With strings you are able to copy parts together like "hello"+"world"+year.ToString() => hello world 2004 but this doesn't work with assignments, does it?
for(li=0;li<lTelephone[0].keys.Length;li++)<br />
{<br />
if (lTelephone[0].keys[li].telephoneNo!=TextBox.Text)<br />
}
in the pseudocode above once again it should be using the keyID to decide which TextBox I am really doing the comparison with.
But at the moment I do wonder if this is really possible or if I have to live with this if-statements and with tons of code for comparison and assignments!
Thanks!
Stephan.
|
|
|
|
|
If I got it right, you can easily do it by creating an array and put references to your textboxes there:
<br />
TextBox []theArray = { TextBox1, TextBox2, TextBox3, ..., TextBox12 };<br />
Then your if becomes
<br />
if (lTelephone[0].keys[li].telephoneNo!=theArray[i].Text)<br />
If an Array is not enough, you can use a Hashtable, an ArrayList, and so on.
People often overlook the fact that controls are simply objects like any other object.
Yes, even I am blogging now!
|
|
|
|
|
Thanks, it works!
I could have thought about this (enumbering the elements) myself, especially after having read it when trying to find a solution but throwing it away thinking it doesn't fit in here!
Stephan.
|
|
|
|
|
Hi there,
One of you bright sparks will know this off the top of your head (any supporting links you have greatful!)
Question: Do MS Collection Class Enumerators use strong reference to keep collection alive?
Background: Want to create collection in delegate function and set IEnumerator references from the parameters of the function to collections created in the function. This is so the delegate can use whatever collection it likes internally - as long as I can enumerate through the results on the other side it doesn't matter.
If MS use Weak Reference in enumerators then that would mean the collection isn't guarnteed to still be there when I go to use the IEnumerators. If it's a normal strong reference then that should keep the collection safe from the clutches of the GC.
Thoughts and answers greatly appreciated!
/**********************************
Paul Evans, Dorset, UK.
**********************************/
|
|
|
|
|
|
Hi all,
i am writing a small custom windows forms application in c# which will ultiamtely be for sale - i hope
i am wondering...when i have created the application i want to control access to it via a license key which i could provide. the user could enter this and it will (validate) and allow them to legally use this software. i want it to be a one of license key validation.
as i am new to c# please excuse my lack of knowledge - can anyone give me any tips on how to handle the above? i have noticed the license manager and License Provider key words but i am unsure where to start
i would like to have some sort of initial entry window allowing my key i provide to be used only on that host pc...i.e. i want to somehow tie the license key to the host when they first install??...
any advice/code/tips no matter how small is appreciated
|
|
|
|
|
There are so many ways of doing this, but you really need to research the problem. Keep in mind that anything a user installs on their local machine is possible to crack. All you can do is make it hard for them. Unfortunately, due to the nature of .NET assemblies, they can be disassembled, changed, and reassembled. Even relying on a native executable (C/C++ DLL) can be gotten around by disassembling the .NET assembly into IL, removing the call(s) to your native executable, and reassembling it (all using tools provided even in the .NET Framework SDK).
The LicenseProvider and related classes are more for design-time components (although a runtime option exists, I'm not sure it's currently supported). You can read more about these in the .NET Framework SDK (just type "LicenseProvider" in the .NET Framework SDK documentation that is installed with the .NET Framework SDK, which is installed by default with VS.NET).
I wrote an article you might want to take a look at, entitled Using XML Digital Signatures for Application Licensing[^]. This is one way you can implement both design-time and runtime licensing (though the actual implementation is up to you, but I do discuss various methods).
There are many others.
For instance, there is a pretty good library we evaluated from XHEO[^] that does - for some types of licensing it supports - use a similar idea for what I wrote about above. It also supports going out to a server on the 'net and verifying a license (a little more secure, but once again that could be removed if disassembled).
Basically, what I'm trying to say is that .NET applications are nearly as secure as natively compiled applications (decompiling these is extremely difficult and can only be done with best-guest mechanisms that are a long way from complete). All you can do is make it hard for the user and keep in mind that most users won't have the slightest clue about how to circumvent your application (though one crafty person could post how or post cracked binaries).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey,
This is my first post here, I am trying to find the name of a program given a .exe file.....I have been banging my head against this for the last few days and am alomst going insane.
Any help would be appreciated.
I have the full path of the file eg c:/windows/sample.exe and from this i am trying to get a program name that I can output for a user to see.
Cheers
|
|
|
|
|
The simple answer is that you cannot.
What you see in the Start Menu and on icons in the Quick Launch bar and Desktop are the result of shortcuts (.lnk files)
If you are the author of the programs then you can write a mechanism that will expose a user friendly name, otherwise there is no consistent or reliable way to do that for third party applications.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
Firstly, thanks for the quick response.
But surely, there must be a way.
I have retrived the icon using SHGetFileInfo() in shell32.dll, i have a full path name, etc...
Where and how, for instance, does XP get the description field in file properties when you right click on a file, or in this case executable? I know this information is only stored when running an NTFS, but even that would be a start.
Or where does the value for the registry entry in MUICache come from? I tried searching the registry for a reliable location, without any luck.
????
Thanks from,
Slowly going insane.....
|
|
|
|
|
There is a way, but keep in mind that it may not be the actual application title. That is set by the application at runtime only and it could be hard-coded into their source, grabbed from a resource string, or just about anything else.
Some of the information displayed in a file's properties (in the "Version" tab for an executable) can be obtained from the FileVersionInfo class like so:
FileVersionInfo info = FileVersionInfo.GetFileVersionInfo(
"%SystemRoot%\notepad.exe");
Console.WriteLine(info.ProductName); See the documentation for the FileVersionInfo class in the .NET Framework SDK for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I need to serialize a COM object from the .NET environment. Can anyone help me?
|
|
|
|
|
That really depends on what interfaces the COM object implements. If it implements any of the IPersist * interfaces (IPersistFile , IPersistStream , IPersistStreamInit , etc.) then you need to declare those interfaces (the .NET BCLs already declare the IPersistFile and IStream interfaces as UCOMIPersistFile and UCOMIStream , respectively) where the methods are in the right order and the interfaces are attributed with the ComImportAttribute , the GuidAttribute (with the right IID of the interface), and the right InterfaceTypeAttribute (to declare it as an IUnknown , IDispatch , or dual interface).
You cast the COM object in managed code (assuming you have a reference to it in managed code) to one of those interfaces in order for the CLR to do a QueryInterface for that method. Then you call Load , Save , or whatever is appropriate and pass the filename, an IStream implementation, or anything else that is required by the IPersist * interface.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
Im trying to implement a function where a message will pop up, say a week before a particular expiry date and every day after that, until the expiry date..can anyone tell me whats the best way to go around this..and is there a namespace to make this function easier?
CODER
|
|
|
|
|
ASGill wrote:
Im trying to implement a function where a message will pop up, say a week before a particular expiry date and every day after that, until the expiry date
That is a bit vague. What is the mechanism for determining the expiry date. At what point is the message to pop-up? (Application start? Or is is supposed to sit in the background somewhere until it needs to display a reminder?)
ASGill wrote:
and is there a namespace to make this function easier?
What do you mean by this? Do you mean is there a class to make this easier? Perhaps the System.Threading.Timer[^] class would be helpful.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
well the message is supposed to sit in the background and pops up when it needs to display a reminder.
thx for the information abt the class...ill look into it.
CODER
|
|
|
|
|
Look up the System.PopupWindows.ExpiryDateNotifyBallon class
|
|
|
|
|
Hi,
I want to convert variables of type double to type float in my C# app.
I've read that casting from a double to float results in the double being rounded to the nearest float value. That is exactly what I need (as opposed to truncation).
However, I've also seen that the Convert.ToSingle(double) method uses rounding as well to convert the double to a float.
Is there any difference between these two techniques - casting and ToSingle? Are there scenarios in which one is preferable over the other? What about overhead?
Thanks!
|
|
|
|
|