Posts

Showing posts from 2006

How To Align Text in an ASP.NET TextBox

(This applies to the .NET Framework v1.1. I'm not sure if things have changed in v2.0. I recently needed to right-align the text in an ASP.NET TextBox control, but the control lacks any sort of text-alignment property. There are, however, two workarounds I found for this issue. The first is to simply add a style attribute to the control. This can be done as illustrated in the example code below, where txtKeyCode represents an ASP.NET TextBox (this is server-side code): txtKeyCode.Attributes.Add( "Style", "text-align: right;" ); The second workaround is more involved but offers more flexibility. It entails using a stylesheet and creating a style class, then assigning this class to the control. With this method, not only can you align the text, but you can also specify all sorts of other properties as you usually do in a stylesheet. This is demonstrated below: (In the stylesheet) .RightAlignedTextBox {    font-size: 12pt;    font-family: Arial;    text-align

TreeViews and Multi-Threaded Madness!

Disclaimer: The following information is for educational purposes only. Don't use at the track. I'm currently working on an app (in C#) that performs three, related tasks. Each of the tasks is performed by an object, and the app's main form has as member variables these three objects. The form also spawns three threads, one for each object, so the tasks can be performed simultaneously. The objects in turn have events, which are handled by the main form. These event handlers take care of, among other things, updating the controls on the form to display the status and progress of the various tasks. One of the controls is a TreeView. I ran into an interesting predicament a little while back when setting these event handlers up: when fired, the events could call the functions within the form class to remove nodes from the TreeView, or alter the text of the nodes, but when they tried to Add nodes, I received the following exception: "The action being performed on thi

A C# IsNumeric Function

As much as I love the .NET Framework and C# (and VB.NET too), sometimes I just have to scratch my head and think "Why?" For example, why no InputBox() function in C# (click here for my custom C# InputBox class)? Also, why no IsNumeric() function in C#? VB has had one for years. So, I set out to write my own. My first inclination was simply to have a function that would attempt to convert a value from either an object or string to a numeric type, wrapped in a try block, and return false if an exception was caught. But as another developer pointed out to me, and as I experienced first hand, when the exception is thrown there is a noticable pause. A much better solution is to use the TryParse() method of the Double class. The TryParse method attempts to convert a value to a double, but instead of throwing an exception if the conversion fails, it simply returns false. This seems like enough right there, but the function takes an out parameter, so you need at least two li

Simulating VB's App.PrevInstance in VB.NET and C#

Disclaimer: The following information is for educational uses only. Please do not use to leak classified information. Versions of Visual Basic prior to VB.NET had an App object (not to be confused with .NET's Application object) which exposed some useful functionality, including the PrevInstance function, which indicated whether or not a previous instance of the application was currently running. This function does not exist in VB.NET, but the functionality itself is present. This posting will show you how to use it, both in VB.NET and C#. To determine if a previous version of the application is running, we can use the Process object contained within the System.Diagnostics namespace. Specifically, the Process object's GetProcessesByName() method provides the functionality we need. Take a look at the C# code below (a using System.Diagnostics; declaration is implied): public int iGetProcessCount( string ProcessName ) {    Process[] proc = null;    try    {       proc = Proces

Loading an Image from an Embedded Resource in .NET

Disclaimer: The following information is for educational purposes only. Do not use to harass muppets. This is one of those things that I learned to do back when I first started learning .NET, but can never remember how to do when I need it, so I'm adding it here. Hopefully this will come in handy to someone. First, add an image to your project. You can do this by right-clicking on your project in the Solution Explorer and selecting Add->Add Existing Item , then browse for an image file using the dialog that appears. After the image has been added, click on it in the Solution Explorer and set its Build Action to Embedded Resource . Adding images to your project this way is useful for when you deploy your assembly -- it removes the need to distribute individual image files, instead embedding them into your assembly. Next, use the following line of code to load your image from the embedded resource (examples are given in both C# and VB.NET. Please note that the below examples

Find a Running Process in .NET

Disclaimer: The sample code below is for educational purposes only. Do not rub on skin. A few years back I wrote a class module in VB6 to allow an application to detect whether a specified process is currently running on the user's system. This required a lot of API code, and, while it gets the job done, it's a bit unwieldly, especially if you're debugging an app that uses it and you end up stepping through it's code rather than stepping over it. But there's a much easier way to do it in .NET, with only a few lines of code. The answer lies in the System.Diagnostics namespace, and the Process class contained within. Why they put the Process class in the Diagnostics namespace is beyond me -- sure, you can use it for diagnostics, but also for, among other things, executing processes. But for this example, we'll focus simply on determining if a specified process is running. First, you'll need to add a reference to the aforementioned System.Diagnostics na

Custom C# InputBox Class

Disclaimer: The following source code is for educational purposes only. Please do not use for evil. I have a small project in C# which I use for testing out various things I find when going through the .NET namespaces. It's a good way to learn new things, and on more than one occasion I've tinkered with something that came up later in a work-related project, thereby saving me the time it would take to learn it on-the-job. Anyway, I was working with this app the other day and was reminded of something when I needed to prompt the user for a string: C# does not have an InputBox function/class. So I wrote my own. This is a very simple example, but just because it's simple doesn't mean it's not useful. First, create a new form, identical to the standard InputBox that VB has. Give it a label for the message to the user, a text box for user input, and two buttons, one labeled "OK", the other "Cancel". Set the form's AcceptButton property to the OK b

Cool Programming Functionality: Windows Management Instrumentation (Part Two)

As a follow-up to my previous post, here is some sample code in C# which demonstrates how to use Windows Management Instrumentation (WMI) to gather information on a system's logical drives. The code is from a class I am working on ( in a personal project, not work related ) which will eventually contain several useful functions pertaining to file and drive operations. Eventually, I would also like the function in this code to return the caption of the device (for example, Memorex 2.4x DVD Writer ). using System; using System.Management; namespace AlsFileSystemTools {    public enum DriveType     {       NoRoot = 1,       Removable = 2,       LocalDisk = 3,       Network = 4,       CDorDVD = 5,       RAMDrive = 6     };     public struct Drive     {       public string Letter;       public string Description;       public int       DriveType;       public string VolumeName;       public string StatusInfo;     }     ///     /// A collection of functions used to