These samples show handling the clipboard in the most basic way. First a test is done to see if there currently is text on the clipboard. If a text is found, the programm proceeds to read and display the text, and then replace it with a new text. By modifieing the parameters you can use […]
Category: C#
Making a form transparant
This code sample displays a method to seemingly draw anywhere freely on the screen. The key to this technique is the TransparanceKey offered by the .net framework, which makes this a fairly straightforward task. To make the background of a form transparant, the *only* thing we need to do is this; oDrawform.AllowTransparency = True oDrawform.TransparencyKey […]
Update a winforms control from a background thread
This is the absolute minimum you need to update a control on the GUI thread from a background thread. The key here is the line Invoke(new Action<int>(UpdateGuiText), result); Which will invoke an update on the GUI thread from another thread. The interface consists of a single button and a single textbox. Each click on the […]
Hiding a control’s commandfield button at runtime
Hiding a command button at runtime can be archieved by casting the commandfield in the DataBound event, and accessing the buttons from there. The sample below shows the hiding of a button based on a security role. protected void DetailsViewDocuments_DataBound(object sender, EventArgs e) { // Check role string currentuser = Common.GetRequestUser(); // some user retrieval […]
Running a console command and capturing the output
This code sample demonstrates how to execute a console command and capture the output. The VB versions takes the “short route” using the shell command, while the C# version takes the approach of starting a process object, which gives you greater control over the proccess and enables you to pipe the outpout directly into the […]
Sending email with an inline image
While you will usualy link images to a static location, there are situations where it can be very practical to include the actual image in the message and send it inline. This code snippet is a demonstration of the minimal code required to send an email message with an inline image using C#. using System.Net.Mail; […]
Moving focus when right-clicking on a datagridview
The .NET framework integrates a context menu that easily can be bound to any datagridview. By default, this context menu operates on the “selected row”. But what if you’re user right-clicks somewhere else on the grid ? He will still get an context menu that operates on the selected row, even tho it appears on […]
Collecting performance data from local and remote systems using C#
Performance data Perfmon is a well known tool to monitor performance data for your local and remote systems. This code snippet gives you a worker class that can be used to connect to local or remote perfmon / system performance data, and provide the resulting information stream to your c# application. Local and remote To […]
Enumerating available performance counters on local and remote systems using C#
Performance counters Perfmon, the well known tool for monitoring performance counters has a nice list of categories and counters for your system. But what if you want to access a counter from code ? Or even worse, want to check if a certain counter is available or not ? In the previous document Collecting performance […]
Creating a threaded windows service
Below is a more or less empty sample from a window service project, you can build in Visual Studio. The service itsself (created by the VS Project) has events for Start and Stop command, which you use to hook your worker code into: Example of an empty worker, which does not do more then writing […]