Posts

Showing posts with the label C# Winform

Toggle Keys Controller

Overview A couple of days before I wrote this article, a member asked how to control the Caps, Num and Scroll lock keys. I knew there were several implementations around so I searched with a view to providing the poster with a link, but I couldn't find one that was complete. Many just read the state of these keys, some also allowed you to set them, but I couldn't find any that monitored them so your application was updated accordingly. So, this article is the result. The ToggleKeysController class in the code attached will monitor these keys and raise events when a change is detected, and allow you to get or set the state. Unanticipated Problems This isn't quite as simple as I first thought it would be! The first thing to do was to get the state of the keys. I knew the Console class had a CapsLock property so I figured it would probably be the answer. Unfortunately, it doesn't have a ScrollLock property. The next step was to set the key states. The one...

Custom Drawn Scrollbar

Image
Introduction I was working (in my free time) on a calendar control which looks similar to the one in Outlook 2007 and I encountered, among other problems, that the standard scrollbar doesn't fit nicely with the look of my calendar control. So I searched the Web and CodeProject for custom drawn scrollbars. Among others, I found the article from Greg Ellis . Although it pointed me in the right direction, I missed some things the system scrollbar is capable of. As a last resort, I searched in the help of Visual Studio and found a simple example of a custom drawn scrollbar which I took as a starting point. Some Remarks This control is mostly useful for developers who build their own custom drawn controls and need a scrollbar. The scrollbar has one major shortcoming - the width is fixed. I didn't need this feature and the drawing would be more complex, so I left it out. Because I used collection initializers, the project builds only with a C# 3.0 compiler and upwar...

Reorderable ListView

Image
Introduction This work just extends another work I found here at CodeProject, and is inspired mainly with the article "Manual reordering of items inside a ListView" . What is new in this project is that it is compact (everything needed is part of the inherited control), and that it supports dragging multiple items and automatic scrolling, which I shaped by playlist of Foobar 2000 multimedia player. In this article, I will not describe everything in detail, on the other side I will describe all extended behavior, which I intended to implement. Drag-drop Construction As you probably found already, some coders, including MSDN writers, prefer to use built in Drag and Drop methods of ListView to implement Reorder by Mouse function. I found this quite limiting, and bringing no advantages at all. It seems also that we can ask, what does reordering have to do with clipboard. I guess the answer is pretty clear, until you need external data to enter ListView . So I ...

Glass Effect Extender Library for your Applications

Image
Introduction As is known, the first noticeable thing when moving from Windows XP to Windows Vista or Windows 7 is the enhanced graphical user interface, and if your hardware is good enough to support the Windows Aero Theme , you'll get great looks for the window title bars (see the figure below): But if this theme is not enabled due to lack of good enough hardware, or is manually disabled by the user, the window titles will appear like this: And here, we are going to get some control of this feature (the Glass Effect of the window titles and frames). Window Desktop Manager In the old days of Windows XP and earlier, each window was responsible to draw directly to the screen or the primary display device, but starting from Windows Vista, this is no longer happening, and a new feature has been introduced. This feature, called the Window Desktop Manager, is a service responsible for Desktop Composition that changes the way applications display pixels on the screen, s...

Single Instance Application in C#

Introduction Single-instance an application means enabling the application to recognize, at startup, if another running instance of itself is already running, In this case, the new application stops its execution. Generally, for a form based application, the new instance activates (brings the application window to foreground) the previous instance, if its already running. Using the code To make a single instance application, add file SingleApplication.cs in your project. It adds a new class SingleApplication defined in namespace SingleInstance and adds the following code for a form based application to your startup code: See full detail: http://www.codeproject.com/KB/cs/singleinstance.aspx

Custom windows using .NET 2.0 and the WinAPI

Image
Introduction This article shows how to create custom windows using GDI+. Background Some time ago, I had a task to create a menu for the MS PPTViewer application that allows the user to switch slides using menu items. I decided to create a menu using the WinAPI function " UpdateLayeredWindow " from the User32.dll library. This functions allow to draw any 32-bit image on the screen. For this menu, I implemented a library that I want to share. Using the code To create a custom window, you should inherit the TransparetWindow class and override the DrawPolygons method. This method should return a collection of IPolygon s. This is done in order to draw the polygons after all the calculations. See full detail: http://www.codeproject.com/KB/dialog/custom_windows.aspx

Displaying Large Quantities of Data in Windows Controls

Image
Introduction This article and accompanying code was written to demonstrate a technique for displaying very large quantities of data in a visual control, with very little performance degradation. Background It is a common situation in working with large quantities of data, where this data needs to be presented to the user. Lists and trees are often used for this purpose. Many times, the programmer attempts to load one of these controls with an entire data set. The problem arises when an increasing number of items are added to these controls; memory usage increases and performance decreases to the point that this solution is no longer acceptable. This article presents a technique to display very large data sets with no performance degradation. This technique could easily be adapted to a variety of .NET controls, as well as other platforms, such as Java Swing controls. Strategy for Displaying Large Quantities of Data in a ListView The approach for this example is to have ...

Change File Date Attributes

Image
Introduction Have you ever found yourself in a situation where you wanted to change the attributes of a file such as the creation date, modified date or last accessed date. For one reason or another, sometimes you might just want to change the attributes, and since Windows does not offer an easy way to do so, I wrote a small easy-to-use program to change the date of a file. Using the Program After extracting and executing the program, click open and load the file for which you want to change the attributes. After you open the file, you will see its path in the textbox in the bottom of the program. Now all you have to do is just click on the checkbox that you wish to edit, and select a new time and date. Click Save and the new attributes will be saved into the file. The file attributes will look like the following: Using the Code I will explain just the functions required to change the date attributes. First we add the IO library: See full detail: http://www.codepro...

