Full view of ComboBox drop-down list components in C# 3.0

By default in C# 3.0 ComboBox controls don’t provide support for showing drop-down list items if they exceed the width of their parent ComboBox, like this one:

Cropped ComboBox

Cropped ComboBox

This is annoying because users cannot read properly the information. To solve that problem, all we have to do is derive the ComboBox class and override the DropDown event as follows:

public class ComboBoxEx : ComboBox
{
	public ComboBoxEx()
		: base()
	{
		DropDown += new EventHandler(event_DropDown);
	}
 
	void event_DropDown(object sender, EventArgs e)
	{
		try
		{
			ComboBox comboBox = (ComboBox)sender; // Catch the combo firing this event
			int width = comboBox.Width; // Current width for ComboBox
			Graphics g = comboBox.CreateGraphics(); // Get graphics for ComboBox
			Font font = comboBox.Font; // Doesn't change original font
 
			//checks if a scrollbar will be displayed.
			int vertScrollBarWidth;
			if (comboBox.Items.Count > comboBox.MaxDropDownItems)
			}
				//If yes, then get its width to adjust the size of the drop down list.
				vertScrollBarWidth = SystemInformation.VerticalScrollBarWidth;
			}
			else
			{
				//Otherwise set to 0
				vertScrollBarWidth = 0;
			}
			//Loop through list items and check size of each items.
			//set the width of the drop down list to the width of the largest item.
			int newWidth;
			foreach (string s in comboBox.Items)
			{
				if (s != null)
				{
					newWidth = (int)g.MeasureString(s.Trim(), font).Width + vertScrollBarWidth;
					if (width < newWidth)
						width = newWidth;
				}
			}
			// Finally, adjust the new width
			comboBox.DropDownWidth = width;
		}
		catch {  }
	}   
}

The following picture shows the results of using the above control instead of the default one:

Non Cropped ComboBox

Non Cropped ComboBox

Share/Save/Bookmark

Controlling the commands executed with xp_cmdshell on SQL Server 2005

SQL Server has a special extended stored procedure called xp_cmdshell. This procedure has a lot of power: it allows to execute any command line code on the machine hosting the SQL Server.

Imagine you want to list all the files on C: on the SQL Server Windows host: you could write a T-SQL statement like this one:

EXECUTE master..xp_cmdshell 'dir c:'

This stored procedure, however, is a very dangerous one, as it would allow to execute harmful code. This is the reason why it’s disabled by default. Even when enabled, only users on the sysadmin role can use it.

If you ever need some users the ability to run only some specific commands with xp_cmdshell, you can use the method I’ll explain below, making use of the EXECUTE AS modifier of the stored procedure definitions in T-SQL.

Read the rest of this entry »

Share/Save/Bookmark

C# and the StringBuilder class

This morning I was working on a project at work. It’s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: “are you using a StringBuilder to use those strings?“. And I replied with this answer: “no, I am not“. This kind of stupid dialog came over because last week we were discussing about using StringBuilders instead of the default String class operators to append strings each other in Java. It seemed using the StringBuilder class resulted in an overall performance gain. It was then when I asked: “don’t tell me this happens with C#, too?“. And he answered: “yes, it does!“.

So, what’s the matter with StringBuilders in C#?

Read the rest of this entry »

Share/Save/Bookmark

Repeatable read and deadlocks in SQL Server

This week we had a bug report of one of our products regarding some strange deadlocks in our database access. For those of you who don’t know what a deadlock is, I’ll try to summarize here what a transaction is in a relational database environment and why those transactions might lead to those nasty errors, and try to explain what was causing this deadlock in our SQL Server 2005 engine.

Read the rest of this entry »

Share/Save/Bookmark

Killing all rails logs with one Ctrl+C?

Well, this is my first post after holidays and it won’t be very long.

Imagine you are developing a rails application. Usually you have:

  • a terminal with the server to see what petitions are received.
  • a terminal with a tail of development.log to see what happens with the database.
  • a terminal with a tail of test.log if you are testing something.

This are a lot of windows… And the other day one friends was very happy and after asking for a while I discovered that the reason was the simple line showed above… With only one Ctrl+C you can kill all this processes :-)

script/server & tail -f log/development.log & tail -f log/test.log & tail -f ; jobs -p | awk '{print "kill -2 " $0}' | sh

Share/Save/Bookmark

The Double Check Design Pattern

