Enable Double Buffering for Controls to reduce flickering
Some controls do not have their DoubleBuffered
property listed in the properties window. For this reason it requires an additional step to enable double buffering for specific controls such as a ListView control.
If you wish to enable the DoubleBuffed
property of a control simply use the method provided below.
1 2 3 4 5 6 | public static void SetDoubleBuffering(System.Windows.Forms.Control control, bool value) { System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control) .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); controlProperty.SetValue(control, value, null); } |
Example
1 | SetDoubleBuffering(listView1, true); |
Posted in C#.
Tagged C#, csharp, double buffering, DoubleBuffered, snippet, winforms
Clear download list in Firefox
Firefox by default never clears the download list automatically. If you wish change the way Firefox works so it will clear the downloads either automatically upon download complete or when Firefox closes following the next steps.
- Type about:config in your address bar
- Search for browser.download.manager.retention
- Set its value to either 0, 1 or 2 depending on how you Firefox to behave
0 indicates that the download should be removed upon completion.
1 indicates that completed and canceled downloads should be removed when Firefox closes.
2 indicates that downloads should never be removed automatically. (default)
Posted in Software Tips.
Tagged firefox, firefox download manager
Delete files or folders to recycle bin in C#
Sadly C# doesn’t have the required libraries in place to aid the users or the developers to send files or folders directly to the recycle bin. System.IO.File.Delete()
and System.IO.Directory.Delete()
simply delete the path without allowing for the action to be undone.
Luckily there are two ways this can be resolved. Either by using the Windows API SHFileOperation
function or by referencing Microsoft.VisualBasic
and using the Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile()
and Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory()
methods.
Personally I prefer the Microsoft.VisualBasic
approach as the code looks much cleaner and is more readable to me but I will demonstrate how both methods can be implemented either way.
The Microsoft.VisualBasic approach:
Reference Microsoft.VisualBasic
in your project and simply call
1 2 3 | Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(@"C:\Users\CooLMinE\Desktop\test.txt", Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin); |
or
1 2 3 | Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(@"C:\Users\CooLMinE\Desktop\test", Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin); |
depending if you want to delete a file or a folder.
Posted in C#.
Tagged C#, csharp, delete, delete file, delete folder, SHFileOperation, SHFILEOPSTRUCT, snippet, winforms
Serialize an object to string and from string back to object
Serialization enables us to store or transmit data structures or objects. This can be done by converting the data to a textual representation when storing or transmitting them which also allows us to recreate the original object using the stored information.
Lets create a dummy class called Person, that will hold some basic information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Person { public string Name { get; set; } public string Surname { get; set; } public int ID { get; set; } private Person() { } public Person(string name, string surname, int id) { this.Name = name; this.Surname = surname; this.ID = id; } } |
Keep in mind that your class needs to have a parameterless constructor in order to be serialized or else you will get an InvalidOperationException
exception (cannot be serialized because it does not have a parameterless constructor) if you try to serialize a class without one. Luckily you can just set it to private or internal so it won’t be accessible from other classes.
Now it is time to serialize our Person object.
1 2 3 4 5 6 7 8 9 | Person aPerson = new Person("name", "surname", 1); // The Person object which we will serialize string serializedData = string.Empty; // The string variable that will hold the serialized data XmlSerializer serializer = new XmlSerializer(aPerson.GetType()); using (StringWriter sw = new StringWriter()) { serializer.Serialize(sw, aPerson); serializedData = sw.ToString(); } |
Posted in C#.
Tagged C#, csharp, deserialize string, serializate object, serializate to string, serialization, serialize, snippet, string to object, tutorial, winforms
Clear all Windows event logs
Windows logs a lot of information in the event logs to make it easier for users to troubleshoot potential problems with their system.
Each event log category has a default limit of 1028 kilobytes. Because of that it is possible for a category to store a few thousand entries dating back a few years. In my case I had entries that were older than 5 years. Obviously this might make it a bit harder to go through the logs, especially if you want to view the logs that were created after you made some particular change to the system which you want to verify that it did not cause any issues.
Sadly, there isn’t a default option to clear all the event logs at the same time and clearing up one category at a time is very time consuming due to the number of categories that are available. Luckily we can create a .bat file to do the work for us.
Open up your favourite text editor, insert the snippet below and save it with the extension .bat
. You can give it any name you want. Then simply run the file as administrator and it will clear up all event log entries for you automatically.
1 2 3 4 5 6 7 8 9 10 11 12 | @echo off FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V IF (%adminTest%)==(Access) goto noAdmin for /F "tokens=*" %%G in ('wevtutil.exe el') DO (call :do_clear "%%G") echo. echo goto theEnd :do_clear echo clearing %1 wevtutil.exe cl %1 goto :eof :noAdmin exit |
Posted in Software Tips.
Tagged wevtutil, Windows, Windows event log