Add Group Collapse Behavior on a Listview Control

Image
Introduction ListView control is an important but complex control in the WinForm environment. Group behavior is added into this control, but unfortunately, we can't collapse or expand the group. I'll show how to use some simple code to add collapse/expand behavior on the ListView control. This is a sample image on Windows Vista and Windows Server 2008: Default group without Collapse/Expand behavior Group with Collapse/Expand behavior Using the Code See full detail: http://www.codeproject.com/KB/list/ListviewGroupCollapse.aspx

GMarkupLabel - A C# Windows Forms control to display XML-formatted text

Image
Introduction I believe that almost every .NET developer, assigned a task to display text, has experienced hard times with precise text measuring and drawing. The System.Drawing.Graphics MeasureString and DrawString methods have several limitations, worst of all being not so accurate measuring and positioning of the desired text. Since .NET 2.0, Microsoft has introduced the TextRenderer class that provides more precise text manipulation, but also has its cons – may render only with solid colors, no transparency, etc. Neither of the above described approaches gives you the opportunity to have mixed font texts, extended paragraph layouts like justify, or additional text effects like stroke or shadow. All that said, I thought it would be a nice exercise to create a custom text rendering solution that both solves the standard problems and also adds some nice features. Having solid experience with GDI+ and Windows Forms (I was a GUI developer for over 4 years), I started ...

Embedding Google Earth in a C# Application

Image
Introduction If you're interested in utilizing the Google Earth viewer as a control in a custom application, then continue reading. There are quite a few links around the Internet that provide information as to how to add references to your project, create a ApplicationGE , and control the Google Earth application. However, most of these tutorials do not focus on the details of embedding Google Earth in an application. That's what I will focus on here. From now on, GE means Google Earth. The Code You'll probably start off on your embedding adventure much like I did, with: EARTHLib.ApplicationGE ge = new ApplicationGEClass(); If the GE process already exists, we will obtain a reference to it; if not, a new process will be created, and you would see the GE logo flash on the screen while loading. You will then see the main Google Earth screen with the embedded viewer. Since we are interested in embedding GE, we are not interested in having the main screen arou...

Check Internet Connection in .net

How to check internet connection in .net application? For checking Internet connection in .NET application, we can use GetIsNetworkAvailable method defined in the System.Net.Network-Information namespace. But it returns a Boolean value that denotes whether any network connection is available and does not say about Internet connectivity. To make sure we also have Internet access, the reliable method is to attempt to ping a host using System.Net.NetworkInformation; public bool IsInternet_Connected() { string hostName = " http://www.google.com " ; bool bl_result = false ; Ping p = new Ping (); try { PingReply ping_reply = p.Send(hostName, 3000); if (ping_reply.Status == IPStatus .Success) return true ; } catch (Exception ex) { throw ex; } return bl_result; } We can also use a simple API function InternetGetConnectedState. This function takes two arguments: The first one is an integer used with out keyword, which means that after calling the function, the var...

BackgroundWorker in VS.net 2005

BackgroundWorker in VS.net 2005 There are many types of operations that can take a long time to execute. For example: Downloader, File downloads and uploads (including for peer-to-peer applications) , Complex local Computing and Database transactions, Local disk access, given its slow speed relative to memory access Operations like these can cause your user interface to hang while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker component provides a convenient solution. The BackgroundWorker component gives you the ability to execute time-consuming operations asynchronously ("in the background"), on a thread different from your application's main UI thread. To use a BackgroundWorker, you simply tell it what time-consuming worker method to execute in the background, and then you call the RunWorkerAsync method. Your calling thread continues to run normally while the worker method runs as...

Listview in virtual mode C# Winform

Listview in virtual mode .net winform: virtual mode is usefull for larg number of data. two main property for listview in virtual mode 1. virtual mode=true 2. VirtualListSize=10000 //should be morethan 0 main event for virtual mode void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { e.Item = new ListViewItem(x.ToString()); } Setting the VirtualMode property to true puts the ListView into virtual mode. In Virtual mode, the normal Items collection is unused. Instead, ListViewItem objects are created dynamically as the ListView requires them. Virtual mode can be useful under many circumstances. If a ListView object must be populated from a very large collection already in memory, creating a ListViewItem object for each entry can be wasteful. In virtual mode, only the items required are created. In other cases, the values of the ListViewItem objects may need to be recalculated frequently, and doing this for the whole collection would produce unaccep...

Display updated list view items with fading color effects in a ListView control

Introduction: Normally a list view is updated by a user directly by selecting a list view item and then editing or deleting it. New items are usually added at the bottom of the list. This works fine since the user knows which items have been changed or deleted and where the new items will pop up. A drawback of it is that, if the list is sorted, then new items will not be displayed in a sorted way, instead they will be listed in the order of being created. Some list views however are automatically modified because of some event which causes the list view to be updated. If these updates occur frequently, it will be difficult for the user to see what has changed, since the user has not initiated the change and did not expect it. An example of this is a list view displaying stock prices in real time. The stock prices are usually updated in a fast manner and keeping up with these updates is not an easy task. Highlighting these changes will definitely be a welcoming feature for the user. The...