One of the deficiencies of actual programming languages, specially those ones still widely used that are old, such as C or C++, is that they were designed having in mind the sequential programming paradigm. This usually means that those languages don’t have standard ways to work with multithreading features, and you usually have to rely on third party libraries to develop thread safe software.

Today I’ll be discussing a design pattern called Double Check, which can be widely used to manage the resource access, elimination and initialization in a safe thread way.

Read the rest of this entry »

Share/Save/Bookmark

Ruby Quiz

Maybe one of the best ways to learn a new programming language is playing with it. Nowadays if you don’t code in Ruby you aren’t cool. I have to recognize that programming in Ruby is funnier than in other language and for the moment I don’t have anything bad to say about it.

Well, I want to introduce Ruby Quiz. It is a collection of minigames prepared to learn Ruby (or to improve your skills). Every week they publish one game, and if you are brave you can send your solution to them and it will be public. I think it’s one of the best ways to learn because you can compare solutions and find were you are weak in Ruby and redo your solution doing it smarter.

Share/Save/Bookmark

The madness of captchas

Nowadays computers still can’t pass the Turing Test (but some researches show that they will pass it at 2029) , well, there are still some humans that can’t. But they have achieved to pass some captcha images.

Captchas

Captchas

BitDefender and SoftScan articles show that in the last days some spam bots have registered email accounts at Hotmail, Gmail or Yahoo mail. This is a big step for spammers that produce on me two side feelings. On the one hand I’m so sad because my mail filter has to work a lot to deal with this nasty plague, but on the other hand this has to wake on us encouraging feelings of a near future where computer can pass the Turing Test. This is like in my Science Fiction books.

Share/Save/Bookmark

Managing the music between iTunes and the iPod

Suppose you have an iPod. Now suppose your music library size is by far bigger than your iPod memory. This is not an unusual situation, specially if your iPod is a Shuffle, Mini or Nano version, since the ones able to play videos usually have much more memory.

Now suppose you’re a busy person or simply don’t have the time to select what music you’ll want to hear every morning before going to work, so sitting in front of your iTunes program and selecting what you thing you’re in the mood to hear is out of the question. But of course, the chances you want to listen to different music from yesterday is high. And because you are a pragmatic geek, you’re trying to find a way to perfectly update your iPod music to fit your needs automatically.

Smart playlists come to the rescue!

Read the rest of this entry »

Share/Save/Bookmark

Sofa Control, or how to operate your Mac from the sofa

When I decided to buy my first Apple Computer, a MacBook, some months ago, I was planning on using it as a Media Center, connecting it to my HD Ready TV. And one of the features that I expected to use the most with it was the FrontRow. However, I was a little disappointed on the lack of configuration options you have on that software, specially when playing movies (I found it extremely useful anb beautiful for iTunes and iPhoto integration).

One of the first problems I encountered was with some codecs. FrontRow uses the QuickTime player, and that program seem to have some problems with video formats. I had to install some third party plugins for it to be able to play such a [now] standard video format as WMV. Another big problem I found was the lack of subtitle rendering option. English is not my native language, and although I’m improving every day, I often need to have my English spoken videos bundled with subtitles that come in external files such as SRT or SUB files. As far as I know, QuickTime player does not have support to render those files.

So one of the features I expected to use on my MacBook was ruined for some of those reasons, because using another video player in a FrontRow style was impossible… until I found Sofa Control. I’m not the kind of man to believe in the typical company “mottos”, but I firmly believe in this one: “Applications that should have been in the box”. This is the motto of CASE Apps, people responsible of Sofa Control, and I have to absolutely agree with them.

Sofa Control is a software that takes out the best use of the new Mac Family Remote Controls, which allow you to use FrontRow. Sofa Control goes further and allows you to absolutely control almost every typical piece of software for Mac OS, including some popular video players such as VLC, the one I use on Mac OS and one that supports almost every video coden and has builtin subtitle support. With Sofa Control you can use you remote to play/stop/resume multimedia files, slide pages on PowerPoint, Keynote or PDF presentations, use it as a virtual mouse moving with the control arrows, and a lot more. This is exactly what you need to simply be able to fully control your Mac when you’re laying on your bed or your sofa. The perfect software for lazy people like me! And it has a builting script manager to allow you to write your own scripts for new applications.

The only thing I miss from it it’s that it’s not free, but it costs only $14.95, and to be honest, it’s worth the price.

Share/Save/Bookmark

←Older