How do I prevent print screen

You can’t. The best you can do is render to a hardware accelerated device on an overlay, similar to what video players used to do. Basically, you paint your entire window blue, and render your graphics onto the video card, and internally the video card will replace the blue with the graphics. The downside to … Read more

How to subscribe to other class’ events in C#?

public class EventThrower { public delegate void EventHandler(object sender, EventArgs args) ; public event EventHandler ThrowEvent = delegate{}; public void SomethingHappened() => ThrowEvent(this, new EventArgs()); } public class EventSubscriber { private EventThrower _Thrower; public EventSubscriber() { _Thrower = new EventThrower(); // using lambda expression..could use method like other answers on here _Thrower.ThrowEvent += (sender, args) … Read more

How to unit test if my object is really serializable?

Here is a generic way: public static Stream Serialize(object source) { IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream, source); return stream; } public static T Deserialize<T>(Stream stream) { IFormatter formatter = new BinaryFormatter(); stream.Position = 0; return (T)formatter.Deserialize(stream); } public static T Clone<T>(object source) { return Deserialize<T>(Serialize(source)); }

Combine Multiple Predicates

How about: public static Predicate<T> Or<T>(params Predicate<T>[] predicates) { return delegate (T item) { foreach (Predicate<T> predicate in predicates) { if (predicate(item)) { return true; } } return false; }; } And for completeness: public static Predicate<T> And<T>(params Predicate<T>[] predicates) { return delegate (T item) { foreach (Predicate<T> predicate in predicates) { if (!predicate(item)) { … Read more

How to get xpath from an XmlNode instance

Okay, I couldn’t resist having a go at it. It’ll only work for attributes and elements, but hey… what can you expect in 15 minutes 🙂 Likewise there may very well be a cleaner way of doing it. It is superfluous to include the index on every element (particularly the root one!) but it’s easier … Read more

Redirect Trace output to Console

You can add the following to your exe’s .config file. <?xml version=”1.0″?> <configuration> <system.diagnostics> <trace autoflush=”true”> <listeners> <add name=”logListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”cat.log” /> <add name=”consoleListener” type=”System.Diagnostics.ConsoleTraceListener”/> </listeners> </trace> </system.diagnostics> </configuration> I included the TextWriter as well, in case you’re interested in logging to a file.

Control.AddRange(…) is slow

Posting this answer because the OP requested it: This is how you’d do something like that in WPF: <UserControl x:Class=”WpfApplication7.ListBoxSample” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”> <DockPanel> <Button Content=”Load” Click=”Load_Click” DockPanel.Dock=”Top”/> <ListBox ItemsSource=”{Binding}” HorizontalContentAlignment=”Stretch”> <ListBox.ItemTemplate> <DataTemplate> <Border BorderBrush=”LightGray” BorderThickness=”1″ Padding=”5″ Background=”#FFFAFAFA”> <Grid> <Grid.RowDefinitions> <RowDefinition Height=”Auto”/> <RowDefinition Height=”Auto”/> <RowDefinition Height=”Auto”/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text=”Dependent … Read more